Move lints to their own module
This commit is contained in:
parent
3691a0aee5
commit
24adca0a26
@ -2,9 +2,8 @@ use rustc_pattern_analysis::constructor::Constructor;
|
|||||||
use rustc_pattern_analysis::cx::MatchCheckCtxt;
|
use rustc_pattern_analysis::cx::MatchCheckCtxt;
|
||||||
use rustc_pattern_analysis::errors::Uncovered;
|
use rustc_pattern_analysis::errors::Uncovered;
|
||||||
use rustc_pattern_analysis::pat::{DeconstructedPat, WitnessPat};
|
use rustc_pattern_analysis::pat::{DeconstructedPat, WitnessPat};
|
||||||
use rustc_pattern_analysis::usefulness::{
|
use rustc_pattern_analysis::usefulness::{Usefulness, UsefulnessReport};
|
||||||
compute_match_usefulness, MatchArm, Usefulness, UsefulnessReport,
|
use rustc_pattern_analysis::{analyze_match, MatchArm};
|
||||||
};
|
|
||||||
|
|
||||||
use crate::errors::*;
|
use crate::errors::*;
|
||||||
|
|
||||||
@ -436,7 +435,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let scrut_ty = scrut.ty;
|
let scrut_ty = scrut.ty;
|
||||||
let report = compute_match_usefulness(&cx, &tarms, scrut_ty);
|
let report = analyze_match(&cx, &tarms, scrut_ty);
|
||||||
|
|
||||||
match source {
|
match source {
|
||||||
// Don't report arm reachability of desugared `match $iter.into_iter() { iter => .. }`
|
// Don't report arm reachability of desugared `match $iter.into_iter() { iter => .. }`
|
||||||
@ -550,7 +549,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
|
|||||||
let cx = self.new_cx(refutability, None, scrut, pat.span);
|
let cx = self.new_cx(refutability, None, scrut, pat.span);
|
||||||
let pat = self.lower_pattern(&cx, pat)?;
|
let pat = self.lower_pattern(&cx, pat)?;
|
||||||
let arms = [MatchArm { pat, hir_id: self.lint_level, has_guard: false }];
|
let arms = [MatchArm { pat, hir_id: self.lint_level, has_guard: false }];
|
||||||
let report = compute_match_usefulness(&cx, &arms, pat.ty());
|
let report = analyze_match(&cx, &arms, pat.ty());
|
||||||
Ok((cx, report))
|
Ok((cx, report))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
pub mod constructor;
|
pub mod constructor;
|
||||||
pub mod cx;
|
pub mod cx;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
|
pub(crate) mod lints;
|
||||||
pub mod pat;
|
pub mod pat;
|
||||||
pub mod usefulness;
|
pub mod usefulness;
|
||||||
|
|
||||||
@ -12,3 +13,44 @@ extern crate tracing;
|
|||||||
extern crate rustc_middle;
|
extern crate rustc_middle;
|
||||||
|
|
||||||
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
|
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
|
||||||
|
|
||||||
|
use lints::PatternColumn;
|
||||||
|
use rustc_hir::HirId;
|
||||||
|
use rustc_middle::ty::Ty;
|
||||||
|
use usefulness::{compute_match_usefulness, UsefulnessReport};
|
||||||
|
|
||||||
|
use crate::cx::MatchCheckCtxt;
|
||||||
|
use crate::lints::{lint_nonexhaustive_missing_variants, lint_overlapping_range_endpoints};
|
||||||
|
use crate::pat::DeconstructedPat;
|
||||||
|
|
||||||
|
/// The arm of a match expression.
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct MatchArm<'p, 'tcx> {
|
||||||
|
/// The pattern must have been lowered through `check_match::MatchVisitor::lower_pattern`.
|
||||||
|
pub pat: &'p DeconstructedPat<'p, 'tcx>,
|
||||||
|
pub hir_id: HirId,
|
||||||
|
pub has_guard: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are
|
||||||
|
/// useful, and runs some lints.
|
||||||
|
pub fn analyze_match<'p, 'tcx>(
|
||||||
|
cx: &MatchCheckCtxt<'p, 'tcx>,
|
||||||
|
arms: &[MatchArm<'p, 'tcx>],
|
||||||
|
scrut_ty: Ty<'tcx>,
|
||||||
|
) -> UsefulnessReport<'p, 'tcx> {
|
||||||
|
let pat_column = PatternColumn::new(arms);
|
||||||
|
|
||||||
|
let report = compute_match_usefulness(cx, arms, scrut_ty);
|
||||||
|
|
||||||
|
// Lint on ranges that overlap on their endpoints, which is likely a mistake.
|
||||||
|
lint_overlapping_range_endpoints(cx, &pat_column);
|
||||||
|
|
||||||
|
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
|
||||||
|
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.
|
||||||
|
if cx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
|
||||||
|
lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty)
|
||||||
|
}
|
||||||
|
|
||||||
|
report
|
||||||
|
}
|
||||||
|
291
compiler/rustc_pattern_analysis/src/lints.rs
Normal file
291
compiler/rustc_pattern_analysis/src/lints.rs
Normal file
@ -0,0 +1,291 @@
|
|||||||
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
|
use rustc_data_structures::captures::Captures;
|
||||||
|
use rustc_middle::ty::{self, Ty};
|
||||||
|
use rustc_session::lint;
|
||||||
|
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
|
||||||
|
use rustc_span::Span;
|
||||||
|
|
||||||
|
use crate::constructor::{Constructor, IntRange, MaybeInfiniteInt, SplitConstructorSet};
|
||||||
|
use crate::cx::MatchCheckCtxt;
|
||||||
|
use crate::errors::{
|
||||||
|
NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Overlap,
|
||||||
|
OverlappingRangeEndpoints, Uncovered,
|
||||||
|
};
|
||||||
|
use crate::pat::{DeconstructedPat, WitnessPat};
|
||||||
|
use crate::usefulness::PatCtxt;
|
||||||
|
use crate::MatchArm;
|
||||||
|
|
||||||
|
/// A column of patterns in the matrix, where a column is the intuitive notion of "subpatterns that
|
||||||
|
/// inspect the same subvalue/place".
|
||||||
|
/// This is used to traverse patterns column-by-column for lints. Despite similarities with
|
||||||
|
/// [`compute_exhaustiveness_and_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.
|
||||||
|
///
|
||||||
|
/// This must not contain an or-pattern. `specialize` takes care to expand them.
|
||||||
|
///
|
||||||
|
/// This is not used in the main algorithm; only in lints.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct PatternColumn<'p, 'tcx> {
|
||||||
|
patterns: Vec<&'p DeconstructedPat<'p, 'tcx>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
|
||||||
|
pub(crate) fn new(arms: &[MatchArm<'p, 'tcx>]) -> Self {
|
||||||
|
let mut patterns = Vec::with_capacity(arms.len());
|
||||||
|
for arm in arms {
|
||||||
|
if arm.pat.is_or_pat() {
|
||||||
|
patterns.extend(arm.pat.flatten_or_pat())
|
||||||
|
} else {
|
||||||
|
patterns.push(arm.pat)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self { patterns }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_empty(&self) -> bool {
|
||||||
|
self.patterns.is_empty()
|
||||||
|
}
|
||||||
|
fn head_ty(&self) -> Option<Ty<'tcx>> {
|
||||||
|
if self.patterns.len() == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
// If the type is opaque and it is revealed anywhere in the column, we take the revealed
|
||||||
|
// version. Otherwise we could encounter constructors for the revealed type and crash.
|
||||||
|
let is_opaque = |ty: Ty<'tcx>| matches!(ty.kind(), ty::Alias(ty::Opaque, ..));
|
||||||
|
let first_ty = self.patterns[0].ty();
|
||||||
|
if is_opaque(first_ty) {
|
||||||
|
for pat in &self.patterns {
|
||||||
|
let ty = pat.ty();
|
||||||
|
if !is_opaque(ty) {
|
||||||
|
return Some(ty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(first_ty)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Do constructor splitting on the constructors of the column.
|
||||||
|
fn analyze_ctors(&self, pcx: &PatCtxt<'_, 'p, 'tcx>) -> SplitConstructorSet<'tcx> {
|
||||||
|
let column_ctors = self.patterns.iter().map(|p| p.ctor());
|
||||||
|
pcx.cx.ctors_for_ty(pcx.ty).split(pcx, column_ctors)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iter<'a>(&'a self) -> impl Iterator<Item = &'p DeconstructedPat<'p, 'tcx>> + Captures<'a> {
|
||||||
|
self.patterns.iter().copied()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
fn specialize(&self, pcx: &PatCtxt<'_, 'p, 'tcx>, ctor: &Constructor<'tcx>) -> Vec<Self> {
|
||||||
|
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 {
|
||||||
|
let specialized = pat.specialize(pcx, ctor);
|
||||||
|
for (subpat, column) in specialized.iter().zip(&mut specialized_columns) {
|
||||||
|
if subpat.is_or_pat() {
|
||||||
|
column.patterns.extend(subpat.flatten_or_pat())
|
||||||
|
} else {
|
||||||
|
column.patterns.push(subpat)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
!specialized_columns[0].is_empty(),
|
||||||
|
"ctor {ctor:?} was listed as present but isn't;
|
||||||
|
there is an inconsistency between `Constructor::is_covered_by` and `ConstructorSet::split`"
|
||||||
|
);
|
||||||
|
specialized_columns
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned
|
||||||
|
/// in a given column.
|
||||||
|
#[instrument(level = "debug", skip(cx), ret)]
|
||||||
|
fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
|
||||||
|
cx: &MatchCheckCtxt<'p, 'tcx>,
|
||||||
|
column: &PatternColumn<'p, 'tcx>,
|
||||||
|
) -> Vec<WitnessPat<'tcx>> {
|
||||||
|
let Some(ty) = column.head_ty() else {
|
||||||
|
return Vec::new();
|
||||||
|
};
|
||||||
|
let pcx = &PatCtxt::new_dummy(cx, ty);
|
||||||
|
|
||||||
|
let set = column.analyze_ctors(pcx);
|
||||||
|
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.
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut witnesses = Vec::new();
|
||||||
|
if cx.is_foreign_non_exhaustive_enum(ty) {
|
||||||
|
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.
|
||||||
|
let wits_for_col_i = collect_nonexhaustive_missing_variants(cx, col_i);
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
witnesses
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>(
|
||||||
|
cx: &MatchCheckCtxt<'p, 'tcx>,
|
||||||
|
arms: &[MatchArm<'p, 'tcx>],
|
||||||
|
pat_column: &PatternColumn<'p, 'tcx>,
|
||||||
|
scrut_ty: Ty<'tcx>,
|
||||||
|
) {
|
||||||
|
if !matches!(
|
||||||
|
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, cx.match_lint_level).0,
|
||||||
|
rustc_session::lint::Level::Allow
|
||||||
|
) {
|
||||||
|
let witnesses = collect_nonexhaustive_missing_variants(cx, pat_column);
|
||||||
|
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`.
|
||||||
|
cx.tcx.emit_spanned_lint(
|
||||||
|
NON_EXHAUSTIVE_OMITTED_PATTERNS,
|
||||||
|
cx.match_lint_level,
|
||||||
|
cx.scrut_span,
|
||||||
|
NonExhaustiveOmittedPattern {
|
||||||
|
scrut_ty,
|
||||||
|
uncovered: Uncovered::new(cx.scrut_span, cx, witnesses),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} 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) =
|
||||||
|
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, arm.hir_id);
|
||||||
|
if !matches!(lint_level, rustc_session::lint::Level::Allow) {
|
||||||
|
let decorator = NonExhaustiveOmittedPatternLintOnArm {
|
||||||
|
lint_span: lint_level_source.span(),
|
||||||
|
suggest_lint_on_match: cx.whole_match_span.map(|span| span.shrink_to_lo()),
|
||||||
|
lint_level: lint_level.as_str(),
|
||||||
|
lint_name: "non_exhaustive_omitted_patterns",
|
||||||
|
};
|
||||||
|
|
||||||
|
use rustc_errors::DecorateLint;
|
||||||
|
let mut err = cx.tcx.sess.struct_span_warn(arm.pat.span(), "");
|
||||||
|
err.set_primary_message(decorator.msg());
|
||||||
|
decorator.decorate_lint(&mut err);
|
||||||
|
err.emit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Traverse the patterns to warn the user about ranges that overlap on their endpoints.
|
||||||
|
#[instrument(level = "debug", skip(cx))]
|
||||||
|
pub(crate) fn lint_overlapping_range_endpoints<'p, 'tcx>(
|
||||||
|
cx: &MatchCheckCtxt<'p, 'tcx>,
|
||||||
|
column: &PatternColumn<'p, 'tcx>,
|
||||||
|
) {
|
||||||
|
let Some(ty) = column.head_ty() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let pcx = &PatCtxt::new_dummy(cx, ty);
|
||||||
|
|
||||||
|
let set = column.analyze_ctors(pcx);
|
||||||
|
|
||||||
|
if matches!(ty.kind(), ty::Char | ty::Int(_) | ty::Uint(_)) {
|
||||||
|
let emit_lint = |overlap: &IntRange, this_span: Span, overlapped_spans: &[Span]| {
|
||||||
|
let overlap_as_pat = cx.hoist_pat_range(overlap, ty);
|
||||||
|
let overlaps: Vec<_> = overlapped_spans
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.map(|span| Overlap { range: overlap_as_pat.clone(), span })
|
||||||
|
.collect();
|
||||||
|
cx.tcx.emit_spanned_lint(
|
||||||
|
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
|
||||||
|
cx.match_lint_level,
|
||||||
|
this_span,
|
||||||
|
OverlappingRangeEndpoints { overlap: overlaps, range: this_span },
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
// If two ranges overlapped, the split set will contain their intersection as a singleton.
|
||||||
|
let split_int_ranges = set.present.iter().filter_map(|c| c.as_int_range());
|
||||||
|
for overlap_range in split_int_ranges.clone() {
|
||||||
|
if overlap_range.is_singleton() {
|
||||||
|
let overlap: MaybeInfiniteInt = overlap_range.lo;
|
||||||
|
// Ranges that look like `lo..=overlap`.
|
||||||
|
let mut prefixes: SmallVec<[_; 1]> = Default::default();
|
||||||
|
// Ranges that look like `overlap..=hi`.
|
||||||
|
let mut suffixes: SmallVec<[_; 1]> = Default::default();
|
||||||
|
// Iterate on patterns that contained `overlap`.
|
||||||
|
for pat in column.iter() {
|
||||||
|
let this_span = pat.span();
|
||||||
|
let Constructor::IntRange(this_range) = pat.ctor() else { continue };
|
||||||
|
if this_range.is_singleton() {
|
||||||
|
// Don't lint when one of the ranges is a singleton.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if this_range.lo == overlap {
|
||||||
|
// `this_range` looks like `overlap..=this_range.hi`; it overlaps with any
|
||||||
|
// ranges that look like `lo..=overlap`.
|
||||||
|
if !prefixes.is_empty() {
|
||||||
|
emit_lint(overlap_range, this_span, &prefixes);
|
||||||
|
}
|
||||||
|
suffixes.push(this_span)
|
||||||
|
} else if this_range.hi == overlap.plus_one() {
|
||||||
|
// `this_range` looks like `this_range.lo..=overlap`; it overlaps with any
|
||||||
|
// ranges that look like `overlap..=hi`.
|
||||||
|
if !suffixes.is_empty() {
|
||||||
|
emit_lint(overlap_range, this_span, &suffixes);
|
||||||
|
}
|
||||||
|
prefixes.push(this_span)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Recurse into the fields.
|
||||||
|
for ctor in set.present {
|
||||||
|
for col in column.specialize(pcx, &ctor) {
|
||||||
|
lint_overlapping_range_endpoints(cx, &col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -555,37 +555,29 @@ use smallvec::{smallvec, SmallVec};
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use rustc_data_structures::{captures::Captures, stack::ensure_sufficient_stack};
|
use rustc_data_structures::{captures::Captures, stack::ensure_sufficient_stack};
|
||||||
use rustc_hir::HirId;
|
|
||||||
use rustc_middle::ty::{self, Ty};
|
use rustc_middle::ty::{self, Ty};
|
||||||
use rustc_session::lint;
|
|
||||||
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
|
|
||||||
use rustc_span::{Span, DUMMY_SP};
|
use rustc_span::{Span, DUMMY_SP};
|
||||||
|
|
||||||
use crate::constructor::{
|
use crate::constructor::{Constructor, ConstructorSet};
|
||||||
Constructor, ConstructorSet, IntRange, MaybeInfiniteInt, SplitConstructorSet,
|
|
||||||
};
|
|
||||||
use crate::cx::MatchCheckCtxt;
|
use crate::cx::MatchCheckCtxt;
|
||||||
use crate::errors::{
|
|
||||||
NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Overlap,
|
|
||||||
OverlappingRangeEndpoints, Uncovered,
|
|
||||||
};
|
|
||||||
use crate::pat::{DeconstructedPat, WitnessPat};
|
use crate::pat::{DeconstructedPat, WitnessPat};
|
||||||
|
use crate::MatchArm;
|
||||||
|
|
||||||
use self::ValidityConstraint::*;
|
use self::ValidityConstraint::*;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub(super) struct PatCtxt<'a, 'p, 'tcx> {
|
pub(crate) struct PatCtxt<'a, 'p, 'tcx> {
|
||||||
pub(super) cx: &'a MatchCheckCtxt<'p, 'tcx>,
|
pub(crate) cx: &'a MatchCheckCtxt<'p, 'tcx>,
|
||||||
/// Type of the current column under investigation.
|
/// Type of the current column under investigation.
|
||||||
pub(super) ty: Ty<'tcx>,
|
pub(crate) ty: Ty<'tcx>,
|
||||||
/// Whether the current pattern is the whole pattern as found in a match arm, or if it's a
|
/// Whether the current pattern is the whole pattern as found in a match arm, or if it's a
|
||||||
/// subpattern.
|
/// subpattern.
|
||||||
pub(super) is_top_level: bool,
|
pub(crate) is_top_level: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'p, 'tcx> PatCtxt<'a, 'p, 'tcx> {
|
impl<'a, 'p, 'tcx> PatCtxt<'a, 'p, 'tcx> {
|
||||||
/// A `PatCtxt` when code other than `is_useful` needs one.
|
/// A `PatCtxt` when code other than `is_useful` needs one.
|
||||||
fn new_dummy(cx: &'a MatchCheckCtxt<'p, 'tcx>, ty: Ty<'tcx>) -> Self {
|
pub(crate) fn new_dummy(cx: &'a MatchCheckCtxt<'p, 'tcx>, ty: Ty<'tcx>) -> Self {
|
||||||
PatCtxt { cx, ty, is_top_level: false }
|
PatCtxt { cx, ty, is_top_level: false }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1279,230 +1271,6 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
|
|||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A column of patterns in the matrix, where a column is the intuitive notion of "subpatterns that
|
|
||||||
/// inspect the same subvalue/place".
|
|
||||||
/// This is used to traverse patterns column-by-column for lints. Despite similarities with
|
|
||||||
/// [`compute_exhaustiveness_and_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.
|
|
||||||
///
|
|
||||||
/// This must not contain an or-pattern. `specialize` takes care to expand them.
|
|
||||||
///
|
|
||||||
/// This is not used in the main algorithm; only in lints.
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct PatternColumn<'p, 'tcx> {
|
|
||||||
patterns: Vec<&'p DeconstructedPat<'p, 'tcx>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
|
|
||||||
fn new(patterns: Vec<&'p DeconstructedPat<'p, 'tcx>>) -> Self {
|
|
||||||
Self { patterns }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_empty(&self) -> bool {
|
|
||||||
self.patterns.is_empty()
|
|
||||||
}
|
|
||||||
fn head_ty(&self) -> Option<Ty<'tcx>> {
|
|
||||||
if self.patterns.len() == 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
// If the type is opaque and it is revealed anywhere in the column, we take the revealed
|
|
||||||
// version. Otherwise we could encounter constructors for the revealed type and crash.
|
|
||||||
let is_opaque = |ty: Ty<'tcx>| matches!(ty.kind(), ty::Alias(ty::Opaque, ..));
|
|
||||||
let first_ty = self.patterns[0].ty();
|
|
||||||
if is_opaque(first_ty) {
|
|
||||||
for pat in &self.patterns {
|
|
||||||
let ty = pat.ty();
|
|
||||||
if !is_opaque(ty) {
|
|
||||||
return Some(ty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(first_ty)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Do constructor splitting on the constructors of the column.
|
|
||||||
fn analyze_ctors(&self, pcx: &PatCtxt<'_, 'p, 'tcx>) -> SplitConstructorSet<'tcx> {
|
|
||||||
let column_ctors = self.patterns.iter().map(|p| p.ctor());
|
|
||||||
pcx.cx.ctors_for_ty(pcx.ty).split(pcx, column_ctors)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iter<'a>(&'a self) -> impl Iterator<Item = &'p DeconstructedPat<'p, 'tcx>> + Captures<'a> {
|
|
||||||
self.patterns.iter().copied()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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.
|
|
||||||
fn specialize(&self, pcx: &PatCtxt<'_, 'p, 'tcx>, ctor: &Constructor<'tcx>) -> Vec<Self> {
|
|
||||||
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 {
|
|
||||||
let specialized = pat.specialize(pcx, ctor);
|
|
||||||
for (subpat, column) in specialized.iter().zip(&mut specialized_columns) {
|
|
||||||
if subpat.is_or_pat() {
|
|
||||||
column.patterns.extend(subpat.flatten_or_pat())
|
|
||||||
} else {
|
|
||||||
column.patterns.push(subpat)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
!specialized_columns[0].is_empty(),
|
|
||||||
"ctor {ctor:?} was listed as present but isn't;
|
|
||||||
there is an inconsistency between `Constructor::is_covered_by` and `ConstructorSet::split`"
|
|
||||||
);
|
|
||||||
specialized_columns
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned
|
|
||||||
/// in a given column.
|
|
||||||
#[instrument(level = "debug", skip(cx), ret)]
|
|
||||||
fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
|
|
||||||
cx: &MatchCheckCtxt<'p, 'tcx>,
|
|
||||||
column: &PatternColumn<'p, 'tcx>,
|
|
||||||
) -> Vec<WitnessPat<'tcx>> {
|
|
||||||
let Some(ty) = column.head_ty() else {
|
|
||||||
return Vec::new();
|
|
||||||
};
|
|
||||||
let pcx = &PatCtxt::new_dummy(cx, ty);
|
|
||||||
|
|
||||||
let set = column.analyze_ctors(pcx);
|
|
||||||
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.
|
|
||||||
return vec![];
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut witnesses = Vec::new();
|
|
||||||
if cx.is_foreign_non_exhaustive_enum(ty) {
|
|
||||||
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.
|
|
||||||
let wits_for_col_i = collect_nonexhaustive_missing_variants(cx, col_i);
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
witnesses
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Traverse the patterns to warn the user about ranges that overlap on their endpoints.
|
|
||||||
#[instrument(level = "debug", skip(cx))]
|
|
||||||
fn lint_overlapping_range_endpoints<'p, 'tcx>(
|
|
||||||
cx: &MatchCheckCtxt<'p, 'tcx>,
|
|
||||||
column: &PatternColumn<'p, 'tcx>,
|
|
||||||
) {
|
|
||||||
let Some(ty) = column.head_ty() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let pcx = &PatCtxt::new_dummy(cx, ty);
|
|
||||||
|
|
||||||
let set = column.analyze_ctors(pcx);
|
|
||||||
|
|
||||||
if matches!(ty.kind(), ty::Char | ty::Int(_) | ty::Uint(_)) {
|
|
||||||
let emit_lint = |overlap: &IntRange, this_span: Span, overlapped_spans: &[Span]| {
|
|
||||||
let overlap_as_pat = cx.hoist_pat_range(overlap, ty);
|
|
||||||
let overlaps: Vec<_> = overlapped_spans
|
|
||||||
.iter()
|
|
||||||
.copied()
|
|
||||||
.map(|span| Overlap { range: overlap_as_pat.clone(), span })
|
|
||||||
.collect();
|
|
||||||
cx.tcx.emit_spanned_lint(
|
|
||||||
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
|
|
||||||
cx.match_lint_level,
|
|
||||||
this_span,
|
|
||||||
OverlappingRangeEndpoints { overlap: overlaps, range: this_span },
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
// If two ranges overlapped, the split set will contain their intersection as a singleton.
|
|
||||||
let split_int_ranges = set.present.iter().filter_map(|c| c.as_int_range());
|
|
||||||
for overlap_range in split_int_ranges.clone() {
|
|
||||||
if overlap_range.is_singleton() {
|
|
||||||
let overlap: MaybeInfiniteInt = overlap_range.lo;
|
|
||||||
// Ranges that look like `lo..=overlap`.
|
|
||||||
let mut prefixes: SmallVec<[_; 1]> = Default::default();
|
|
||||||
// Ranges that look like `overlap..=hi`.
|
|
||||||
let mut suffixes: SmallVec<[_; 1]> = Default::default();
|
|
||||||
// Iterate on patterns that contained `overlap`.
|
|
||||||
for pat in column.iter() {
|
|
||||||
let this_span = pat.span();
|
|
||||||
let Constructor::IntRange(this_range) = pat.ctor() else { continue };
|
|
||||||
if this_range.is_singleton() {
|
|
||||||
// Don't lint when one of the ranges is a singleton.
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if this_range.lo == overlap {
|
|
||||||
// `this_range` looks like `overlap..=this_range.hi`; it overlaps with any
|
|
||||||
// ranges that look like `lo..=overlap`.
|
|
||||||
if !prefixes.is_empty() {
|
|
||||||
emit_lint(overlap_range, this_span, &prefixes);
|
|
||||||
}
|
|
||||||
suffixes.push(this_span)
|
|
||||||
} else if this_range.hi == overlap.plus_one() {
|
|
||||||
// `this_range` looks like `this_range.lo..=overlap`; it overlaps with any
|
|
||||||
// ranges that look like `overlap..=hi`.
|
|
||||||
if !suffixes.is_empty() {
|
|
||||||
emit_lint(overlap_range, this_span, &suffixes);
|
|
||||||
}
|
|
||||||
prefixes.push(this_span)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Recurse into the fields.
|
|
||||||
for ctor in set.present {
|
|
||||||
for col in column.specialize(pcx, &ctor) {
|
|
||||||
lint_overlapping_range_endpoints(cx, &col);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The arm of a match expression.
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
pub struct MatchArm<'p, 'tcx> {
|
|
||||||
/// The pattern must have been lowered through `check_match::MatchVisitor::lower_pattern`.
|
|
||||||
pub pat: &'p DeconstructedPat<'p, 'tcx>,
|
|
||||||
pub hir_id: HirId,
|
|
||||||
pub has_guard: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Indicates whether or not a given arm is useful.
|
/// Indicates whether or not a given arm is useful.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Usefulness {
|
pub enum Usefulness {
|
||||||
@ -1524,10 +1292,9 @@ pub struct UsefulnessReport<'p, 'tcx> {
|
|||||||
pub non_exhaustiveness_witnesses: Vec<WitnessPat<'tcx>>,
|
pub non_exhaustiveness_witnesses: Vec<WitnessPat<'tcx>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The entrypoint for this file. Computes whether a match is exhaustive and which of its arms are
|
/// Computes whether a match is exhaustive and which of its arms are useful.
|
||||||
/// useful.
|
|
||||||
#[instrument(skip(cx, arms), level = "debug")]
|
#[instrument(skip(cx, arms), level = "debug")]
|
||||||
pub fn compute_match_usefulness<'p, 'tcx>(
|
pub(crate) fn compute_match_usefulness<'p, 'tcx>(
|
||||||
cx: &MatchCheckCtxt<'p, 'tcx>,
|
cx: &MatchCheckCtxt<'p, 'tcx>,
|
||||||
arms: &[MatchArm<'p, 'tcx>],
|
arms: &[MatchArm<'p, 'tcx>],
|
||||||
scrut_ty: Ty<'tcx>,
|
scrut_ty: Ty<'tcx>,
|
||||||
@ -1551,59 +1318,5 @@ pub fn compute_match_usefulness<'p, 'tcx>(
|
|||||||
(arm, usefulness)
|
(arm, usefulness)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let report = UsefulnessReport { arm_usefulness, non_exhaustiveness_witnesses };
|
UsefulnessReport { arm_usefulness, non_exhaustiveness_witnesses }
|
||||||
|
|
||||||
let pat_column = PatternColumn::new(matrix.heads().collect());
|
|
||||||
// Lint on ranges that overlap on their endpoints, which is likely a mistake.
|
|
||||||
lint_overlapping_range_endpoints(cx, &pat_column);
|
|
||||||
|
|
||||||
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
|
|
||||||
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.
|
|
||||||
if cx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
|
|
||||||
if !matches!(
|
|
||||||
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, cx.match_lint_level).0,
|
|
||||||
rustc_session::lint::Level::Allow
|
|
||||||
) {
|
|
||||||
let witnesses = collect_nonexhaustive_missing_variants(cx, &pat_column);
|
|
||||||
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`.
|
|
||||||
cx.tcx.emit_spanned_lint(
|
|
||||||
NON_EXHAUSTIVE_OMITTED_PATTERNS,
|
|
||||||
cx.match_lint_level,
|
|
||||||
cx.scrut_span,
|
|
||||||
NonExhaustiveOmittedPattern {
|
|
||||||
scrut_ty,
|
|
||||||
uncovered: Uncovered::new(cx.scrut_span, cx, witnesses),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} 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) =
|
|
||||||
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, arm.hir_id);
|
|
||||||
if !matches!(lint_level, rustc_session::lint::Level::Allow) {
|
|
||||||
let decorator = NonExhaustiveOmittedPatternLintOnArm {
|
|
||||||
lint_span: lint_level_source.span(),
|
|
||||||
suggest_lint_on_match: cx.whole_match_span.map(|span| span.shrink_to_lo()),
|
|
||||||
lint_level: lint_level.as_str(),
|
|
||||||
lint_name: "non_exhaustive_omitted_patterns",
|
|
||||||
};
|
|
||||||
|
|
||||||
use rustc_errors::DecorateLint;
|
|
||||||
let mut err = cx.tcx.sess.struct_span_warn(arm.pat.span(), "");
|
|
||||||
err.set_primary_message(decorator.msg());
|
|
||||||
decorator.decorate_lint(&mut err);
|
|
||||||
err.emit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
report
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user