From 169127444e8b16a8aad4acfe29ba812894fd897c Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 7 Mar 2024 15:09:50 -0600 Subject: [PATCH] fix: Force src impl for == on slices (#4507) # Description ## Problem\* Resolves #4506 ## Summary\* `==` on slices previously tried to use the built-in impl we have but this leads to a panic when evaluating binary operators in SSA. We expect both sides of the binary to be non-tuples but this isn't true for slice values. Instead of making this work for tuples I changed the type checker to force slices to use the stdlib impl we have for `==` rather than the built in one. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** 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. --- compiler/noirc_frontend/src/hir/type_check/expr.rs | 8 +++++++- test_programs/execution_success/slices/src/main.nr | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/hir/type_check/expr.rs b/compiler/noirc_frontend/src/hir/type_check/expr.rs index 7b854e58fca..c5287d35caf 100644 --- a/compiler/noirc_frontend/src/hir/type_check/expr.rs +++ b/compiler/noirc_frontend/src/hir/type_check/expr.rs @@ -863,7 +863,13 @@ impl<'interner> TypeChecker<'interner> { span: op.location.span, }); - self.comparator_operand_type_rules(x_type, y_type, op, span) + let (_, use_impl) = self.comparator_operand_type_rules(x_type, y_type, op, span)?; + + // If the size is not constant, we must fall back to a user-provided impl for + // equality on slices. + let size = x_size.follow_bindings(); + let use_impl = use_impl || size.evaluate_to_u64().is_none(); + Ok((Bool, use_impl)) } (String(x_size), String(y_size)) => { diff --git a/test_programs/execution_success/slices/src/main.nr b/test_programs/execution_success/slices/src/main.nr index eca42a660c4..6823bf05d96 100644 --- a/test_programs/execution_success/slices/src/main.nr +++ b/test_programs/execution_success/slices/src/main.nr @@ -50,6 +50,8 @@ fn main(x: Field, y: pub Field) { // The parameters to this function must come from witness values (inputs to main) regression_merge_slices(x, y); regression_2370(); + + regression_4506(); } // Ensure that slices of struct/tuple values work. fn regression_2083() { @@ -297,3 +299,8 @@ fn regression_2370() { let mut slice = []; slice = [1, 2, 3]; } + +fn regression_4506() { + let slice: [Field] = [1, 2, 3]; + assert(slice == slice); +}