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

fix: fix an ICE happening when we call a closure result from if/else #2146

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ fn main() -> pub Field {
x += 1;
assert(closure_capturing_mutable(1) == 5);

regression_2154();

let ret = twice(add1, 3);

test_array_functions();
Expand Down Expand Up @@ -85,3 +87,21 @@ fn add1(x: Field) -> Field {
fn twice(f: fn(Field) -> Field, x: Field) -> Field {
f(f(x))
}

// 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
// issue https://github.com/noir-lang/noir/issues/2154
fn regression_2154() {
let x: u32 = 32;

let closure_if_else = if x > 2 {
|| x
} else {
|| x + 2342
};

assert(closure_if_else() == 32);
}
15 changes: 13 additions & 2 deletions crates/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,19 @@
HirType::Function(args, ret, env) => {
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)
let env = Self::convert_type(env);
match &env {
ast::Type::Unit => ast::Type::Function(args, ret, Box::new(env)),
ast::Type::Tuple(_elements) => ast::Type::Tuple(vec![
env.clone(),
ast::Type::Function(args, ret, Box::new(env)),
]),
jfecher marked this conversation as resolved.
Show resolved Hide resolved
_ => {
unreachable!(
"internal Type::Function env should be either a Unit or a Tuple, not {env}"
)
}
}
}

HirType::MutableReference(element) => {
Expand Down Expand Up @@ -1045,7 +1056,7 @@
expr: node_interner::ExprId,
) -> (ast::Expression, ast::Expression) {
// returns (<closure setup>, <closure variable>)
// which can be used directly in callsites or transformed

Check warning on line 1059 in crates/noirc_frontend/src/monomorphization/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (callsites)
// directly to a single `Expression`
// for other cases by `lambda` which is called by `expr`
//
Expand Down