Skip to content

Commit ae2e7af

Browse files
committed
Migrate lower range bound diagnostics
1 parent b460768 commit ae2e7af

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

compiler/rustc_error_messages/locales/en-US/mir_build.ftl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,10 @@ mir_build_const_pattern_depends_on_generic_parameter =
200200
constant pattern depends on a generic parameter
201201
202202
mir_build_could_not_eval_const_pattern = could not evaluate constant pattern
203+
204+
mir_build_lower_range_bound_must_be_less_than_or_equal_to_upper =
205+
lower range bound must be less than or equal to upper
206+
.label = lower bound larger than upper bound
207+
.teach_note = When matching against a range, the compiler verifies that the range is non-empty. Range patterns include both end-points, so this is equivalent to requiring the start of the range to be less than or equal to the end of the range.
208+
209+
mir_build_lower_range_bound_must_be_less_than_upper = lower range bound must be less than upper

compiler/rustc_mir_build/src/errors.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,20 @@ pub struct CouldNotEvalConstPattern {
480480
#[primary_span]
481481
pub span: Span,
482482
}
483+
484+
#[derive(SessionDiagnostic)]
485+
#[diag(mir_build::lower_range_bound_must_be_less_than_or_equal_to_upper, code = "E0030")]
486+
pub struct LowerRangeBoundMustBeLessThanOrEqualToUpper {
487+
#[primary_span]
488+
#[label]
489+
pub span: Span,
490+
#[note(mir_build::teach_note)]
491+
pub teach: Option<()>,
492+
}
493+
494+
#[derive(SessionDiagnostic)]
495+
#[diag(mir_build::lower_range_bound_must_be_less_than_upper, code = "E0579")]
496+
pub struct LowerRangeBoundMustBeLessThanUpper {
497+
#[primary_span]
498+
pub span: Span,
499+
}

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) use self::usefulness::MatchCheckCtxt;
1111
use crate::errors::*;
1212
use crate::thir::util::UserAnnotatedTyHelpers;
1313

14-
use rustc_errors::struct_span_err;
14+
use rustc_errors::error_code;
1515
use rustc_hir as hir;
1616
use rustc_hir::def::{CtorOf, DefKind, Res};
1717
use rustc_hir::pat_util::EnumerateAndAdjustIterator;
@@ -139,13 +139,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
139139
(RangeEnd::Excluded, Some(Ordering::Less)) => PatKind::Range(PatRange { lo, hi, end }),
140140
// `x..y` where `x >= y`. The range is empty => error.
141141
(RangeEnd::Excluded, _) => {
142-
struct_span_err!(
143-
self.tcx.sess,
144-
span,
145-
E0579,
146-
"lower range bound must be less than upper"
147-
)
148-
.emit();
142+
self.tcx.sess.emit_err(LowerRangeBoundMustBeLessThanUpper { span });
149143
PatKind::Wild
150144
}
151145
// `x..=y` where `x == y`.
@@ -154,23 +148,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
154148
(RangeEnd::Included, Some(Ordering::Less)) => PatKind::Range(PatRange { lo, hi, end }),
155149
// `x..=y` where `x > y` hence the range is empty => error.
156150
(RangeEnd::Included, _) => {
157-
let mut err = struct_span_err!(
158-
self.tcx.sess,
151+
self.tcx.sess.emit_err(LowerRangeBoundMustBeLessThanOrEqualToUpper {
159152
span,
160-
E0030,
161-
"lower range bound must be less than or equal to upper"
162-
);
163-
err.span_label(span, "lower bound larger than upper bound");
164-
if self.tcx.sess.teach(&err.get_code().unwrap()) {
165-
err.note(
166-
"When matching against a range, the compiler \
167-
verifies that the range is non-empty. Range \
168-
patterns include both end-points, so this is \
169-
equivalent to requiring the start of the range \
170-
to be less than or equal to the end of the range.",
171-
);
172-
}
173-
err.emit();
153+
teach: if self.tcx.sess.teach(&error_code!(E0030)) { Some(()) } else { None },
154+
});
174155
PatKind::Wild
175156
}
176157
}

0 commit comments

Comments
 (0)