Skip to content

Commit

Permalink
fix: maintain correct type when simplifying x ^ x (#4082)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #4080 

## Summary\*

This PR fixes an incorrect type being applied to a simplification of `x
^ x` which then results in compiler panics should this result be used in
addition/subtraction operations.

## 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
TomAFrench authored Jan 23, 2024
1 parent 5dc259e commit 9d83c2b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ impl Binary {
return SimplifyResult::SimplifiedTo(self.lhs);
}
if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) {
let zero = dfg.make_constant(FieldElement::zero(), Type::bool());
let zero = dfg.make_constant(FieldElement::zero(), operand_type);
return SimplifyResult::SimplifiedTo(zero);
}
}
Expand Down
5 changes: 5 additions & 0 deletions test_programs/noir_test_success/regression_4080/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "regression_4080"
type = "bin"
authors = [""]
[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "5"
8 changes: 8 additions & 0 deletions test_programs/noir_test_success/regression_4080/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This test checks that `var^var` is assigned the correct type.
// https://github.com/noir-lang/noir/issues/4080

#[test(should_fail_with = "attempt to add with overflow")]
fn main() {
let var1: u8 = ((255 + 1) ^ (255 + 1)) - ((255 + 1) - (255 + 1));
assert_eq(var1, 0);
}

0 comments on commit 9d83c2b

Please sign in to comment.