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 2 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
16 changes: 13 additions & 3 deletions compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
Type::Error
})
}
HirExpression::Index(index_expr) => self.check_index_expression(index_expr),
HirExpression::Index(index_expr) => self.check_index_expression(expr_id, index_expr),
HirExpression::Call(call_expr) => {
self.check_if_deprecated(&call_expr.func);

Expand Down Expand Up @@ -396,7 +396,11 @@
}
}

fn check_index_expression(&mut self, index_expr: expr::HirIndexExpression) -> Type {
fn check_index_expression(
&mut self,
id: &ExprId,
mut index_expr: expr::HirIndexExpression,
) -> Type {
let index_type = self.check_expression(&index_expr.index);
let span = self.interner.expr_span(&index_expr.index);

Expand All @@ -408,14 +412,20 @@
}
});

// When writing `a[i]`, if `a : &mut ...` then automatically dereference `a` as many
// times as needed to get the underlying array.
let lhs_type = self.check_expression(&index_expr.collection);
let (new_lhs, lhs_type) = self.insert_auto_dereferences(index_expr.collection, lhs_type);
index_expr.collection = new_lhs;
self.interner.replace_expr(id, HirExpression::Index(index_expr));

match lhs_type.follow_bindings() {
// XXX: We can check the array bounds here also, but it may be better to constant fold first
// and have ConstId instead of ExprId for constants
Type::Array(_, base_type) => *base_type,
Type::Error => Type::Error,
typ => {
let span = self.interner.expr_span(&index_expr.collection);
let span = self.interner.expr_span(&new_lhs);
self.errors.push(TypeCheckError::TypeMismatch {
expected_typ: "Array".to_owned(),
expr_typ: typ.to_string(),
Expand Down Expand Up @@ -465,7 +475,7 @@
arguments: Vec<(Type, ExprId, Span)>,
span: Span,
) -> Type {
let (fntyp, param_len) = match method_ref {

Check warning on line 478 in compiler/noirc_frontend/src/hir/type_check/expr.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (fntyp)
HirMethodReference::FuncId(func_id) => {
if func_id == FuncId::dummy_id() {
return Type::Error;
Expand Down Expand Up @@ -494,7 +504,7 @@
});
}

let (function_type, instantiation_bindings) = fntyp.instantiate(self.interner);

Check warning on line 507 in compiler/noirc_frontend/src/hir/type_check/expr.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (fntyp)

self.interner.store_instantiation_bindings(*function_ident_id, instantiation_bindings);
self.interner.push_expr_type(function_ident_id, function_type.clone());
Expand Down Expand Up @@ -892,19 +902,19 @@
&mut self,
fn_params: &Vec<Type>,
fn_ret: &Type,
callsite_args: &Vec<(Type, ExprId, Span)>,

Check warning on line 905 in compiler/noirc_frontend/src/hir/type_check/expr.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (callsite)
span: Span,
) -> Type {
if fn_params.len() != callsite_args.len() {

Check warning on line 908 in compiler/noirc_frontend/src/hir/type_check/expr.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (callsite)
self.errors.push(TypeCheckError::ParameterCountMismatch {
expected: fn_params.len(),
found: callsite_args.len(),

Check warning on line 911 in compiler/noirc_frontend/src/hir/type_check/expr.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (callsite)
span,
});
return Type::Error;
}

for (param, (arg, _, arg_span)) in fn_params.iter().zip(callsite_args) {

Check warning on line 917 in compiler/noirc_frontend/src/hir/type_check/expr.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (callsite)
self.unify(arg, param, || TypeCheckError::TypeMismatch {
expected_typ: param.to_string(),
expr_typ: arg.to_string(),
Expand Down
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
@@ -0,0 +1,7 @@
[package]
name = "auto_deref"
type = "bin"
authors = [""]
compiler_version = "0.16.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

fn main() {
let a = &mut [1, 2, 3];
jfecher marked this conversation as resolved.
Show resolved Hide resolved
assert(a[0] == 1);

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