diff --git a/compiler/noirc_frontend/src/elaborator/expressions.rs b/compiler/noirc_frontend/src/elaborator/expressions.rs index ca7603b23ed..311c8f401be 100644 --- a/compiler/noirc_frontend/src/elaborator/expressions.rs +++ b/compiler/noirc_frontend/src/elaborator/expressions.rs @@ -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) => { diff --git a/compiler/noirc_frontend/src/tests.rs b/compiler/noirc_frontend/src/tests.rs index e61437cb80e..0c993f2ac0b 100644 --- a/compiler/noirc_frontend/src/tests.rs +++ b/compiler/noirc_frontend/src/tests.rs @@ -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"); +}