diff --git a/compiler/noirc_frontend/src/hir/type_check/expr.rs b/compiler/noirc_frontend/src/hir/type_check/expr.rs index 998abeedcec..11b22fb2bfe 100644 --- a/compiler/noirc_frontend/src/hir/type_check/expr.rs +++ b/compiler/noirc_frontend/src/hir/type_check/expr.rs @@ -153,11 +153,10 @@ impl<'interner> TypeChecker<'interner> { HirExpression::Call(call_expr) => { // Need to setup these flags here as `self` is borrowed mutably to type check the rest of the call expression // These flags are later used to type check calls to unconstrained functions from constrained functions - let current_func = self - .current_function - .expect("Can only have call expression inside of a function body"); - let func_mod = self.interner.function_modifiers(¤t_func); - let is_current_func_constrained = !func_mod.is_unconstrained; + let current_func = self.current_function; + let func_mod = current_func.map(|func| self.interner.function_modifiers(&func)); + let is_current_func_constrained = + !func_mod.map_or(false, |func_mod| func_mod.is_unconstrained); let is_unconstrained_call = self.is_unconstrained_call(&call_expr.func); self.check_if_deprecated(&call_expr.func); @@ -170,15 +169,14 @@ impl<'interner> TypeChecker<'interner> { }); // Check that we are not passing a mutable reference from a constrained runtime to an unconstrained runtime - for (typ, _, _) in args.iter() { - if is_current_func_constrained - && is_unconstrained_call - && matches!(&typ, Type::MutableReference(_)) - { - self.errors.push(TypeCheckError::ConstrainedReferenceToUnconstrained { - span: self.interner.expr_span(expr_id), - }); - return Type::Error; + if is_current_func_constrained && is_unconstrained_call { + for (typ, _, _) in args.iter() { + if matches!(&typ, Type::MutableReference(_)) { + self.errors.push(TypeCheckError::ConstrainedReferenceToUnconstrained { + span: self.interner.expr_span(expr_id), + }); + return Type::Error; + } } }