2023-12-11 09:40:31 +00:00
|
|
|
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
|
2023-12-28 21:22:20 +00:00
|
|
|
use rustc_span::ErrorGuaranteed;
|
2023-12-11 09:40:31 +00:00
|
|
|
|
2024-01-15 18:17:28 +00:00
|
|
|
use crate::errors::{NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered};
|
2024-01-07 10:03:40 +00:00
|
|
|
use crate::pat::PatOrWild;
|
2023-12-11 19:15:52 +00:00
|
|
|
use crate::rustc::{
|
2024-01-15 18:17:28 +00:00
|
|
|
Constructor, DeconstructedPat, MatchArm, MatchCtxt, PlaceCtxt, RevealedTy, RustcMatchCheckCtxt,
|
|
|
|
SplitConstructorSet, WitnessPat,
|
2023-12-11 19:15:52 +00:00
|
|
|
};
|
2023-12-11 09:40:31 +00:00
|
|
|
|
|
|
|
/// A column of patterns in the matrix, where a column is the intuitive notion of "subpatterns that
|
|
|
|
/// inspect the same subvalue/place".
|
2023-12-11 11:53:01 +00:00
|
|
|
/// This is used to traverse patterns column-by-column for lints. Despite similarities with the
|
|
|
|
/// algorithm in [`crate::usefulness`], this does a different traversal. Notably this is linear in
|
|
|
|
/// the depth of patterns, whereas `compute_exhaustiveness_and_usefulness` is worst-case exponential
|
|
|
|
/// (exhaustiveness is NP-complete). The core difference is that we treat sub-columns separately.
|
2023-12-11 09:40:31 +00:00
|
|
|
///
|
2024-01-09 15:31:04 +00:00
|
|
|
/// This must not contain an or-pattern. `expand_and_push` takes care to expand them.
|
2023-12-11 09:40:31 +00:00
|
|
|
///
|
2024-01-09 15:31:04 +00:00
|
|
|
/// This is not used in the usefulness algorithm; only in lints.
|
2023-12-11 09:40:31 +00:00
|
|
|
#[derive(Debug)]
|
2023-12-26 01:59:18 +00:00
|
|
|
pub(crate) struct PatternColumn<'p, 'tcx> {
|
|
|
|
patterns: Vec<&'p DeconstructedPat<'p, 'tcx>>,
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 01:59:18 +00:00
|
|
|
impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
|
2023-12-11 09:40:31 +00:00
|
|
|
pub(crate) fn new(arms: &[MatchArm<'p, 'tcx>]) -> Self {
|
2024-01-06 20:30:08 +00:00
|
|
|
let patterns = Vec::with_capacity(arms.len());
|
|
|
|
let mut column = PatternColumn { patterns };
|
2023-12-11 09:40:31 +00:00
|
|
|
for arm in arms {
|
2024-01-07 10:03:40 +00:00
|
|
|
column.expand_and_push(PatOrWild::Pat(arm.pat));
|
2024-01-06 20:30:08 +00:00
|
|
|
}
|
|
|
|
column
|
|
|
|
}
|
2024-01-09 15:31:04 +00:00
|
|
|
/// Pushes a pattern onto the column, expanding any or-patterns into its subpatterns.
|
|
|
|
/// Internal method, prefer [`PatternColumn::new`].
|
2024-01-07 10:03:40 +00:00
|
|
|
fn expand_and_push(&mut self, pat: PatOrWild<'p, RustcMatchCheckCtxt<'p, 'tcx>>) {
|
2024-01-09 15:31:04 +00:00
|
|
|
// We flatten or-patterns and skip algorithm-generated wildcards.
|
2024-01-06 20:30:08 +00:00
|
|
|
if pat.is_or_pat() {
|
2024-01-07 10:03:40 +00:00
|
|
|
self.patterns.extend(
|
|
|
|
pat.flatten_or_pat().into_iter().filter_map(|pat_or_wild| pat_or_wild.as_pat()),
|
|
|
|
)
|
|
|
|
} else if let Some(pat) = pat.as_pat() {
|
2024-01-06 20:30:08 +00:00
|
|
|
self.patterns.push(pat)
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 12:21:35 +00:00
|
|
|
fn head_ty(&self) -> Option<RevealedTy<'tcx>> {
|
|
|
|
self.patterns.first().map(|pat| pat.ty())
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Do constructor splitting on the constructors of the column.
|
2024-01-01 22:54:20 +00:00
|
|
|
fn analyze_ctors(
|
|
|
|
&self,
|
|
|
|
pcx: &PlaceCtxt<'_, 'p, 'tcx>,
|
|
|
|
) -> Result<SplitConstructorSet<'p, 'tcx>, ErrorGuaranteed> {
|
2023-12-11 09:40:31 +00:00
|
|
|
let column_ctors = self.patterns.iter().map(|p| p.ctor());
|
2024-01-01 22:54:20 +00:00
|
|
|
let ctors_for_ty = &pcx.ctors_for_ty()?;
|
2024-01-10 21:23:21 +00:00
|
|
|
Ok(ctors_for_ty.split(column_ctors))
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Does specialization: given a constructor, this takes the patterns from the column that match
|
|
|
|
/// the constructor, and outputs their fields.
|
|
|
|
/// This returns one column per field of the constructor. They usually all have the same length
|
|
|
|
/// (the number of patterns in `self` that matched `ctor`), except that we expand or-patterns
|
|
|
|
/// which may change the lengths.
|
2023-12-14 16:58:33 +00:00
|
|
|
fn specialize(
|
2023-12-11 16:57:53 +00:00
|
|
|
&self,
|
2023-12-26 01:59:18 +00:00
|
|
|
pcx: &PlaceCtxt<'_, 'p, 'tcx>,
|
2023-12-11 19:01:02 +00:00
|
|
|
ctor: &Constructor<'p, 'tcx>,
|
2023-12-26 01:59:18 +00:00
|
|
|
) -> Vec<PatternColumn<'p, 'tcx>> {
|
2023-12-11 09:40:31 +00:00
|
|
|
let arity = ctor.arity(pcx);
|
|
|
|
if arity == 0 {
|
|
|
|
return Vec::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We specialize the column by `ctor`. This gives us `arity`-many columns of patterns. These
|
|
|
|
// columns may have different lengths in the presence of or-patterns (this is why we can't
|
|
|
|
// reuse `Matrix`).
|
|
|
|
let mut specialized_columns: Vec<_> =
|
|
|
|
(0..arity).map(|_| Self { patterns: Vec::new() }).collect();
|
|
|
|
let relevant_patterns =
|
|
|
|
self.patterns.iter().filter(|pat| ctor.is_covered_by(pcx, pat.ctor()));
|
|
|
|
for pat in relevant_patterns {
|
2024-01-03 00:25:32 +00:00
|
|
|
let specialized = pat.specialize(ctor, arity);
|
2024-01-07 10:03:40 +00:00
|
|
|
for (subpat, column) in specialized.into_iter().zip(&mut specialized_columns) {
|
2024-01-06 20:30:08 +00:00
|
|
|
column.expand_and_push(subpat);
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
specialized_columns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned
|
|
|
|
/// in a given column.
|
2023-12-15 15:53:29 +00:00
|
|
|
#[instrument(level = "debug", skip(cx), ret)]
|
2023-12-11 16:57:53 +00:00
|
|
|
fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
|
2023-12-15 15:53:29 +00:00
|
|
|
cx: MatchCtxt<'a, 'p, 'tcx>,
|
2023-12-26 01:59:18 +00:00
|
|
|
column: &PatternColumn<'p, 'tcx>,
|
2024-01-07 20:20:16 +00:00
|
|
|
) -> Result<Vec<WitnessPat<'p, 'tcx>>, ErrorGuaranteed> {
|
2023-12-26 12:21:35 +00:00
|
|
|
let Some(ty) = column.head_ty() else {
|
2024-01-07 20:20:16 +00:00
|
|
|
return Ok(Vec::new());
|
2023-12-11 09:40:31 +00:00
|
|
|
};
|
2023-12-15 15:53:29 +00:00
|
|
|
let pcx = &PlaceCtxt::new_dummy(cx, ty);
|
2023-12-11 09:40:31 +00:00
|
|
|
|
2024-01-01 22:54:20 +00:00
|
|
|
let set = column.analyze_ctors(pcx)?;
|
2023-12-11 09:40:31 +00:00
|
|
|
if set.present.is_empty() {
|
|
|
|
// We can't consistently handle the case where no constructors are present (since this would
|
|
|
|
// require digging deep through any type in case there's a non_exhaustive enum somewhere),
|
|
|
|
// so for consistency we refuse to handle the top-level case, where we could handle it.
|
2024-01-07 20:20:16 +00:00
|
|
|
return Ok(Vec::new());
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut witnesses = Vec::new();
|
2023-12-15 15:53:29 +00:00
|
|
|
if cx.tycx.is_foreign_non_exhaustive_enum(ty) {
|
2023-12-11 09:40:31 +00:00
|
|
|
witnesses.extend(
|
|
|
|
set.missing
|
|
|
|
.into_iter()
|
|
|
|
// This will list missing visible variants.
|
|
|
|
.filter(|c| !matches!(c, Constructor::Hidden | Constructor::NonExhaustive))
|
|
|
|
.map(|missing_ctor| WitnessPat::wild_from_ctor(pcx, missing_ctor)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse into the fields.
|
|
|
|
for ctor in set.present {
|
|
|
|
let specialized_columns = column.specialize(pcx, &ctor);
|
|
|
|
let wild_pat = WitnessPat::wild_from_ctor(pcx, ctor);
|
|
|
|
for (i, col_i) in specialized_columns.iter().enumerate() {
|
|
|
|
// Compute witnesses for each column.
|
2024-01-07 20:20:16 +00:00
|
|
|
let wits_for_col_i = collect_nonexhaustive_missing_variants(cx, col_i)?;
|
2023-12-11 09:40:31 +00:00
|
|
|
// For each witness, we build a new pattern in the shape of `ctor(_, _, wit, _, _)`,
|
|
|
|
// adding enough wildcards to match `arity`.
|
|
|
|
for wit in wits_for_col_i {
|
|
|
|
let mut pat = wild_pat.clone();
|
|
|
|
pat.fields[i] = wit;
|
|
|
|
witnesses.push(pat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-07 20:20:16 +00:00
|
|
|
Ok(witnesses)
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 16:57:53 +00:00
|
|
|
pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
|
2023-12-15 15:53:29 +00:00
|
|
|
cx: MatchCtxt<'a, 'p, 'tcx>,
|
2023-12-11 09:40:31 +00:00
|
|
|
arms: &[MatchArm<'p, 'tcx>],
|
2023-12-26 01:59:18 +00:00
|
|
|
pat_column: &PatternColumn<'p, 'tcx>,
|
2023-12-26 12:21:35 +00:00
|
|
|
scrut_ty: RevealedTy<'tcx>,
|
2024-01-07 20:20:16 +00:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2023-12-15 15:53:29 +00:00
|
|
|
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
|
2023-12-11 09:40:31 +00:00
|
|
|
if !matches!(
|
2023-12-15 15:53:29 +00:00
|
|
|
rcx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, rcx.match_lint_level).0,
|
2023-12-11 09:40:31 +00:00
|
|
|
rustc_session::lint::Level::Allow
|
|
|
|
) {
|
2024-01-07 20:20:16 +00:00
|
|
|
let witnesses = collect_nonexhaustive_missing_variants(cx, pat_column)?;
|
2023-12-11 09:40:31 +00:00
|
|
|
if !witnesses.is_empty() {
|
|
|
|
// Report that a match of a `non_exhaustive` enum marked with `non_exhaustive_omitted_patterns`
|
|
|
|
// is not exhaustive enough.
|
|
|
|
//
|
|
|
|
// NB: The partner lint for structs lives in `compiler/rustc_hir_analysis/src/check/pat.rs`.
|
2023-12-15 15:53:29 +00:00
|
|
|
rcx.tcx.emit_spanned_lint(
|
2023-12-11 09:40:31 +00:00
|
|
|
NON_EXHAUSTIVE_OMITTED_PATTERNS,
|
2023-12-15 15:53:29 +00:00
|
|
|
rcx.match_lint_level,
|
|
|
|
rcx.scrut_span,
|
2023-12-11 09:40:31 +00:00
|
|
|
NonExhaustiveOmittedPattern {
|
2023-12-26 12:21:35 +00:00
|
|
|
scrut_ty: scrut_ty.inner(),
|
2023-12-15 15:53:29 +00:00
|
|
|
uncovered: Uncovered::new(rcx.scrut_span, rcx, witnesses),
|
2023-12-11 09:40:31 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We used to allow putting the `#[allow(non_exhaustive_omitted_patterns)]` on a match
|
|
|
|
// arm. This no longer makes sense so we warn users, to avoid silently breaking their
|
|
|
|
// usage of the lint.
|
|
|
|
for arm in arms {
|
|
|
|
let (lint_level, lint_level_source) =
|
2023-12-15 15:53:29 +00:00
|
|
|
rcx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, arm.arm_data);
|
2023-12-11 09:40:31 +00:00
|
|
|
if !matches!(lint_level, rustc_session::lint::Level::Allow) {
|
|
|
|
let decorator = NonExhaustiveOmittedPatternLintOnArm {
|
|
|
|
lint_span: lint_level_source.span(),
|
2023-12-15 15:53:29 +00:00
|
|
|
suggest_lint_on_match: rcx.whole_match_span.map(|span| span.shrink_to_lo()),
|
2023-12-11 09:40:31 +00:00
|
|
|
lint_level: lint_level.as_str(),
|
|
|
|
lint_name: "non_exhaustive_omitted_patterns",
|
|
|
|
};
|
|
|
|
|
|
|
|
use rustc_errors::DecorateLint;
|
2023-12-22 22:25:12 +00:00
|
|
|
let mut err = rcx.tcx.dcx().struct_span_warn(arm.pat.data().unwrap().span, "");
|
2023-12-23 22:08:41 +00:00
|
|
|
err.primary_message(decorator.msg());
|
2023-12-11 09:40:31 +00:00
|
|
|
decorator.decorate_lint(&mut err);
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-07 20:20:16 +00:00
|
|
|
Ok(())
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|