Skip to content

Commit ef33072

Browse files
mejrsdtolnay
mejrs
authored andcommitted
Migrate usefulness.rs
1 parent 5d2b9a9 commit ef33072

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,24 @@ mir_build_float_pattern = floating-point types cannot be used in patterns
313313
314314
mir_build_pointer_pattern = function pointers and unsized pointers in patterns behave unpredictably and should not be relied upon. See https://github.com/rust-lang/rust/issues/70861 for details.
315315
316-
mir_build_indirect_structural_match =
316+
mir_build_indirect_structural_match =
317317
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq, Eq)]`
318318
319-
mir_build_nontrivial_structural_match =
319+
mir_build_nontrivial_structural_match =
320320
to use a constant of type `{$non_sm_ty}` in a pattern, the constant's initializer must be trivial or `{$non_sm_ty}` must be annotated with `#[derive(PartialEq, Eq)]`
321321
322322
mir_build_overlapping_range_endpoints = multiple patterns overlap on their endpoints
323323
.range = ... with this range
324324
.note = you likely meant to write mutually exclusive ranges
325325
326326
mir_build_overlapping_range = this range overlaps on `{$range}`...
327+
328+
mir_build_non_exhaustive_omitted_pattern = some variants are not matched explicitly
329+
.label = {$count ->
330+
[1] pattern `{$witness_1}`
331+
[2] patterns `{$witness_1}` and `{$witness_2}`
332+
[3] patterns `{$witness_1}`, `{$witness_2}` and `{$witness_3}`
333+
*[other] patterns `{$witness_1}`, `{$witness_2}`, `{$witness_3}` and more
334+
} not covered
335+
.help = ensure that all variants are matched explicitly by adding the suggested match arms
336+
.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

+14
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,17 @@ pub struct Overlap<'tcx> {
685685
pub span: Span,
686686
pub range: Pat<'tcx>,
687687
}
688+
689+
#[derive(LintDiagnostic)]
690+
#[diag(mir_build_non_exhaustive_omitted_pattern)]
691+
#[help]
692+
#[note]
693+
pub(crate) struct NonExhaustiveOmittedPattern<'tcx> {
694+
pub scrut_ty: Ty<'tcx>,
695+
#[label]
696+
pub uncovered: Span,
697+
pub count: usize,
698+
pub witness_1: Pat<'tcx>,
699+
pub witness_2: Pat<'tcx>,
700+
pub witness_3: Pat<'tcx>,
701+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl<'tcx> ConstToPat<'tcx> {
435435
_ => {
436436
if !pointee_ty.is_sized(tcx, param_env) {
437437
// `tcx.deref_mir_constant()` below will ICE with an unsized type
438-
// (except slices, which are handled in a separate arm above).
438+
// (except slices, which are handled in a separate arm above).
439439

440440
let err = UnsizedPattern { span, non_sm_ty: *pointee_ty };
441441
tcx.sess.create_err(err).emit_unless(!self.include_lint_checks);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
mod check_match;
44
mod const_to_pat;
5-
mod deconstruct_pat;
5+
pub(crate) mod deconstruct_pat;
66
mod usefulness;
77

88
pub(crate) use self::check_match::check_match;

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,8 @@
291291
292292
use self::ArmType::*;
293293
use self::Usefulness::*;
294-
295-
use super::check_match::{joined_uncovered_patterns, pattern_not_covered_label};
296294
use super::deconstruct_pat::{Constructor, DeconstructedPat, Fields, SplitWildcard};
295+
use crate::errors::NonExhaustiveOmittedPattern;
297296

298297
use rustc_data_structures::captures::Captures;
299298

@@ -754,6 +753,23 @@ fn lint_non_exhaustive_omitted_patterns<'p, 'tcx>(
754753
hir_id: HirId,
755754
witnesses: Vec<DeconstructedPat<'p, 'tcx>>,
756755
) {
756+
let witness_1 = witnesses.get(0).unwrap().to_pat(cx);
757+
758+
cx.tcx.emit_spanned_lint(
759+
NON_EXHAUSTIVE_OMITTED_PATTERNS,
760+
hir_id,
761+
sp,
762+
NonExhaustiveOmittedPattern {
763+
scrut_ty,
764+
uncovered: sp,
765+
count: witnesses.len(),
766+
// Substitute dummy values if witnesses is smaller than 3.
767+
witness_2: witnesses.get(1).map(|w| w.to_pat(cx)).unwrap_or_else(|| witness_1.clone()),
768+
witness_3: witnesses.get(2).map(|w| w.to_pat(cx)).unwrap_or_else(|| witness_1.clone()),
769+
witness_1,
770+
},
771+
);
772+
/*
757773
cx.tcx.struct_span_lint_hir(NON_EXHAUSTIVE_OMITTED_PATTERNS, hir_id, sp, "some variants are not matched explicitly", |lint| {
758774
let joined_patterns = joined_uncovered_patterns(cx, &witnesses);
759775
lint.span_label(sp, pattern_not_covered_label(&witnesses, &joined_patterns));
@@ -766,6 +782,7 @@ fn lint_non_exhaustive_omitted_patterns<'p, 'tcx>(
766782
));
767783
lint
768784
});
785+
*/
769786
}
770787

771788
/// Algorithm from <http://moscova.inria.fr/~maranget/papers/warn/index.html>.

0 commit comments

Comments
 (0)