Skip to content

Commit

Permalink
fix: allow performing bitwise NOT on unsigned integers (#4229)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

Currently running `bit_not` results in the output

```
$ nargo info --show-ssa
error: u8 cannot be used in a unary operation
  ┌─ /home/tom/Programming/aztec/noir/test_programs/execution_success/bit_not/src/main.nr:3:30
  │
3 │     let not_four_as_u8: u8 = !four_as_u8;
  │                              -----------
  │

error: u32 cannot be used in a unary operation
  ┌─ /home/tom/Programming/aztec/noir/test_programs/execution_success/bit_not/src/main.nr:6:32
  │
6 │     let not_four_as_u32: u32 = !four_as_u32;
  │                                ------------
  │
```

We're unable to perform bitwise nots for unsigned integers (signed is
fine). This PR loosens the check being applied in
`lint_overflowing_uint` so that it only errors for using an unary `-`.

## 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 committed Feb 2, 2024
1 parent afcb385 commit b3ddf10
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
13 changes: 10 additions & 3 deletions compiler/noirc_frontend/src/hir/type_check/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::hir_def::stmt::{
};
use crate::hir_def::types::Type;
use crate::node_interner::{DefinitionId, ExprId, StmtId};
use crate::UnaryOp;

use super::errors::{Source, TypeCheckError};
use super::TypeChecker;
Expand Down Expand Up @@ -361,9 +362,15 @@ impl<'interner> TypeChecker<'interner> {
};
};
}
HirExpression::Prefix(_) => self
.errors
.push(TypeCheckError::InvalidUnaryOp { kind: annotated_type.to_string(), span }),
HirExpression::Prefix(expr) => {
self.lint_overflowing_uint(&expr.rhs, annotated_type);
if matches!(expr.operator, UnaryOp::Minus) {
self.errors.push(TypeCheckError::InvalidUnaryOp {
kind: "annotated_type".to_string(),
span,
});
}
}
HirExpression::Infix(expr) => {
self.lint_overflowing_uint(&expr.lhs, annotated_type);
self.lint_overflowing_uint(&expr.rhs, annotated_type);
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/bit_not/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "bit_not"
type = "bin"
authors = [""]
compiler_version = ">=0.23.0"

[dependencies]
1 change: 1 addition & 0 deletions test_programs/execution_success/bit_not/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
four_as_u32 = 4
8 changes: 8 additions & 0 deletions test_programs/execution_success/bit_not/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main(four_as_u32: u32) {
let four_as_u8: u8 = 4;
let not_four_as_u8: u8 = !four_as_u8;
assert_eq(not_four_as_u8, 251);

let not_four_as_u32: u32 = !four_as_u32;
assert_eq(not_four_as_u32, 4294967291);
}

0 comments on commit b3ddf10

Please sign in to comment.