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

chore: early check type equality in try_unify #7263

Merged
merged 2 commits into from
Feb 3, 2025
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
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,13 @@
) -> Result<(), UnificationError> {
use Type::*;

// If the two types are exactly the same then they trivially unify.
// This check avoids potentially unifying very complex types (usually infix
// expressions) when they are the same.
if self == other {
return Ok(());
}

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

Expand Down Expand Up @@ -2312,7 +2319,7 @@
}

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

Check warning on line 2322 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 @@ -2398,7 +2405,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 2408 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 @@ -2564,7 +2571,7 @@
}
}

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

Check warning on line 2574 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> {
Expand All @@ -2585,7 +2592,7 @@

/// 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 2595 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 @@ -3055,7 +3062,7 @@
len.hash(state);
env.hash(state);
}
Type::Tuple(elems) => elems.hash(state),

Check warning on line 3065 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 3065 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)
Type::DataType(def, args) => {
def.hash(state);
args.hash(state);
Expand Down
15 changes: 9 additions & 6 deletions compiler/noirc_frontend/src/hir_def/types/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@
let dummy_span = Span::default();
// evaluate_to_field_element also calls canonicalize so if we just called
// `self.evaluate_to_field_element(..)` we'd get infinite recursion.
if let (Ok(lhs_value), Ok(rhs_value)) = (
lhs.evaluate_to_field_element_helper(&kind, dummy_span, run_simplifications),
rhs.evaluate_to_field_element_helper(&kind, dummy_span, run_simplifications),
) {
if let Ok(result) = op.function(lhs_value, rhs_value, &kind, dummy_span) {
return Type::Constant(result, kind);
if let Ok(lhs_value) =
lhs.evaluate_to_field_element_helper(&kind, dummy_span, run_simplifications)
{
if let Ok(rhs_value) =
rhs.evaluate_to_field_element_helper(&kind, dummy_span, run_simplifications)
{
if let Ok(result) = op.function(lhs_value, rhs_value, &kind, dummy_span) {
return Type::Constant(result, kind);
}
}
}

Expand Down Expand Up @@ -499,7 +502,7 @@
// Generate (arbitrary_unsigned_type, generator for that type)
fn arbitrary_unsigned_type_with_generator() -> BoxedStrategy<(Type, BoxedStrategy<FieldElement>)>
{
prop_oneof![

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (oneof)
strategy::Just((Type::FieldElement, arbitrary_field_element().boxed())),
any::<IntegerBitSize>().prop_map(|bit_size| {
let typ = Type::Integer(Signedness::Unsigned, bit_size);
Expand Down Expand Up @@ -536,7 +539,7 @@
arbitrary_value: BoxedStrategy<FieldElement>,
num_variables: usize,
) -> impl Strategy<Value = Type> {
let leaf = prop_oneof![

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (oneof)
arbitrary_variable(typ.clone(), num_variables),
arbitrary_value
.prop_map(move |value| Type::Constant(value, Kind::numeric(typ.clone()))),
Expand Down
Loading