Skip to content

Commit 372ac9c

Browse files
mejrsdtolnay
mejrs
authored andcommitted
Translate Overlap eagerly
1 parent 3d260fa commit 372ac9c

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

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

-2
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ mir_build_overlapping_range_endpoints = multiple patterns overlap on their endpo
323323
.range = ... with this range
324324
.note = you likely meant to write mutually exclusive ranges
325325
326-
mir_build_overlapping_range = this range overlaps on `{$range}`...
327-
328326
mir_build_non_exhaustive_omitted_pattern = some variants are not matched explicitly
329327
.help = ensure that all variants are matched explicitly by adding the suggested match arms
330328
.note = the matched value is of type `{$scrut_ty}` and the `non_exhaustive_omitted_patterns` attribute was found

compiler/rustc_mir_build/src/errors.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -677,17 +677,28 @@ pub struct OverlappingRangeEndpoints<'tcx> {
677677
#[label(range)]
678678
pub range: Span,
679679
#[subdiagnostic]
680-
pub overlap: Overlap<'tcx>,
680+
pub overlap: Vec<Overlap<'tcx>>,
681681
}
682682

683-
#[derive(Subdiagnostic)]
684-
#[label(mir_build_overlapping_range)]
685683
pub struct Overlap<'tcx> {
686-
#[primary_span]
687684
pub span: Span,
688685
pub range: Pat<'tcx>,
689686
}
690687

688+
impl<'tcx> AddToDiagnostic for Overlap<'tcx> {
689+
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
690+
where
691+
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
692+
{
693+
let Overlap { span, range } = self;
694+
695+
// FIXME(mejrs) unfortunately `#[derive(LintDiagnostic)]`
696+
// does not support `#[subdiagnostic(eager)]`...
697+
let message = format!("this range overlaps on `{range}`...");
698+
diag.span_label(span, message);
699+
}
700+
}
701+
691702
#[derive(LintDiagnostic)]
692703
#[diag(mir_build_non_exhaustive_omitted_pattern)]
693704
#[help]

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,16 @@ impl IntRange {
285285
return;
286286
}
287287

288-
// Get the first overlap. We get only the first rather than all of them
289-
// because displaying multiple overlaps requires a way to eagerly translate
290-
// lintdiagnostics, but that doesn't exist.
291-
let overlap = pats
288+
let overlap: Vec<_> = pats
292289
.filter_map(|pat| Some((pat.ctor().as_int_range()?, pat.span())))
293290
.filter(|(range, _)| self.suspicious_intersection(range))
294291
.map(|(range, span)| Overlap {
295292
range: self.intersection(&range).unwrap().to_pat(pcx.cx.tcx, pcx.ty),
296293
span,
297294
})
298-
.next();
295+
.collect();
299296

300-
if let Some(overlap) = overlap {
297+
if !overlap.is_empty() {
301298
pcx.cx.tcx.emit_spanned_lint(
302299
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
303300
hir_id,

tests/ui/pattern/usefulness/integer-ranges/overlapping_range_endpoints.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ error: multiple patterns overlap on their endpoints
5959
LL | 0..=10 => {}
6060
| ------ this range overlaps on `10_u8`...
6161
LL | 20..=30 => {}
62+
| ------- this range overlaps on `20_u8`...
6263
LL | 10..=20 => {}
6364
| ^^^^^^^ ... with this range
6465
|

0 commit comments

Comments
 (0)