Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative integer literals are not supported for unsigned types #2583

Closed
Jurarpereurs opened this issue Sep 7, 2023 · 1 comment · Fixed by #2742
Closed

Negative integer literals are not supported for unsigned types #2583

Jurarpereurs opened this issue Sep 7, 2023 · 1 comment · Fixed by #2742
Assignees
Labels
bug Something isn't working compiler frontend `noirc_frontend` crate

Comments

@Jurarpereurs
Copy link

Aim

Noir throws an error saying that "u32 cannot be used in a unary operation" when directly assigning a negative integer literal to an unsigned integer type.

let a: u32 = -1;

However, if you do some operations on the negative integer literal, the error is not thrown.

let a: u32 = -1 - 1;

Expected Behavior

Noir should not throw an error when directly assigning a negative integer literal to an unsigned integer type.

Bug

Create a new Noir project with the following command:

noir new neg

Save the following code into src/main.noir:

fn main() {
    let a: u32 = -1;
}

Run nargo prove and observe the following error:

warning: unused variable a
  ┌─ /home/Jurarpereurs/neg/src/main.nr:2:9
  │
2 │     let a: u32 = -1;
  │         - unused variable
  │

error: u32 cannot be used in a unary operation
  ┌─ /home/Jurarpereurs/neg/src/main.nr:2:18
  │
2 │     let a: u32 = -1;
  │                  --
  │

Error: Aborting due to 1 previous error

Location:
    crates/nargo_cli/src/cli/mod.rs:79:5

To see that the error is not thrown when doing some operations on the negative integer literal, save the following code into src/main.noir:

fn main() {
    let a: u32 = -1 - 1;
}

Run nargo prove and observe that the error is not thrown.

To Reproduce

  1. Create a new Noir project with the following command:

    noir new neg
  2. Save the following code into src/main.noir:

    fn main() {
        let a: u32 = -1;
    }
  3. Run nargo prove and observe the error.

Installation Method

Binary

Nargo Version

nargo 0.10.5 (git version hash: 9fe4cfd, is dirty: false)

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@Jurarpereurs Jurarpereurs added the bug Something isn't working label Sep 7, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Sep 7, 2023
@jfecher jfecher added P-MEDIUM compiler frontend `noirc_frontend` crate labels Sep 7, 2023
@guipublic
Copy link
Contributor

The issue is not that -1 is forbidden, but rather the fact that -1-1 is allowed. This is because the check we perform is too simple.
We have a similar issue if you assign 300 to u8 vs 300+4 to u8.
Because we are moving toward preventing integer overflows, the solution is to error on overflows also in these cases

@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler frontend `noirc_frontend` crate
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants