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

ops::Range::end out of range (overflowing_literals) #47213

Closed
hellow554 opened this issue Jan 5, 2018 · 11 comments · Fixed by #60330
Closed

ops::Range::end out of range (overflowing_literals) #47213

hellow554 opened this issue Jan 5, 2018 · 11 comments · Fixed by #60330
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

@hellow554
Copy link
Contributor

I would expect that the the following prints would lead to the same result, instead rust complains about 256 being out of range for u8, which is correct, but because end is exclusive, it should't complain.

#![feature(inclusive_range_syntax)]

fn main() {
    let range_a = 0..256;
    let range_b = 0..=255;
    
    println!("{:?}", range_a.collect::<Vec<u8>>());
    println!("{:?}", range_b.collect::<Vec<u8>>());
}

PlayGround

@hellow554
Copy link
Contributor Author

hellow554 commented Jan 5, 2018

Okay, after thinking about this issue and talking with somebody this is very, very difficult to solve.
Range uses the underlying type (in this case it is an u8) and so it complains about being out of range (256 does not fit into a u8, period!)
In this case (aloso for u16, i16, u32, ...) it should work, yes, but range is defined for a type that implements PartialOrd and not only numeric literals which is indeed a problem.
The question, is this anyhow solveable?
If somebody stumbles across this, I'd like to hear his/her comment, but would also accept a comment closed #Notabug :)

@leonardo-m
Copy link

The solution is to use 0 ..= 255 to denote a closed interval. That syntax was invented to solve the problem you note.

@durka
Copy link
Contributor

durka commented Jan 5, 2018

This is the entire reason that inclusive ranges were added.

@durka
Copy link
Contributor

durka commented Jan 5, 2018

Actually -- perhaps keep this open as a diagnostics issue? Is it possible for overflowing_literals to have a note recommending an inclusive range? Sorry for my dismissive tone in the previous comment.

@hellow554 hellow554 reopened this Jan 5, 2018
@nagisa nagisa added A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Jan 6, 2018
@jkordish jkordish added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 12, 2018
@porglezomp
Copy link
Contributor

I'd like to try working on this issue.

@porglezomp
Copy link
Contributor

How does this look for a possible message?

warning: literal out of range for u8
  --> $DIR/suggestions.rs:61:23
   |
LL |         for _ in 0u8..256 { }
   |                  -----^^^
   |                  |
   |                  help: use a closed range: `0u8..=255`
   |
   = note: #[warn(overflowing_literals)] on by default

@leonardo-m
Copy link

That message seems nice.

@hellow554
Copy link
Contributor Author

hellow554 commented May 12, 2018

Closed or inclusive range? The feature was named inclusive, that's why I'm asking.
Also, can you please verify, that the note will not appear with 257? 😅 Would be a classic that I would forget to Check

@porglezomp
Copy link
Contributor

You're right, that should say "inclusive." I should also handle ..256u8 -> ..=255u8, even though that's probably a much less common case.

@hellow554
Copy link
Contributor Author

@porglezomp Any progress? Did you open a PR?

@porglezomp
Copy link
Contributor

I haven't made much progress unfortunately, I got distracted by other things and don't know enough about the compiler internals to work very fast. If someone else wants to pick it up that's fine by me; if not, I'll get it done eventually.

Centril added a commit to Centril/rust that referenced this issue May 1, 2019
…, r=estebank

Suggest using an inclusive range instead of an exclusive range when the endpoint overflows by 1

Fixes rust-lang#47213.
Centril added a commit to Centril/rust that referenced this issue May 1, 2019
…, r=estebank

Suggest using an inclusive range instead of an exclusive range when the endpoint overflows by 1

Fixes rust-lang#47213.
Centril added a commit to Centril/rust that referenced this issue May 1, 2019
…, r=estebank

Suggest using an inclusive range instead of an exclusive range when the endpoint overflows by 1

Fixes rust-lang#47213.
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
6 participants