Skip to content

Integer literal overflows in match patterns result in unhelpful error message #94239

Description

@ArcaneNibble

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`

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Feb 22, 2022
  2. added a commit that references this issue on Feb 26, 2022
    da3db4e
  3. added a commit that references this issue on Feb 27, 2022
    02c614b
  4. added a commit that references this issue on Feb 27, 2022
    734a8e6
  5. Nadrieril commented on Oct 11, 2023

    @Nadrieril
    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

  6. added a commit that references this issue on Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions