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

Incorrect overflowing literals error with saturating_sub #63733

Closed
bjorn3 opened this issue Aug 20, 2019 · 4 comments
Closed

Incorrect overflowing literals error with saturating_sub #63733

bjorn3 opened this issue Aug 20, 2019 · 4 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bjorn3
Copy link
Member

bjorn3 commented Aug 20, 2019

fn main() {
    assert_eq!(i8::min_value(), -128i8);
    println!("{}", -128i8.saturating_sub(-1));
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error: literal out of range for `i8`
 --> src/main.rs:3:21
  |
3 |     println!("{}", -128i8.saturating_sub(-1));
  |                     ^^^^^
  |
  = note: #[deny(overflowing_literals)] on by default

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

-128 is not out of range for i8, however the lint claims it is. This doesn't happen when printing a plain -128i8 without .saturating_sub. It also doesn't happen when the -128i8 is passed as arg to .saturating_sub.

@bjorn3
Copy link
Member Author

bjorn3 commented Aug 20, 2019

Never mind. Operator precedence was confusing. The - gets applied after calling saturating_sub.

@hellow554
Copy link
Contributor

Could a hint be emitted? E.g.

help: did you mean (-128i8).saturating_sub(-1)?

@jonas-schievink jonas-schievink added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Aug 20, 2019
@Centril Centril added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 20, 2019
@Centril
Copy link
Contributor

Centril commented Aug 20, 2019

I'm not in favor of a general lint to suggest parenthesizing -$lit.method() but adding a hint to the existing overflow lint would be fine.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 15, 2020
…flow, r=ecstatic-morse

Add more context to the literal overflow message

related to issue rust-lang#63733
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 16, 2020
…flow, r=ecstatic-morse

Add more context to the literal overflow message

related to issue rust-lang#63733
@oli-obk
Copy link
Contributor

oli-obk commented Jun 8, 2021

This has been solved in #69995

@oli-obk oli-obk closed this as completed Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants