Skip to content

Commit

Permalink
fix: Force src impl for == on slices (#4507)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
jfecher committed Mar 7, 2024
1 parent 1510702 commit 1691274
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/slices/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}

0 comments on commit 1691274

Please sign in to comment.