Skip to content

Commit

Permalink
Merge d990ff8 into abcae75
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Sep 16, 2024
2 parents abcae75 + d990ff8 commit 7ec87d9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
13 changes: 7 additions & 6 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,13 @@ impl<'context> Elaborator<'context> {
let expr = self.interner.expression(&expr_id);
match expr {
HirExpression::Ident(hir_ident, _) => {
let definition = self.interner.definition(hir_ident.id);
if !definition.mutable {
self.push_err(TypeCheckError::CannotMutateImmutableVariable {
name: definition.name.clone(),
span,
});
if let Some(definition) = self.interner.try_definition(hir_ident.id) {
if !definition.mutable {
self.push_err(TypeCheckError::CannotMutateImmutableVariable {
name: definition.name.clone(),
span,
});
}
}
}
HirExpression::MemberAccess(member_access) => {
Expand Down
24 changes: 24 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3533,3 +3533,27 @@ fn cannot_mutate_immutable_variable_on_member_access() {

assert_eq!(name, "foo");
}

#[test]
fn does_not_crash_when_passing_mutable_undefined_variable() {
let src = r#"
fn main() {
mutate(&mut undefined);
}
fn mutate(foo: &mut Field) {
*foo = 1;
}
"#;

let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);

let CompilationError::ResolverError(ResolverError::VariableNotDeclared { name, .. }) =
&errors[0].0
else {
panic!("Expected a VariableNotDeclared error");
};

assert_eq!(name, "undefined");
}

0 comments on commit 7ec87d9

Please sign in to comment.