Skip to content

Commit 0b6c382

Browse files
authored
chore: early check type equality in try_unify (#7263)
1 parent fc75298 commit 0b6c382

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

compiler/noirc_frontend/src/hir_def/types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,13 @@ impl Type {
17421742
) -> Result<(), UnificationError> {
17431743
use Type::*;
17441744

1745+
// If the two types are exactly the same then they trivially unify.
1746+
// This check avoids potentially unifying very complex types (usually infix
1747+
// expressions) when they are the same.
1748+
if self == other {
1749+
return Ok(());
1750+
}
1751+
17451752
let lhs = self.follow_bindings_shallow();
17461753
let rhs = other.follow_bindings_shallow();
17471754

compiler/noirc_frontend/src/hir_def/types/arithmetic.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,15 @@ impl Type {
6363
let dummy_span = Span::default();
6464
// evaluate_to_field_element also calls canonicalize so if we just called
6565
// `self.evaluate_to_field_element(..)` we'd get infinite recursion.
66-
if let (Ok(lhs_value), Ok(rhs_value)) = (
67-
lhs.evaluate_to_field_element_helper(&kind, dummy_span, run_simplifications),
68-
rhs.evaluate_to_field_element_helper(&kind, dummy_span, run_simplifications),
69-
) {
70-
if let Ok(result) = op.function(lhs_value, rhs_value, &kind, dummy_span) {
71-
return Type::Constant(result, kind);
66+
if let Ok(lhs_value) =
67+
lhs.evaluate_to_field_element_helper(&kind, dummy_span, run_simplifications)
68+
{
69+
if let Ok(rhs_value) =
70+
rhs.evaluate_to_field_element_helper(&kind, dummy_span, run_simplifications)
71+
{
72+
if let Ok(result) = op.function(lhs_value, rhs_value, &kind, dummy_span) {
73+
return Type::Constant(result, kind);
74+
}
7275
}
7376
}
7477

0 commit comments

Comments
 (0)