Skip to content

Commit

Permalink
Translate Overlap eagerly
Browse files Browse the repository at this point in the history
  • Loading branch information
mejrs authored and dtolnay committed Jan 11, 2023
1 parent 3d260fa commit 372ac9c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 0 additions & 2 deletions compiler/rustc_error_messages/locales/en-US/mir_build.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,6 @@ mir_build_overlapping_range_endpoints = multiple patterns overlap on their endpo
.range = ... with this range
.note = you likely meant to write mutually exclusive ranges
mir_build_overlapping_range = this range overlaps on `{$range}`...
mir_build_non_exhaustive_omitted_pattern = some variants are not matched explicitly
.help = ensure that all variants are matched explicitly by adding the suggested match arms
.note = the matched value is of type `{$scrut_ty}` and the `non_exhaustive_omitted_patterns` attribute was found
Expand Down
19 changes: 15 additions & 4 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,17 +677,28 @@ pub struct OverlappingRangeEndpoints<'tcx> {
#[label(range)]
pub range: Span,
#[subdiagnostic]
pub overlap: Overlap<'tcx>,
pub overlap: Vec<Overlap<'tcx>>,
}

#[derive(Subdiagnostic)]
#[label(mir_build_overlapping_range)]
pub struct Overlap<'tcx> {
#[primary_span]
pub span: Span,
pub range: Pat<'tcx>,
}

impl<'tcx> AddToDiagnostic for Overlap<'tcx> {
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
where
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
{
let Overlap { span, range } = self;

// FIXME(mejrs) unfortunately `#[derive(LintDiagnostic)]`
// does not support `#[subdiagnostic(eager)]`...
let message = format!("this range overlaps on `{range}`...");
diag.span_label(span, message);
}
}

#[derive(LintDiagnostic)]
#[diag(mir_build_non_exhaustive_omitted_pattern)]
#[help]
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,16 @@ impl IntRange {
return;
}

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

if let Some(overlap) = overlap {
if !overlap.is_empty() {
pcx.cx.tcx.emit_spanned_lint(
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
hir_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ error: multiple patterns overlap on their endpoints
LL | 0..=10 => {}
| ------ this range overlaps on `10_u8`...
LL | 20..=30 => {}
| ------- this range overlaps on `20_u8`...
LL | 10..=20 => {}
| ^^^^^^^ ... with this range
|
Expand Down

0 comments on commit 372ac9c

Please sign in to comment.