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

Silent failure for empty range #54204

Closed
jeffreydecker opened this issue Sep 13, 2018 · 6 comments
Closed

Silent failure for empty range #54204

jeffreydecker opened this issue Sep 13, 2018 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints 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.

Comments

@jeffreydecker
Copy link

jeffreydecker commented Sep 13, 2018

I've just run into an issue where with trying to for loop over a range where the upper bound is one above the MAX of the primitive represented by i as shown below:

// i is u8
for i in 0..256 {...}

I understand that in this particular case 0 and 256 are the same, meaning that the range 0..0 and 0..256 with regard to u8 are the same. Part of the issue in my opinion is that I'd argue that the above code is also equivalent to the below code:

// i is u8
for i in 0..=255 {...}

I definitely understand the argument that they aren't so to be fair, if the first example can/will not be supported that's totally reasonable seeing as 256 is outside of the range of a u8.

However, I do think it's reasonable for the compiler to at least bark at you about the first example considering the loop and it's contained code are essentially dead/unreachable code.

My initial proposal would be to just have the compiler at least warn you about the unreachable nature of your code from example 1. My pie in the sky ask would be that the compiler would be smart enough to handle the first example, again though, I do understand why that might not be a popular ask.

@ExpHP
Copy link
Contributor

ExpHP commented Sep 13, 2018

But it does bark at you:

warning: literal out of range for u8
 --> src/main.rs:4:17
  |
4 |     for i in 0..256 {
  |                 ^^^
  |
  = note: #[warn(overflowing_literals)] on by default

@jeffreydecker
Copy link
Author

Hmmm, you're right! I guess I ran into this within a macro I'm working on so I just don't get some of the compiler stuffs in that case. Must have missed the waring the first time I ran the example outside of a macro. Thanks!

I realized after writing this issue that I also meant to include the example below which does not complain:

// i is u8
for i in 0..0 {...}

Obviously it's a ridiculous scenario so probably not worth covering but to me 0..0 and 0..256 should probably warn you in the same/a similar way. So maybe warning about unreachable code?

Either way, thanks for the quick follow up 😸

@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints labels Sep 13, 2018
@estebank
Copy link
Contributor

Agree that we should probably add an "empty range" lint for cases like 0..0.

@estebank estebank changed the title Silent failure at upper bound of non inclusive range Silent failure for empty range Sep 14, 2018
@hellow554
Copy link
Contributor

Related: #47213

@zackmdavis
Copy link
Member

This is covered by Clippy's reverse_range_loop lint, which is on #53224's shortlist of Clippy lints to potentially uplift to rustc.

@estebank
Copy link
Contributor

Then we can safely close this ticket, as it will be handled by the clippy one.

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 A-lints 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.
Projects
None yet
Development

No branches or pull requests

5 participants