Skip to content

Commit 5c64edf

Browse files
lzcuntmejrs
authored and
mejrs
committed
Migrate lower range bound diagnostics
1 parent baa6c15 commit 5c64edf

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;
@@ -141,13 +141,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
141141
}
142142
// `x..y` where `x >= y`. The range is empty => error.
143143
(RangeEnd::Excluded, _) => {
144-
struct_span_err!(
145-
self.tcx.sess,
146-
span,
147-
E0579,
148-
"lower range bound must be less than upper"
149-
)
150-
.emit();
144+
self.tcx.sess.emit_err(LowerRangeBoundMustBeLessThanUpper { span });
151145
PatKind::Wild
152146
}
153147
// `x..=y` where `x == y`.
@@ -158,23 +152,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
158152
}
159153
// `x..=y` where `x > y` hence the range is empty => error.
160154
(RangeEnd::Included, _) => {
161-
let mut err = struct_span_err!(
162-
self.tcx.sess,
155+
self.tcx.sess.emit_err(LowerRangeBoundMustBeLessThanOrEqualToUpper {
163156
span,
164-
E0030,
165-
"lower range bound must be less than or equal to upper"
166-
);
167-
err.span_label(span, "lower bound larger than upper bound");
168-
if self.tcx.sess.teach(&err.get_code().unwrap()) {
169-
err.note(
170-
"When matching against a range, the compiler \
171-
verifies that the range is non-empty. Range \
172-
patterns include both end-points, so this is \
173-
equivalent to requiring the start of the range \
174-
to be less than or equal to the end of the range.",
175-
);
176-
}
177-
err.emit();
157+
teach: if self.tcx.sess.teach(&error_code!(E0030)) { Some(()) } else { None },
158+
});
178159
PatKind::Wild
179160
}
180161
}

0 commit comments

Comments
 (0)