Skip to content

Commit

Permalink
fix: unsigned integers cannot be negated (#3688)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #3649 

## Summary\*

Forbid negation of unsigned integers

## 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
guipublic authored Dec 6, 2023
1 parent 468fbbc commit f904ae1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,10 @@ impl<'interner> TypeChecker<'interner> {

match op {
crate::UnaryOp::Minus => {
if rhs_type.is_unsigned() {
self.errors
.push(TypeCheckError::InvalidUnaryOp { kind: rhs_type.to_string(), span });
}
let expected = Type::polymorphic_integer(self.interner);
rhs_type.unify(&expected, &mut self.errors, || TypeCheckError::InvalidUnaryOp {
kind: rhs_type.to_string(),
Expand Down
6 changes: 6 additions & 0 deletions test_programs/compile_failure/negate_unsigned/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "negate_unsigned"
type = "bin"
authors = [""]

[dependencies]
Empty file.
6 changes: 6 additions & 0 deletions test_programs/compile_failure/negate_unsigned/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use dep::std;

fn main() {
let var = -1 as u8;
std::println(var);
}

0 comments on commit f904ae1

Please sign in to comment.