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

Integer literal overflow/underflow doesn't always error when assigning to explicitly typed variable #5419

Closed
asterite opened this issue Jul 5, 2024 · 0 comments · Fixed by #5416
Assignees
Labels
bug Something isn't working

Comments

@asterite
Copy link
Collaborator

asterite commented Jul 5, 2024

Aim

The compiler should error when an integer literal is assigned to a variable with an explicit integer type, and that literal is outside the range of the integer.

This is already the case, but only for unsigned integers, but only if positive values exceed the maximum. What's missing:

  • check that negative values can't be assigned to unsigned integers
  • check min/max against signed integers

This is similar to #5372, except that here the compiler does an early check with explicitly annotated types, and in the related issue the check is done later on, once unknown types are resolved.

Expected Behavior

All of these programs should error:

fn main() {
  let x: u8 = -1;
}
fn main() {
  let x: i8 = -129;
}
fn main() {
  let x: i8 = 128;
}

Bug

The above programs don't error.

To Reproduce

No response

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

None

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

Yes

Support Needs

No response

@asterite asterite added the bug Something isn't working label Jul 5, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jul 5, 2024
github-merge-queue bot pushed a commit that referenced this issue Jul 5, 2024
…5416)

# Description

## Problem

Resolves #5419

## Summary

This is similar to #5372, except that here the compiler does an early
check with explicitly annotated types, and in the related issue the
check is done later on, once unknown types are resolved.

Also in this PR:
- a test was moved from `test_programs` to `noirc_frontend/src/tests.rs`
because the error produced here can be caught there
- the error message produced in both places is now exactly the same
(showing the possible range of values, which helps to know why you
exceeded the min/max)

## Additional Context

None

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** 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.
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jul 5, 2024
@asterite asterite self-assigned this Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

1 participant