diff --git a/crates/nargo_cli/tests/test_data/higher_order_functions/src/main.nr b/crates/nargo_cli/tests/test_data/higher_order_functions/src/main.nr index fefd23b7dbc..a75ea21e61e 100644 --- a/crates/nargo_cli/tests/test_data/higher_order_functions/src/main.nr +++ b/crates/nargo_cli/tests/test_data/higher_order_functions/src/main.nr @@ -36,6 +36,21 @@ fn main() -> pub Field { x += 1; assert(closure_capturing_mutable(1) == 5); + // Fixing an ICE, where rewriting the closures + // during monomorphization didn't correspond + // to an internal `if` type + // found by @jfecher: + // https://github.com/noir-lang/noir/pull/1959#issuecomment-1658992989 + let x2: u32 = 32; + + let closure_if_else = if x2 > 2 { + || x2 + } else { + || x2 + 2342 + }; + + assert(closure_if_else() == 32); + let ret = twice(add1, 3); test_array_functions(); diff --git a/crates/noirc_frontend/src/monomorphization/mod.rs b/crates/noirc_frontend/src/monomorphization/mod.rs index c8167baf6bb..e68c9eff755 100644 --- a/crates/noirc_frontend/src/monomorphization/mod.rs +++ b/crates/noirc_frontend/src/monomorphization/mod.rs @@ -690,7 +690,18 @@ impl<'interner> Monomorphizer<'interner> { let args = vecmap(args, Self::convert_type); let ret = Box::new(Self::convert_type(ret)); let env = Box::new(Self::convert_type(env)); - ast::Type::Function(args, ret, env) + match (*env).clone() { + ast::Type::Unit => ast::Type::Function(args, ret, env), + ast::Type::Tuple(elements) => ast::Type::Tuple(vec![ + elements[0].clone(), + ast::Type::Function(args, ret, env), + ]), + _ => { + unreachable!( + "internal Type::Function env should be either a Unit or a Tuple, not {env}" + ) + } + } } HirType::MutableReference(element) => {