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 overflows in match patterns result in unhelpful error message #94239

Closed
ArcaneNibble opened this issue Feb 22, 2022 · 1 comment · Fixed by #116623
Closed
Assignees
Labels
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.

Comments

@ArcaneNibble
Copy link

ArcaneNibble commented Feb 22, 2022

Given the following code:

pub const fn width_calc(len: usize) -> usize {
    match len {
        0..=9 => 1,
        10..=99 => 2,
        100..=999 => 3,
        1000..=9999 => 4,
        10000..=99999 => 5,
        100000..=999999 => 6,
        1000000..=9999999 => 7,
        10000000..=99999999 => 8,
        100000000..=999999999 => 9,
        1000000000..=9999999999 => 10,
        10000000000..=99999999999 => 11,
        100000000000..=999999999999 => 12,
        1000000000000..=9999999999999 => 13,
        10000000000000..=99999999999999 => 14,
        100000000000000..=999999999999999 => 15,
        1000000000000000..=9999999999999999 => 16,
        10000000000000000..=99999999999999999 => 17,
        100000000000000000..=999999999999999999 => 18,
        1000000000000000000..=9999999999999999999 => 19,
        10000000000000000000..=99999999999999999999 => 20,
        // 10000000000000000000..=18446744073709551615 => 20,
        _ => unreachable!(),
    }
}

The current output is:

error[E0030]: lower range bound must be less than or equal to upper
  --> src/lib.rs:22:9
   |
22 |         10000000000000000000..=99999999999999999999 => 20,
   |         ^^^^^^^^^^^^^^^^^^^^ lower bound larger than upper bound

warning: unreachable pattern
  --> src/lib.rs:24:9
   |
22 |         10000000000000000000..=99999999999999999999 => 20,
   |         ------------------------------------------- matches any value
23 |         // 10000000000000000000..=18446744073709551615 => 20,
24 |         _ => unreachable!(),
   |         ^ unreachable pattern
   |
   = note: `#[warn(unreachable_patterns)]` on by default

Ideally the output should look like:

error: literal out of range for `usize`
  --> src/lib.rs:22:9
   |
22 |         10000000000000000000..=99999999999999999999 => 20,
   |                                ^^^^^^^^^^^^^^^^^^^^
   |
  = note: `#[deny(overflowing_literals)]` on by default
  = note: the literal `99999999999999999999` does not fit into the type `usize` whose range is `0..=18446744073709551615`
@ArcaneNibble ArcaneNibble 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. labels Feb 22, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 26, 2022
Fix show error message when literal overflows in match patterns

Fix rust-lang#94239
This changes overflow behavior in [fn lit_to_const](https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_build/src/thir/constant.rs#L10)
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Feb 27, 2022
Fix show error message when literal overflows in match patterns

Fix rust-lang#94239
This changes overflow behavior in [fn lit_to_const](https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_build/src/thir/constant.rs#L10)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 27, 2022
Fix show error message when literal overflows in match patterns

Fix rust-lang#94239
This changes overflow behavior in [fn lit_to_const](https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_build/src/thir/constant.rs#L10)
@Nadrieril
Copy link
Member

This has since been partially fixed: the error message is correct for unsigned ints, but not signed ints, e.g. (playground link).

fn main() {
    match 0i8 {
        0..=150 => {}
        _ => {}
    }
}

@rustbot claim

@bors bors closed this as completed in 71704c4 Oct 11, 2023
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Oct 12, 2023
Fix overflow checking in range patterns

When a range pattern contains an overflowing literal, if we're not careful we might not notice the overflow and use the wrapped value. This makes for confusing error messages because linting against overflowing literals is only done in a later pass. So when a range is invalid we check for overflows to provide a better error.

This check didn't use to handle negative types; this PR fixes that. First commit adds tests, second cleans up without changing behavior, third does the fix.

EDIT: while I was at it, I fixed a small annoyance about the span of the overflow lint on negated literals.

Fixes rust-lang/rust#94239
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
2 participants