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: Do a shallow follow_bindings before unification #6558

Merged
merged 1 commit into from
Nov 19, 2024
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
22 changes: 20 additions & 2 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,12 +1547,15 @@
) -> Result<(), UnificationError> {
use Type::*;

let lhs = match self {
let lhs = self.follow_bindings_shallow();
let rhs = other.follow_bindings_shallow();

let lhs = match lhs.as_ref() {
Type::InfixExpr(..) => Cow::Owned(self.canonicalize()),
other => Cow::Borrowed(other),
};

let rhs = match other {
let rhs = match rhs.as_ref() {
Type::InfixExpr(..) => Cow::Owned(other.canonicalize()),
other => Cow::Borrowed(other),
};
Expand Down Expand Up @@ -2135,7 +2138,7 @@
}

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

Check warning on line 2141 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 {
Expand Down Expand Up @@ -2220,7 +2223,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 2226 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 Expand Up @@ -2386,13 +2389,28 @@
}
}

/// Follow bindings if this is a type variable or generic to the first non-typevariable

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevariable)
/// type. Unlike `follow_bindings`, this won't recursively follow any bindings on any
/// fields or arguments of this type.
pub fn follow_bindings_shallow(&self) -> Cow<Type> {
match self {
Type::TypeVariable(var) | Type::NamedGeneric(var, _) => {
if let TypeBinding::Bound(typ) = &*var.borrow() {
return Cow::Owned(typ.follow_bindings_shallow().into_owned());
}
Cow::Borrowed(self)
}
other => Cow::Borrowed(other),
}
}

pub fn from_generics(generics: &GenericTypeVars) -> Vec<Type> {
vecmap(generics, |var| Type::TypeVariable(var.clone()))
}

/// Replace any `Type::NamedGeneric` in this type with a `Type::TypeVariable`
/// using to the same inner `TypeVariable`. This is used during monomorphization
/// to bind to named generics since they are unbindable during type checking.

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (unbindable)
pub fn replace_named_generics_with_type_variables(&mut self) {
match self {
Type::FieldElement
Expand Down Expand Up @@ -2853,7 +2871,7 @@
len.hash(state);
env.hash(state);
}
Type::Tuple(elems) => elems.hash(state),

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)
Type::Struct(def, args) => {
def.hash(state);
args.hash(state);
Expand Down
6 changes: 4 additions & 2 deletions compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,8 @@ impl<'interner> Monomorphizer<'interner> {

/// Convert a non-tuple/struct type to a monomorphized type
fn convert_type(typ: &HirType, location: Location) -> Result<ast::Type, MonomorphizationError> {
Ok(match typ {
let typ = typ.follow_bindings_shallow();
Ok(match typ.as_ref() {
HirType::FieldElement => ast::Type::Field,
HirType::Integer(sign, bits) => ast::Type::Integer(*sign, *bits),
HirType::Bool => ast::Type::Bool,
Expand Down Expand Up @@ -1125,7 +1126,8 @@ impl<'interner> Monomorphizer<'interner> {

// Similar to `convert_type` but returns an error if any type variable can't be defaulted.
fn check_type(typ: &HirType, location: Location) -> Result<(), MonomorphizationError> {
match typ {
let typ = typ.follow_bindings_shallow();
match typ.as_ref() {
HirType::FieldElement
| HirType::Integer(..)
| HirType::Bool
Expand Down
Loading