Skip to content

Compiler fails to warn about certain unreachable match arms containing exclusive ranges #43267

Closed
@feadoor

Description

@feadoor
#![feature(exclusive_range_pattern)]

fn main() {
    match 10 {
        1...10 => {},
        1..10 => {},
         _ => {},        
    };

    match 10 {
        1...10 => {},
        1..11 => {},
         _ => {},        
    };
}

In each case, the second arm of the match is unreachable, but the compiler fails to generate a warning.

This is related to #43253, but more tricky to resolve. There are problems with all of the first three match arms of the following function from librustc_const_eval/_match.rs:

fn range_covered_by_constructor(tcx: TyCtxt, span: Span,
                                ctor: &Constructor,
                                from: &ConstVal, to: &ConstVal,
                                end: RangeEnd) {
    ...
}

The problem with the first two arms is resolved by #43266. The third arm cannot be fixed in a similar fashion. Essentially, this function tries to work out whether the range represented by ctor fits entirely within the range determined by the function parameters, and does so using the results of comparisons between the endpoints of the two ranges.

This is not sufficient information to determine if an exclusive range fits within an inclusive range. In particular, for ranges of integers:

  • 1..11 fits entirely within 1...10
  • 1..12 does not fit entirely within 1...10

This is despite the results of comparisons between the range endpoints are the same in each case.

The fix here will probably need to treat discrete ranges (e.g. of integers) differently from continuous ranges (e.g. of floating point numbers - yes, everything is discrete to a computer, but that's not how people think when actually using floating point numbers!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.F-exclusive_range_pattern`#![feature(exclusive_range_pattern)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions