Skip to content

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

Closed
@ArcaneNibble

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`

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