diff --git a/crates/nargo/tests/test_data/struct/src/main.nr b/crates/nargo/tests/test_data/struct/src/main.nr index 197223f0510..1b3defb09a5 100644 --- a/crates/nargo/tests/test_data/struct/src/main.nr +++ b/crates/nargo/tests/test_data/struct/src/main.nr @@ -2,6 +2,7 @@ use dep::std; struct Foo { bar: Field, + array: [Field; 2], } struct Pair { @@ -10,8 +11,8 @@ struct Pair { } impl Foo { - fn default() -> Self { - Self { bar: 0 } + fn default(x: Field,y: Field) -> Self { + Self { bar: 0, array: [x,y] } } } @@ -26,10 +27,11 @@ impl Pair { } fn main(x: Field, y: Field) { - let first = Foo::default(); + let first = Foo::default(x,y); let p = Pair { first, second: 1 }; constrain p.bar() == x; constrain p.second == y; + constrain p.first.array[0] != p.first.array[1]; } diff --git a/crates/noirc_evaluator/src/ssa/code_gen.rs b/crates/noirc_evaluator/src/ssa/code_gen.rs index 32e83498b9f..ecc07fc9bb3 100644 --- a/crates/noirc_evaluator/src/ssa/code_gen.rs +++ b/crates/noirc_evaluator/src/ssa/code_gen.rs @@ -212,14 +212,25 @@ impl IRGenerator { array: &LValue, index: &Expression, env: &mut Environment, - ) -> Result<(ArrayId, NodeId), RuntimeError> { - let ident_def = Self::lvalue_ident_def(array); - let val = self.find_variable(ident_def).unwrap(); - let lhs = val.unwrap_id(); - - let a_id = self.context.get_object_type(lhs).type_to_pointer(); + ) -> Result<(NodeId, NodeId), RuntimeError> { + let value = self.lvalue_to_value(array); + let lhs = value.unwrap_id(); let index = self.codegen_expression(env, index)?.unwrap_id(); - Ok((a_id, index)) + Ok((lhs, index)) + } + + fn lvalue_to_value(&self, lvalue: &LValue) -> &Value { + match lvalue { + LValue::Ident(ident) => self.find_variable(ident.id).unwrap(), + LValue::Index { array, .. } => { + self.find_variable(Self::lvalue_ident_def(array.as_ref())).unwrap() + } + LValue::MemberAccess { object, field_index, .. } => { + let ident_def = Self::lvalue_ident_def(object.as_ref()); + let val = self.find_variable(ident_def).unwrap(); + val.get_field_member(*field_index) + } + } } fn lvalue_ident_def(lvalue: &LValue) -> DefinitionId { @@ -406,10 +417,8 @@ impl IRGenerator { self.variable_values.insert(ident_def, result); } LValue::Index { array, index } => { - let (_, array_idx) = self.codegen_indexed_value(array.as_ref(), index, env)?; - let val = self.find_variable(ident_def).unwrap(); + let (lhs_id, array_idx) = self.codegen_indexed_value(array.as_ref(), index, env)?; let rhs_id = rhs.unwrap_id(); - let lhs_id = val.unwrap_id(); self.context.handle_assign(lhs_id, Some(array_idx), rhs_id)?; } LValue::MemberAccess { object: _, field_index } => { diff --git a/crates/noirc_evaluator/src/ssa/node.rs b/crates/noirc_evaluator/src/ssa/node.rs index 42faf5932d1..486edc38c44 100644 --- a/crates/noirc_evaluator/src/ssa/node.rs +++ b/crates/noirc_evaluator/src/ssa/node.rs @@ -255,13 +255,6 @@ impl ObjectType { } } - pub fn type_to_pointer(&self) -> ArrayId { - match self { - ObjectType::Pointer(a) => *a, - _ => unreachable!("Type is not a pointer",), - } - } - pub fn field_to_type(&self, f: FieldElement) -> FieldElement { match self { ObjectType::NotAnObject | ObjectType::Pointer(_) => {