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: Avoid panic in type system #5332

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
29 changes: 24 additions & 5 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@
TypeBinding::Unbound(id) => *id,
};

assert!(!typ.occurs(id));
assert!(!typ.occurs(id), "{self:?} occurs within {typ:?}");
*self.1.borrow_mut() = TypeBinding::Bound(typ);
}

Expand Down Expand Up @@ -1343,8 +1343,7 @@
TypeBinding::Unbound(id) => *id,
};

let this = self.substitute(bindings);

let this = self.substitute(bindings).follow_bindings();
if let Some(binding) = this.get_inner_type_variable() {
match &*binding.borrow() {
TypeBinding::Bound(typ) => return typ.try_bind_to(var, bindings),
Expand Down Expand Up @@ -1743,6 +1742,15 @@
}
}

fn type_variable_id(&self) -> Option<TypeVariableId> {
match self {
Type::TypeVariable(variable, _) | Type::NamedGeneric(variable, _, _) => {
Some(variable.0)
}
_ => None,
}
}

/// Substitute any type variables found within this type with the
/// given bindings if found. If a type variable is not found within
/// the given TypeBindings, it is unchanged.
Expand Down Expand Up @@ -1777,18 +1785,29 @@
return self.clone();
}

let recur_on_binding = |id, replacement: &Type| {
// Prevent recuring forever if there's a `T := T` binding

Check warning on line 1789 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (recuring)
if replacement.type_variable_id() == Some(id) {
replacement.clone()
} else {
replacement.substitute_helper(type_bindings, substitute_bound_typevars)
}
};

let substitute_binding = |binding: &TypeVariable| {
// Check the id first to allow substituting to
// type variables that have already been bound over.
// This is needed for monomorphizing trait impl methods.
match type_bindings.get(&binding.0) {
Some((_, binding)) if substitute_bound_typevars => binding.clone(),
Some((_, replacement)) if substitute_bound_typevars => {
recur_on_binding(binding.0, replacement)
}
_ => match &*binding.borrow() {
TypeBinding::Bound(binding) => {
binding.substitute_helper(type_bindings, substitute_bound_typevars)
}
TypeBinding::Unbound(id) => match type_bindings.get(id) {
Some((_, binding)) => binding.clone(),
Some((_, replacement)) => recur_on_binding(binding.0, replacement),
None => self.clone(),
},
},
Expand Down Expand Up @@ -1838,7 +1857,7 @@
Type::Tuple(fields)
}
Type::Forall(typevars, typ) => {
// Trying to substitute_helper a variable de, substitute_bound_typevarsfined within a nested Forall

Check warning on line 1860 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsfined)
// is usually impossible and indicative of an error in the type checker somewhere.
for var in typevars {
assert!(!type_bindings.contains_key(&var.id()));
Expand Down
Loading