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

Suggest ..= when someone tries to create an overflowing range #109529

Closed
D4v1d-L4u opened this issue Mar 23, 2023 · 4 comments · Fixed by #109554
Closed

Suggest ..= when someone tries to create an overflowing range #109529

D4v1d-L4u opened this issue Mar 23, 2023 · 4 comments · Fixed by #109554
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@D4v1d-L4u
Copy link

D4v1d-L4u commented Mar 23, 2023

I tried this code:

for i in 0..256 as u8 {
        println!("{}", i);
}

I expected to see this happen:
printing all numbers from 0 to 255 (inclusive)

Instead, this happened:

error: literal out of range for `u8`
  --> src/main.rs:10:17
   |
10 |     for i in 0..256 as u8 {
   |                 ^^^
   |
   = note: the literal `256` does not fit into the type `u8` whose range is `0..=255`
   = note: `#[deny(overflowing_literals)]` on by default

rustc --version --verbose:

rustc 1.68.0 (2c8cc3432 2023-03-06)
binary: rustc
commit-hash: 2c8cc343237b8f7d5a3c3703e3a87f2eb2c54a74
commit-date: 2023-03-06
host: x86_64-unknown-linux-gnu
release: 1.68.0
LLVM version: 15.0.6
@D4v1d-L4u D4v1d-L4u added the C-bug Category: This is a bug. label Mar 23, 2023
@Noratrieb
Copy link
Member

The error is correct, 256 is indeed out of range for u8. You need to use 0..=255.

@scottmcm
Copy link
Member

You can run that code if you want, by allowing the lint: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1d23a2e2a27104c5b6cb6351d5ad6722

But if you do you'll see that it will print nothing because the 256 overflows and becomes 0, so it's in 0..0 which is empty.

Examples like this where allowing the literal overflow would do something you don't want are exactly why this lint is deny-by-default 🙂

@scottmcm scottmcm changed the title literal out of range even though it is not. (0..256 as u8) Suggest ..= when someone tries to create an overflowing range Mar 23, 2023
@scottmcm scottmcm added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. and removed C-bug Category: This is a bug. labels Mar 23, 2023
@scottmcm
Copy link
Member

I've renamed this issue because the lint is correct. If there's anything to be done here, it would be to add a range-specific suggestion for this situation to help guide towards the correct code.

@Noratrieb Noratrieb added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Mar 23, 2023
@mu001999
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants