Skip to content

Range bound error in match has bad error message when integer literal wraps #68972

@Lokathor

Description

@Lokathor
Contributor

For a match where I'm matching on a u8, I put 251..=256 on accident, and then rather than saying that 256 is out of range for u8, it decided to wrap 256 to 0 and then error that 251 is lower than the end of the range:

error[E0030]: lower range bound must be less than or equal to upper                        
  --> src\lib.rs:86:7
   |
86 |       251..=256 => StarClass::BlueGiant,
   |       ^^^ lower bound larger than upper bound

Clearly this is not a good situation.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Feb 8, 2020
hirschenberger

hirschenberger commented on Feb 11, 2020

@hirschenberger
Contributor

I'd like to fix this

@rustbot claim

self-assigned this
on Feb 11, 2020
JohnTitor

JohnTitor commented on Jul 24, 2020

@JohnTitor
Member

Triage: I'm going to release assignment due to inactivity.
@hirschenberger If you're still interested in this, feel free to re-claim.
@rustbot release-assignment

removed their assignment
on Jul 24, 2020
added
D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
A-patternsRelating to patterns and pattern matching
on Feb 8, 2021
estebank

estebank commented on Feb 9, 2021

@estebank
Contributor

Explanation on how to solve this in #81903 (comment):

the error is being emitted here

(RangeEnd::Excluded, _) => {
struct_span_err!(
self.tcx.sess,
span,
E0579,
"lower range bound must be less than upper"
)
.emit();
PatKind::Wild
}

You would also need access to the AST to figure out that the issue is an overflow, unless the const value of variant Scalar actually has the right value (255 instead of 0). If it does, it just requires you to see that the start is 0 and the end is the maximally representable integer in the type being used and add a note to the error explaining what is happening. In the former case, you will need to pass along the expressions available here into the fn emitting the error:

hir::PatKind::Range(ref lo_expr, ref hi_expr, end) => {
let (lo_expr, hi_expr) = (lo_expr.as_deref(), hi_expr.as_deref());
let lo_span = lo_expr.map_or(pat.span, |e| e.span);
let lo = lo_expr.map(|e| self.lower_range_expr(e));
let hi = hi_expr.map(|e| self.lower_range_expr(e));
let (lp, hp) = (lo.as_ref().map(|x| &x.0), hi.as_ref().map(|x| &x.0));
let mut kind = match self.normalize_range_pattern_ends(ty, lp, hp) {
Some((lc, hc)) => self.lower_pattern_range(ty, lc, hc, end, lo_span),

added a commit that references this issue on Jan 11, 2023
52d534e

2 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-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

      Participants

      @hirschenberger@estebank@jonas-schievink@Lokathor@JohnTitor

      Issue actions

        Range bound error in match has bad error message when integer literal wraps · Issue #68972 · rust-lang/rust