Skip to content

Commit

Permalink
fix: Fix occurs check (#5535)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

## Summary\*

Fixes two issues:
1. Our `occurs` check did not check the type variable ids at each step.
So if the same type variable is in the middle of several bindings, we
could miss it and bind anyway, creating a cycle.
2. `force_bind` should still do an `occurs` check

## Additional Context

## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
jfecher authored Jul 18, 2024
1 parent edb3810 commit 51dd529
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,9 @@ impl TypeVariable {
/// variable is already bound to a different type. This generally
/// a logic error to use outside of monomorphization.
pub fn force_bind(&self, typ: Type) {
*self.1.borrow_mut() = TypeBinding::Bound(typ);
if !typ.occurs(self.id()) {
*self.1.borrow_mut() = TypeBinding::Bound(typ);
}
}
}

Expand Down Expand Up @@ -1924,9 +1926,11 @@ impl Type {
generic_args.iter().any(|arg| arg.occurs(target_id))
}
Type::Tuple(fields) => fields.iter().any(|field| field.occurs(target_id)),
Type::NamedGeneric(binding, _, _) | Type::TypeVariable(binding, _) => {
match &*binding.borrow() {
TypeBinding::Bound(binding) => binding.occurs(target_id),
Type::NamedGeneric(type_var, _, _) | Type::TypeVariable(type_var, _) => {
match &*type_var.borrow() {
TypeBinding::Bound(binding) => {
type_var.id() == target_id || binding.occurs(target_id)
}
TypeBinding::Unbound(id) => *id == target_id,
}
}
Expand Down

0 comments on commit 51dd529

Please sign in to comment.