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 when reassigning a mutable lambda variable to one with a different environment type #2172

Merged
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
@@ -0,0 +1,9 @@
fn bad() {
let a: i32 = 100;
let b: i32 = 200;

let mut f = || a;

// this should fail with a type error, since the closures have different environments & types
f = || a + b;
}
8 changes: 6 additions & 2 deletions crates/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@
Type::Bool(comp_time) => write!(f, "{comp_time}bool"),
Type::String(len) => write!(f, "str<{len}>"),
Type::FmtString(len, elements) => {
write!(f, "fmtstr<{len}, {elements}>")

Check warning on line 790 in crates/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (fmtstr)
}
Type::Unit => write!(f, "()"),
Type::Error => write!(f, "error"),
Expand Down Expand Up @@ -1206,12 +1206,14 @@
}
}

(Function(params_a, ret_a, _env_a), Function(params_b, ret_b, _env_b)) => {
(Function(params_a, ret_a, env_a), Function(params_b, ret_b, env_b)) => {
if params_a.len() == params_b.len() {
for (a, b) in params_a.iter().zip(params_b.iter()) {
a.try_unify(b, span)?;
}

env_a.try_unify(env_b, span)?;

ret_b.try_unify(ret_a, span)
} else {
Err(SpanKind::None)
Expand Down Expand Up @@ -1413,12 +1415,14 @@
}
}

(Function(params_a, ret_a, _env_a), Function(params_b, ret_b, _env_b)) => {
(Function(params_a, ret_a, env_a), Function(params_b, ret_b, env_b)) => {
if params_a.len() == params_b.len() {
for (a, b) in params_a.iter().zip(params_b) {
a.is_subtype_of(b, span)?;
}

env_a.is_subtype_of(env_b, span)?;

// return types are contravariant, so this must be ret_b <: ret_a instead of the reverse
ret_b.is_subtype_of(ret_a, span)
} else {
Expand Down