Skip to content

Commit 39ed139

Browse files
committed
Lint non_exhaustive_omitted_patterns per column
1 parent 2d20e5f commit 39ed139

File tree

10 files changed

+279
-232
lines changed

10 files changed

+279
-232
lines changed

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3953,8 +3953,13 @@ declare_lint! {
39533953
}
39543954

39553955
declare_lint! {
3956-
/// The `non_exhaustive_omitted_patterns` lint detects when a wildcard (`_` or `..`) in a
3957-
/// pattern for a `#[non_exhaustive]` struct or enum is reachable.
3956+
/// The `non_exhaustive_omitted_patterns` lint aims to help consumers of a `#[non_exhaustive]`
3957+
/// struct or enum who want to match all of its fields/variants explicitly.
3958+
///
3959+
/// The `#[non_exhaustive]` annotation forces matches to use wildcards, so exhaustiveness
3960+
/// checking cannot be used to ensure that all fields/variants are matched explicitly. To remedy
3961+
/// this, this allow-by-default lint warns the user when a match mentions some but not all of
3962+
/// the fields/variants of a `#[non_exhaustive]` struct or enum.
39583963
///
39593964
/// ### Example
39603965
///
@@ -3968,39 +3973,42 @@ declare_lint! {
39683973
///
39693974
/// // in crate B
39703975
/// #![feature(non_exhaustive_omitted_patterns_lint)]
3976+
/// #[warn(non_exhaustive_omitted_patterns)]
39713977
/// match Bar::A {
39723978
/// Bar::A => {},
3973-
/// #[warn(non_exhaustive_omitted_patterns)]
39743979
/// _ => {},
39753980
/// }
39763981
/// ```
39773982
///
39783983
/// This will produce:
39793984
///
39803985
/// ```text
3981-
/// warning: reachable patterns not covered of non exhaustive enum
3986+
/// warning: some variants are not matched explicitly
39823987
/// --> $DIR/reachable-patterns.rs:70:9
39833988
/// |
3984-
/// LL | _ => {}
3985-
/// | ^ pattern `B` not covered
3989+
/// LL | match Bar::A {
3990+
/// | ^ pattern `Bar::B` not covered
39863991
/// |
39873992
/// note: the lint level is defined here
39883993
/// --> $DIR/reachable-patterns.rs:69:16
39893994
/// |
39903995
/// LL | #[warn(non_exhaustive_omitted_patterns)]
39913996
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3992-
/// = help: ensure that all possible cases are being handled by adding the suggested match arms
3997+
/// = help: ensure that all variants are matched explicitly by adding the suggested match arms
39933998
/// = note: the matched value is of type `Bar` and the `non_exhaustive_omitted_patterns` attribute was found
39943999
/// ```
39954000
///
4001+
/// Warning: setting this to `deny` will make upstream non-breaking changes (adding fields or
4002+
/// variants to a `#[non_exhaustive]` struct or enum) break your crate. This goes against
4003+
/// expected semver behavior.
4004+
///
39964005
/// ### Explanation
39974006
///
3998-
/// Structs and enums tagged with `#[non_exhaustive]` force the user to add a
3999-
/// (potentially redundant) wildcard when pattern-matching, to allow for future
4000-
/// addition of fields or variants. The `non_exhaustive_omitted_patterns` lint
4001-
/// detects when such a wildcard happens to actually catch some fields/variants.
4002-
/// In other words, when the match without the wildcard would not be exhaustive.
4003-
/// This lets the user be informed if new fields/variants were added.
4007+
/// Structs and enums tagged with `#[non_exhaustive]` force the user to add a (potentially
4008+
/// redundant) wildcard when pattern-matching, to allow for future addition of fields or
4009+
/// variants. The `non_exhaustive_omitted_patterns` lint detects when such a wildcard happens to
4010+
/// actually catch some fields/variants. In other words, when the match without the wildcard
4011+
/// would not be exhaustive. This lets the user be informed if new fields/variants were added.
40044012
pub NON_EXHAUSTIVE_OMITTED_PATTERNS,
40054013
Allow,
40064014
"detect when patterns of types marked `non_exhaustive` are missed",

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
277277

278278
let scrut = &self.thir[scrut];
279279
let scrut_ty = scrut.ty;
280-
let report = compute_match_usefulness(&cx, &tarms, self.lint_level, scrut_ty);
280+
let report = compute_match_usefulness(&cx, &tarms, self.lint_level, scrut_ty, scrut.span);
281281

282282
match source {
283283
// Don't report arm reachability of desugared `match $iter.into_iter() { iter => .. }`
@@ -455,7 +455,8 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
455455
let pattern = self.lower_pattern(&mut cx, pat);
456456
let pattern_ty = pattern.ty();
457457
let arm = MatchArm { pat: pattern, hir_id: self.lint_level, has_guard: false };
458-
let report = compute_match_usefulness(&cx, &[arm], self.lint_level, pattern_ty);
458+
let report =
459+
compute_match_usefulness(&cx, &[arm], self.lint_level, pattern_ty, pattern.span());
459460

460461
// Note: we ignore whether the pattern is unreachable (i.e. whether the type is empty). We
461462
// only care about exhaustiveness here.
@@ -646,7 +647,7 @@ fn is_let_irrefutable<'p, 'tcx>(
646647
pat: &'p DeconstructedPat<'p, 'tcx>,
647648
) -> bool {
648649
let arms = [MatchArm { pat, hir_id: pat_id, has_guard: false }];
649-
let report = compute_match_usefulness(&cx, &arms, pat_id, pat.ty());
650+
let report = compute_match_usefulness(&cx, &arms, pat_id, pat.ty(), pat.span());
650651

651652
// Report if the pattern is unreachable, which can only occur when the type is uninhabited.
652653
// This also reports unreachable sub-patterns though, so we can't just replace it with an

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

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -629,18 +629,11 @@ pub(super) enum Constructor<'tcx> {
629629
/// `#[doc(hidden)]` ones.
630630
Hidden,
631631
/// Fake extra constructor for constructors that are not seen in the matrix, as explained in the
632-
/// code for [`Constructor::split`]. The carried `bool` is used for the
633-
/// `non_exhaustive_omitted_patterns` lint.
634-
Missing {
635-
nonexhaustive_enum_missing_visible_variants: bool,
636-
},
632+
/// code for [`Constructor::split`].
633+
Missing,
637634
}
638635

639636
impl<'tcx> Constructor<'tcx> {
640-
pub(super) fn is_wildcard(&self) -> bool {
641-
matches!(self, Wildcard)
642-
}
643-
644637
pub(super) fn is_non_exhaustive(&self) -> bool {
645638
matches!(self, NonExhaustive)
646639
}
@@ -778,14 +771,8 @@ impl<'tcx> Constructor<'tcx> {
778771
let all_missing = split_set.present.is_empty();
779772
let report_when_all_missing =
780773
pcx.is_top_level && !IntRange::is_integral(pcx.ty);
781-
let ctor = if all_missing && !report_when_all_missing {
782-
Wildcard
783-
} else {
784-
Missing {
785-
nonexhaustive_enum_missing_visible_variants: split_set
786-
.nonexhaustive_enum_missing_visible_variants,
787-
}
788-
};
774+
let ctor =
775+
if all_missing && !report_when_all_missing { Wildcard } else { Missing };
789776
smallvec![ctor]
790777
} else {
791778
split_set.present
@@ -905,11 +892,9 @@ pub(super) enum ConstructorSet {
905892
/// either fully included in or disjoint from each constructor in the column. This avoids
906893
/// non-trivial intersections like between `0..10` and `5..15`.
907894
#[derive(Debug)]
908-
struct SplitConstructorSet<'tcx> {
909-
present: SmallVec<[Constructor<'tcx>; 1]>,
910-
missing: Vec<Constructor<'tcx>>,
911-
/// For the `non_exhaustive_omitted_patterns` lint.
912-
nonexhaustive_enum_missing_visible_variants: bool,
895+
pub(super) struct SplitConstructorSet<'tcx> {
896+
pub(super) present: SmallVec<[Constructor<'tcx>; 1]>,
897+
pub(super) missing: Vec<Constructor<'tcx>>,
913898
}
914899

915900
impl ConstructorSet {
@@ -1039,7 +1024,7 @@ impl ConstructorSet {
10391024
/// constructors to 1/ determine which constructors of the type (if any) are missing; 2/ split
10401025
/// constructors to handle non-trivial intersections e.g. on ranges or slices.
10411026
#[instrument(level = "debug", skip(self, pcx, ctors), ret)]
1042-
fn split<'a, 'tcx>(
1027+
pub(super) fn split<'a, 'tcx>(
10431028
&self,
10441029
pcx: &PatCtxt<'_, '_, 'tcx>,
10451030
ctors: impl Iterator<Item = &'a Constructor<'tcx>> + Clone,
@@ -1051,7 +1036,6 @@ impl ConstructorSet {
10511036
let mut missing = Vec::new();
10521037
// Constructors in `ctors`, except wildcards.
10531038
let mut seen = ctors.filter(|c| !(matches!(c, Opaque | Wildcard)));
1054-
let mut nonexhaustive_enum_missing_visible_variants = false;
10551039
match self {
10561040
ConstructorSet::Single => {
10571041
if seen.next().is_none() {
@@ -1063,6 +1047,7 @@ impl ConstructorSet {
10631047
ConstructorSet::Variants { visible_variants, hidden_variants, non_exhaustive } => {
10641048
let seen_set: FxHashSet<_> = seen.map(|c| c.as_variant().unwrap()).collect();
10651049
let mut skipped_a_hidden_variant = false;
1050+
10661051
for variant in visible_variants {
10671052
let ctor = Variant(*variant);
10681053
if seen_set.contains(&variant) {
@@ -1071,8 +1056,6 @@ impl ConstructorSet {
10711056
missing.push(ctor);
10721057
}
10731058
}
1074-
nonexhaustive_enum_missing_visible_variants =
1075-
*non_exhaustive && !missing.is_empty();
10761059

10771060
for variant in hidden_variants {
10781061
let ctor = Variant(*variant);
@@ -1159,7 +1142,7 @@ impl ConstructorSet {
11591142
ConstructorSet::Uninhabited => {}
11601143
}
11611144

1162-
SplitConstructorSet { present, missing, nonexhaustive_enum_missing_visible_variants }
1145+
SplitConstructorSet { present, missing }
11631146
}
11641147

11651148
/// Compute the set of constructors missing from this column.
@@ -1523,6 +1506,13 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
15231506
pub(super) fn is_or_pat(&self) -> bool {
15241507
matches!(self.ctor, Or)
15251508
}
1509+
pub(super) fn flatten_or_pat(&'p self) -> SmallVec<[&'p Self; 1]> {
1510+
if self.is_or_pat() {
1511+
self.iter_fields().flat_map(|p| p.flatten_or_pat()).collect()
1512+
} else {
1513+
smallvec![self]
1514+
}
1515+
}
15261516

15271517
pub(super) fn ctor(&self) -> &Constructor<'tcx> {
15281518
&self.ctor
@@ -1708,7 +1698,7 @@ impl<'p, 'tcx> fmt::Debug for DeconstructedPat<'p, 'tcx> {
17081698
#[derive(Debug, Clone)]
17091699
pub(crate) struct WitnessPat<'tcx> {
17101700
ctor: Constructor<'tcx>,
1711-
fields: Vec<WitnessPat<'tcx>>,
1701+
pub(crate) fields: Vec<WitnessPat<'tcx>>,
17121702
ty: Ty<'tcx>,
17131703
}
17141704

0 commit comments

Comments
 (0)