Skip to content

Commit

Permalink
Accessing array inside a struct bug fix (#396)
Browse files Browse the repository at this point in the history
* Fix an issue when accessing array inside a struct

* Fix cargo clippy
  • Loading branch information
guipublic authored Oct 28, 2022
1 parent 20048e7 commit 22a92a9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
8 changes: 5 additions & 3 deletions crates/nargo/tests/test_data/struct/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use dep::std;

struct Foo {
bar: Field,
array: [Field; 2],
}

struct Pair {
Expand All @@ -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] }
}
}

Expand All @@ -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];
}

29 changes: 19 additions & 10 deletions crates/noirc_evaluator/src/ssa/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 } => {
Expand Down
7 changes: 0 additions & 7 deletions crates/noirc_evaluator/src/ssa/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand Down

0 comments on commit 22a92a9

Please sign in to comment.