Skip to content

Commit

Permalink
fix: allow performing bitwise NOT on unsigned integers
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 1, 2024
1 parent c284e01 commit de7c5ce
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 @@
x = 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 de7c5ce

Please sign in to comment.