Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement automatic dereferencing for indexing lvalues #3083

Merged
merged 7 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions compiler/noirc_frontend/src/hir/type_check/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,20 @@ impl<'interner> TypeChecker<'interner> {
},
);

let (array_type, array, mutable) = self.check_lvalue(array, assign_span);
let array = Box::new(array);
let (mut lvalue_type, mut lvalue, mut mutable) =
self.check_lvalue(array, assign_span);

// Before we check that the lvalue is an array, try to dereference it as many times
// as needed to unwrap any &mut wrappers.
while let Type::MutableReference(element) = lvalue_type.follow_bindings() {
let element_type = element.as_ref().clone();
lvalue = HirLValue::Dereference { lvalue: Box::new(lvalue), element_type };
lvalue_type = *element;
// We know this value to be mutable now since we found an `&mut`
mutable = true;
}

let typ = match array_type.follow_bindings() {
let typ = match lvalue_type.follow_bindings() {
Type::Array(_, elem_type) => *elem_type,
Type::Error => Type::Error,
other => {
Expand All @@ -265,6 +275,7 @@ impl<'interner> TypeChecker<'interner> {
}
};

let array = Box::new(lvalue);
(typ.clone(), HirLValue::Index { array, index: *index, typ }, mutable)
}
HirLValue::Dereference { lvalue, element_type: _ } => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

fn main() {
let a = &mut [1, 2, 3];
let a = &mut &mut &mut [1, 2, 3];
assert(a[0] == 1);

a[0] = 4;
assert(a[0] == 4);
}
Loading