1
Fork 0

Disentangle the arena from MatchCheckCtxt

This commit is contained in:
Nadrieril 2023-12-11 17:57:53 +01:00
parent 081c3dcf43
commit 3ad76f9325
6 changed files with 140 additions and 117 deletions

View file

@ -7,7 +7,7 @@ use rustc_pattern_analysis::{analyze_match, MatchArm};
use crate::errors::*; use crate::errors::*;
use rustc_arena::TypedArena; use rustc_arena::{DroplessArena, TypedArena};
use rustc_ast::Mutability; use rustc_ast::Mutability;
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::stack::ensure_sufficient_stack;
@ -31,6 +31,7 @@ pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), Err
let (thir, expr) = tcx.thir_body(def_id)?; let (thir, expr) = tcx.thir_body(def_id)?;
let thir = thir.borrow(); let thir = thir.borrow();
let pattern_arena = TypedArena::default(); let pattern_arena = TypedArena::default();
let dropless_arena = DroplessArena::default();
let mut visitor = MatchVisitor { let mut visitor = MatchVisitor {
tcx, tcx,
thir: &*thir, thir: &*thir,
@ -38,6 +39,7 @@ pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), Err
lint_level: tcx.local_def_id_to_hir_id(def_id), lint_level: tcx.local_def_id_to_hir_id(def_id),
let_source: LetSource::None, let_source: LetSource::None,
pattern_arena: &pattern_arena, pattern_arena: &pattern_arena,
dropless_arena: &dropless_arena,
error: Ok(()), error: Ok(()),
}; };
visitor.visit_expr(&thir[expr]); visitor.visit_expr(&thir[expr]);
@ -82,6 +84,7 @@ struct MatchVisitor<'thir, 'p, 'tcx> {
lint_level: HirId, lint_level: HirId,
let_source: LetSource, let_source: LetSource,
pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>, pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>,
dropless_arena: &'p DroplessArena,
/// Tracks if we encountered an error while checking this body. That the first function to /// Tracks if we encountered an error while checking this body. That the first function to
/// report it stores it here. Some functions return `Result` to allow callers to short-circuit /// report it stores it here. Some functions return `Result` to allow callers to short-circuit
/// on error, but callers don't need to store it here again. /// on error, but callers don't need to store it here again.
@ -382,6 +385,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
param_env: self.param_env, param_env: self.param_env,
module: self.tcx.parent_module(self.lint_level).to_def_id(), module: self.tcx.parent_module(self.lint_level).to_def_id(),
pattern_arena: self.pattern_arena, pattern_arena: self.pattern_arena,
dropless_arena: self.dropless_arena,
match_lint_level: self.lint_level, match_lint_level: self.lint_level,
whole_match_span, whole_match_span,
scrut_span, scrut_span,

View file

@ -1,15 +1,15 @@
use std::fmt; use std::fmt;
use std::iter::once; use std::iter::once;
use rustc_arena::TypedArena; use rustc_arena::{DroplessArena, TypedArena};
use rustc_data_structures::captures::Captures; use rustc_data_structures::captures::Captures;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::{HirId, RangeEnd}; use rustc_hir::{HirId, RangeEnd};
use rustc_index::Idx; use rustc_index::Idx;
use rustc_index::IndexVec; use rustc_index::IndexVec;
use rustc_middle::middle::stability::EvalResult; use rustc_middle::middle::stability::EvalResult;
use rustc_middle::mir;
use rustc_middle::mir::interpret::Scalar; use rustc_middle::mir::interpret::Scalar;
use rustc_middle::mir::{self};
use rustc_middle::thir::{FieldPat, Pat, PatKind, PatRange, PatRangeBoundary}; use rustc_middle::thir::{FieldPat, Pat, PatKind, PatRange, PatRangeBoundary};
use rustc_middle::ty::layout::IntegerExt; use rustc_middle::ty::layout::IntegerExt;
use rustc_middle::ty::{self, Ty, TyCtxt, VariantDef}; use rustc_middle::ty::{self, Ty, TyCtxt, VariantDef};
@ -35,6 +35,7 @@ pub struct MatchCheckCtxt<'p, 'tcx> {
pub module: DefId, pub module: DefId,
pub param_env: ty::ParamEnv<'tcx>, pub param_env: ty::ParamEnv<'tcx>,
pub pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>, pub pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>,
pub dropless_arena: &'p DroplessArena,
/// Lint level at the match. /// Lint level at the match.
pub match_lint_level: HirId, pub match_lint_level: HirId,
/// The span of the whole match, if applicable. /// The span of the whole match, if applicable.
@ -67,14 +68,6 @@ impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
} }
} }
pub(crate) fn alloc_wildcard_slice(
&self,
tys: impl IntoIterator<Item = Ty<'tcx>>,
) -> &'p [DeconstructedPat<'p, 'tcx>] {
self.pattern_arena
.alloc_from_iter(tys.into_iter().map(|ty| DeconstructedPat::wildcard(ty, DUMMY_SP)))
}
// In the cases of either a `#[non_exhaustive]` field list or a non-public field, we hide // In the cases of either a `#[non_exhaustive]` field list or a non-public field, we hide
// uninhabited fields in order not to reveal the uninhabitedness of the whole variant. // uninhabited fields in order not to reveal the uninhabitedness of the whole variant.
// This lists the fields we keep along with their types. // This lists the fields we keep along with their types.
@ -117,40 +110,36 @@ impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
} }
} }
/// Creates a new list of wildcard fields for a given constructor. The result must have a length /// Returns the types of the fields for a given constructor. The result must have a length of
/// of `ctor.arity()`. /// `ctor.arity()`.
#[instrument(level = "trace", skip(self))] #[instrument(level = "trace", skip(self))]
pub(crate) fn ctor_wildcard_fields( pub(crate) fn ctor_sub_tys(&self, ctor: &Constructor<'tcx>, ty: Ty<'tcx>) -> &[Ty<'tcx>] {
&self,
ctor: &Constructor<'tcx>,
ty: Ty<'tcx>,
) -> &'p [DeconstructedPat<'p, 'tcx>] {
let cx = self; let cx = self;
match ctor { match ctor {
Struct | Variant(_) | UnionField => match ty.kind() { Struct | Variant(_) | UnionField => match ty.kind() {
ty::Tuple(fs) => cx.alloc_wildcard_slice(fs.iter()), ty::Tuple(fs) => cx.dropless_arena.alloc_from_iter(fs.iter()),
ty::Adt(adt, args) => { ty::Adt(adt, args) => {
if adt.is_box() { if adt.is_box() {
// The only legal patterns of type `Box` (outside `std`) are `_` and box // The only legal patterns of type `Box` (outside `std`) are `_` and box
// patterns. If we're here we can assume this is a box pattern. // patterns. If we're here we can assume this is a box pattern.
cx.alloc_wildcard_slice(once(args.type_at(0))) cx.dropless_arena.alloc_from_iter(once(args.type_at(0)))
} else { } else {
let variant = let variant =
&adt.variant(MatchCheckCtxt::variant_index_for_adt(&ctor, *adt)); &adt.variant(MatchCheckCtxt::variant_index_for_adt(&ctor, *adt));
let tys = cx.list_variant_nonhidden_fields(ty, variant).map(|(_, ty)| ty); let tys = cx.list_variant_nonhidden_fields(ty, variant).map(|(_, ty)| ty);
cx.alloc_wildcard_slice(tys) cx.dropless_arena.alloc_from_iter(tys)
} }
} }
_ => bug!("Unexpected type for constructor `{ctor:?}`: {ty:?}"), _ => bug!("Unexpected type for constructor `{ctor:?}`: {ty:?}"),
}, },
Ref => match ty.kind() { Ref => match ty.kind() {
ty::Ref(_, rty, _) => cx.alloc_wildcard_slice(once(*rty)), ty::Ref(_, rty, _) => cx.dropless_arena.alloc_from_iter(once(*rty)),
_ => bug!("Unexpected type for `Ref` constructor: {ty:?}"), _ => bug!("Unexpected type for `Ref` constructor: {ty:?}"),
}, },
Slice(slice) => match *ty.kind() { Slice(slice) => match *ty.kind() {
ty::Slice(ty) | ty::Array(ty, _) => { ty::Slice(ty) | ty::Array(ty, _) => {
let arity = slice.arity(); let arity = slice.arity();
cx.alloc_wildcard_slice((0..arity).map(|_| ty)) cx.dropless_arena.alloc_from_iter((0..arity).map(|_| ty))
} }
_ => bug!("bad slice pattern {:?} {:?}", ctor, ty), _ => bug!("bad slice pattern {:?} {:?}", ctor, ty),
}, },

View file

@ -39,17 +39,19 @@ pub fn analyze_match<'p, 'tcx>(
arms: &[MatchArm<'p, 'tcx>], arms: &[MatchArm<'p, 'tcx>],
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
) -> UsefulnessReport<'p, 'tcx> { ) -> UsefulnessReport<'p, 'tcx> {
// Arena to store the extra wildcards we construct during analysis.
let wildcard_arena = cx.pattern_arena;
let pat_column = PatternColumn::new(arms); let pat_column = PatternColumn::new(arms);
let report = compute_match_usefulness(cx, arms, scrut_ty); let report = compute_match_usefulness(cx, arms, scrut_ty, wildcard_arena);
// Lint on ranges that overlap on their endpoints, which is likely a mistake. // Lint on ranges that overlap on their endpoints, which is likely a mistake.
lint_overlapping_range_endpoints(cx, &pat_column); lint_overlapping_range_endpoints(cx, &pat_column, wildcard_arena);
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting // 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 let`s. Only run if the match is exhaustive otherwise the error is redundant.
if cx.refutable && report.non_exhaustiveness_witnesses.is_empty() { if cx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty) lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty, wildcard_arena)
} }
report report

View file

@ -1,3 +1,4 @@
use rustc_arena::TypedArena;
use smallvec::SmallVec; use smallvec::SmallVec;
use rustc_data_structures::captures::Captures; use rustc_data_structures::captures::Captures;
@ -27,11 +28,11 @@ use crate::MatchArm;
/// ///
/// This is not used in the main algorithm; only in lints. /// This is not used in the main algorithm; only in lints.
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct PatternColumn<'p, 'tcx> { pub(crate) struct PatternColumn<'a, 'p, 'tcx> {
patterns: Vec<&'p DeconstructedPat<'p, 'tcx>>, patterns: Vec<&'a DeconstructedPat<'p, 'tcx>>,
} }
impl<'p, 'tcx> PatternColumn<'p, 'tcx> { impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
pub(crate) fn new(arms: &[MatchArm<'p, 'tcx>]) -> Self { pub(crate) fn new(arms: &[MatchArm<'p, 'tcx>]) -> Self {
let mut patterns = Vec::with_capacity(arms.len()); let mut patterns = Vec::with_capacity(arms.len());
for arm in arms { for arm in arms {
@ -71,7 +72,7 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
pcx.cx.ctors_for_ty(pcx.ty).split(pcx, column_ctors) 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> { fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, 'tcx>> + Captures<'b> {
self.patterns.iter().copied() self.patterns.iter().copied()
} }
@ -80,7 +81,14 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
/// This returns one column per field of the constructor. They usually all have the same length /// 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 /// (the number of patterns in `self` that matched `ctor`), except that we expand or-patterns
/// which may change the lengths. /// which may change the lengths.
fn specialize(&self, pcx: &PatCtxt<'_, 'p, 'tcx>, ctor: &Constructor<'tcx>) -> Vec<Self> { fn specialize<'b>(
&self,
pcx: &'b PatCtxt<'_, 'p, 'tcx>,
ctor: &Constructor<'tcx>,
) -> Vec<PatternColumn<'b, 'p, 'tcx>>
where
'a: 'b,
{
let arity = ctor.arity(pcx); let arity = ctor.arity(pcx);
if arity == 0 { if arity == 0 {
return Vec::new(); return Vec::new();
@ -115,15 +123,16 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
/// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned /// Traverse the patterns to collect any variants of a non_exhaustive enum that fail to be mentioned
/// in a given column. /// in a given column.
#[instrument(level = "debug", skip(cx), ret)] #[instrument(level = "debug", skip(cx, wildcard_arena), ret)]
fn collect_nonexhaustive_missing_variants<'p, 'tcx>( fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &MatchCheckCtxt<'p, 'tcx>,
column: &PatternColumn<'p, 'tcx>, column: &PatternColumn<'a, 'p, 'tcx>,
wildcard_arena: &TypedArena<DeconstructedPat<'p, 'tcx>>,
) -> Vec<WitnessPat<'tcx>> { ) -> Vec<WitnessPat<'tcx>> {
let Some(ty) = column.head_ty() else { let Some(ty) = column.head_ty() else {
return Vec::new(); return Vec::new();
}; };
let pcx = &PatCtxt::new_dummy(cx, ty); let pcx = &PatCtxt::new_dummy(cx, ty, wildcard_arena);
let set = column.analyze_ctors(pcx); let set = column.analyze_ctors(pcx);
if set.present.is_empty() { if set.present.is_empty() {
@ -150,7 +159,7 @@ fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
let wild_pat = WitnessPat::wild_from_ctor(pcx, ctor); let wild_pat = WitnessPat::wild_from_ctor(pcx, ctor);
for (i, col_i) in specialized_columns.iter().enumerate() { for (i, col_i) in specialized_columns.iter().enumerate() {
// Compute witnesses for each column. // Compute witnesses for each column.
let wits_for_col_i = collect_nonexhaustive_missing_variants(cx, col_i); let wits_for_col_i = collect_nonexhaustive_missing_variants(cx, col_i, wildcard_arena);
// For each witness, we build a new pattern in the shape of `ctor(_, _, wit, _, _)`, // For each witness, we build a new pattern in the shape of `ctor(_, _, wit, _, _)`,
// adding enough wildcards to match `arity`. // adding enough wildcards to match `arity`.
for wit in wits_for_col_i { for wit in wits_for_col_i {
@ -163,17 +172,18 @@ fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
witnesses witnesses
} }
pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>( pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &MatchCheckCtxt<'p, 'tcx>,
arms: &[MatchArm<'p, 'tcx>], arms: &[MatchArm<'p, 'tcx>],
pat_column: &PatternColumn<'p, 'tcx>, pat_column: &PatternColumn<'a, 'p, 'tcx>,
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
wildcard_arena: &TypedArena<DeconstructedPat<'p, 'tcx>>,
) { ) {
if !matches!( if !matches!(
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, cx.match_lint_level).0, cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, cx.match_lint_level).0,
rustc_session::lint::Level::Allow rustc_session::lint::Level::Allow
) { ) {
let witnesses = collect_nonexhaustive_missing_variants(cx, pat_column); let witnesses = collect_nonexhaustive_missing_variants(cx, pat_column, wildcard_arena);
if !witnesses.is_empty() { if !witnesses.is_empty() {
// Report that a match of a `non_exhaustive` enum marked with `non_exhaustive_omitted_patterns` // Report that a match of a `non_exhaustive` enum marked with `non_exhaustive_omitted_patterns`
// is not exhaustive enough. // is not exhaustive enough.
@ -215,15 +225,16 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>(
} }
/// Traverse the patterns to warn the user about ranges that overlap on their endpoints. /// Traverse the patterns to warn the user about ranges that overlap on their endpoints.
#[instrument(level = "debug", skip(cx))] #[instrument(level = "debug", skip(cx, wildcard_arena))]
pub(crate) fn lint_overlapping_range_endpoints<'p, 'tcx>( pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &MatchCheckCtxt<'p, 'tcx>,
column: &PatternColumn<'p, 'tcx>, column: &PatternColumn<'a, 'p, 'tcx>,
wildcard_arena: &TypedArena<DeconstructedPat<'p, 'tcx>>,
) { ) {
let Some(ty) = column.head_ty() else { let Some(ty) = column.head_ty() else {
return; return;
}; };
let pcx = &PatCtxt::new_dummy(cx, ty); let pcx = &PatCtxt::new_dummy(cx, ty, wildcard_arena);
let set = column.analyze_ctors(pcx); let set = column.analyze_ctors(pcx);
@ -282,7 +293,7 @@ pub(crate) fn lint_overlapping_range_endpoints<'p, 'tcx>(
// Recurse into the fields. // Recurse into the fields.
for ctor in set.present { for ctor in set.present {
for col in column.specialize(pcx, &ctor) { for col in column.specialize(pcx, &ctor) {
lint_overlapping_range_endpoints(cx, &col); lint_overlapping_range_endpoints(cx, &col, wildcard_arena);
} }
} }
} }

View file

@ -53,7 +53,7 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
matches!(self.ctor, Or) matches!(self.ctor, Or)
} }
/// Expand this (possibly-nested) or-pattern into its alternatives. /// Expand this (possibly-nested) or-pattern into its alternatives.
pub(crate) fn flatten_or_pat(&'p self) -> SmallVec<[&'p Self; 1]> { pub(crate) fn flatten_or_pat(&self) -> SmallVec<[&Self; 1]> {
if self.is_or_pat() { if self.is_or_pat() {
self.iter_fields().flat_map(|p| p.flatten_or_pat()).collect() self.iter_fields().flat_map(|p| p.flatten_or_pat()).collect()
} else { } else {
@ -80,25 +80,29 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
/// Specialize this pattern with a constructor. /// Specialize this pattern with a constructor.
/// `other_ctor` can be different from `self.ctor`, but must be covered by it. /// `other_ctor` can be different from `self.ctor`, but must be covered by it.
pub(crate) fn specialize<'a>( pub(crate) fn specialize<'a>(
&'a self, &self,
pcx: &PatCtxt<'_, 'p, 'tcx>, pcx: &PatCtxt<'a, 'p, 'tcx>,
other_ctor: &Constructor<'tcx>, other_ctor: &Constructor<'tcx>,
) -> SmallVec<[&'p DeconstructedPat<'p, 'tcx>; 2]> { ) -> SmallVec<[&'a DeconstructedPat<'p, 'tcx>; 2]> {
let wildcard_sub_tys = || {
let tys = pcx.cx.ctor_sub_tys(other_ctor, pcx.ty);
tys.iter()
.map(|ty| DeconstructedPat::wildcard(*ty, Span::default()))
.map(|pat| pcx.wildcard_arena.alloc(pat) as &_)
.collect()
};
match (&self.ctor, other_ctor) { match (&self.ctor, other_ctor) {
(Wildcard, _) => { // Return a wildcard for each field of `other_ctor`.
// We return a wildcard for each field of `other_ctor`. (Wildcard, _) => wildcard_sub_tys(),
pcx.cx.ctor_wildcard_fields(other_ctor, pcx.ty).iter().collect() // The only non-trivial case: two slices of different arity. `other_slice` is
} // guaranteed to have a larger arity, so we fill the middle part with enough
// wildcards to reach the length of the new, larger slice.
( (
&Slice(self_slice @ Slice { kind: SliceKind::VarLen(prefix, suffix), .. }), &Slice(self_slice @ Slice { kind: SliceKind::VarLen(prefix, suffix), .. }),
&Slice(other_slice), &Slice(other_slice),
) if self_slice.arity() != other_slice.arity() => { ) if self_slice.arity() != other_slice.arity() => {
// The only non-trivial case: two slices of different arity. `other_slice` is
// guaranteed to have a larger arity, so we fill the middle part with enough
// wildcards to reach the length of the new, larger slice.
// Start with a slice of wildcards of the appropriate length. // Start with a slice of wildcards of the appropriate length.
let mut fields: SmallVec<[_; 2]> = let mut fields: SmallVec<[_; 2]> = wildcard_sub_tys();
pcx.cx.ctor_wildcard_fields(other_ctor, pcx.ty).iter().collect();
// Fill in the fields from both ends. // Fill in the fields from both ends.
let new_arity = fields.len(); let new_arity = fields.len();
for i in 0..prefix { for i in 0..prefix {
@ -179,9 +183,8 @@ impl<'tcx> WitnessPat<'tcx> {
/// For example, if `ctor` is a `Constructor::Variant` for `Option::Some`, we get the pattern /// For example, if `ctor` is a `Constructor::Variant` for `Option::Some`, we get the pattern
/// `Some(_)`. /// `Some(_)`.
pub(crate) fn wild_from_ctor(pcx: &PatCtxt<'_, '_, 'tcx>, ctor: Constructor<'tcx>) -> Self { pub(crate) fn wild_from_ctor(pcx: &PatCtxt<'_, '_, 'tcx>, ctor: Constructor<'tcx>) -> Self {
let field_tys = let field_tys = pcx.cx.ctor_sub_tys(&ctor, pcx.ty);
pcx.cx.ctor_wildcard_fields(&ctor, pcx.ty).iter().map(|deco_pat| deco_pat.ty()); let fields = field_tys.iter().map(|ty| Self::wildcard(*ty)).collect();
let fields = field_tys.map(|ty| Self::wildcard(ty)).collect();
Self::new(ctor, fields, pcx.ty) Self::new(ctor, fields, pcx.ty)
} }

View file

@ -555,9 +555,10 @@
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use std::fmt; use std::fmt;
use rustc_arena::TypedArena;
use rustc_data_structures::{captures::Captures, stack::ensure_sufficient_stack}; use rustc_data_structures::{captures::Captures, stack::ensure_sufficient_stack};
use rustc_middle::ty::Ty; use rustc_middle::ty::Ty;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::Span;
use crate::constructor::{Constructor, ConstructorSet}; use crate::constructor::{Constructor, ConstructorSet};
use crate::cx::MatchCheckCtxt; use crate::cx::MatchCheckCtxt;
@ -574,12 +575,18 @@ pub(crate) struct PatCtxt<'a, 'p, '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(crate) is_top_level: bool, pub(crate) is_top_level: bool,
/// An arena to store the wildcards we produce during analysis.
pub(crate) wildcard_arena: &'a TypedArena<DeconstructedPat<'p, 'tcx>>,
} }
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.
pub(crate) fn new_dummy(cx: &'a MatchCheckCtxt<'p, 'tcx>, ty: Ty<'tcx>) -> Self { pub(crate) fn new_dummy(
PatCtxt { cx, ty, is_top_level: false } cx: &'a MatchCheckCtxt<'p, 'tcx>,
ty: Ty<'tcx>,
wildcard_arena: &'a TypedArena<DeconstructedPat<'p, 'tcx>>,
) -> Self {
PatCtxt { cx, ty, is_top_level: false, wildcard_arena }
} }
} }
@ -651,14 +658,18 @@ impl fmt::Display for ValidityConstraint {
} }
/// Represents a pattern-tuple under investigation. /// Represents a pattern-tuple under investigation.
// The three lifetimes are:
// - 'a allocated by us
// - 'p coming from the input
// - 'tcx global compilation context
#[derive(Clone)] #[derive(Clone)]
struct PatStack<'p, 'tcx> { struct PatStack<'a, 'p, 'tcx> {
// Rows of len 1 are very common, which is why `SmallVec[_; 2]` works well. // Rows of len 1 are very common, which is why `SmallVec[_; 2]` works well.
pats: SmallVec<[&'p DeconstructedPat<'p, 'tcx>; 2]>, pats: SmallVec<[&'a DeconstructedPat<'p, 'tcx>; 2]>,
} }
impl<'p, 'tcx> PatStack<'p, 'tcx> { impl<'a, 'p, 'tcx> PatStack<'a, 'p, 'tcx> {
fn from_pattern(pat: &'p DeconstructedPat<'p, 'tcx>) -> Self { fn from_pattern(pat: &'a DeconstructedPat<'p, 'tcx>) -> Self {
PatStack { pats: smallvec![pat] } PatStack { pats: smallvec![pat] }
} }
@ -670,17 +681,17 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> {
self.pats.len() self.pats.len()
} }
fn head(&self) -> &'p DeconstructedPat<'p, 'tcx> { fn head(&self) -> &'a DeconstructedPat<'p, 'tcx> {
self.pats[0] self.pats[0]
} }
fn iter(&self) -> impl Iterator<Item = &DeconstructedPat<'p, 'tcx>> { fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, 'tcx>> + Captures<'b> {
self.pats.iter().copied() self.pats.iter().copied()
} }
// Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is // Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is
// an or-pattern. Panics if `self` is empty. // an or-pattern. Panics if `self` is empty.
fn expand_or_pat<'a>(&'a self) -> impl Iterator<Item = PatStack<'p, 'tcx>> + Captures<'a> { fn expand_or_pat<'b>(&'b self) -> impl Iterator<Item = PatStack<'a, 'p, 'tcx>> + Captures<'b> {
self.head().flatten_or_pat().into_iter().map(move |pat| { self.head().flatten_or_pat().into_iter().map(move |pat| {
let mut new = self.clone(); let mut new = self.clone();
new.pats[0] = pat; new.pats[0] = pat;
@ -692,9 +703,9 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> {
/// Only call if `ctor.is_covered_by(self.head().ctor())` is true. /// Only call if `ctor.is_covered_by(self.head().ctor())` is true.
fn pop_head_constructor( fn pop_head_constructor(
&self, &self,
pcx: &PatCtxt<'_, 'p, 'tcx>, pcx: &PatCtxt<'a, 'p, 'tcx>,
ctor: &Constructor<'tcx>, ctor: &Constructor<'tcx>,
) -> PatStack<'p, 'tcx> { ) -> PatStack<'a, 'p, 'tcx> {
// We pop the head pattern and push the new fields extracted from the arguments of // We pop the head pattern and push the new fields extracted from the arguments of
// `self.head()`. // `self.head()`.
let mut new_pats = self.head().specialize(pcx, ctor); let mut new_pats = self.head().specialize(pcx, ctor);
@ -703,7 +714,7 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> {
} }
} }
impl<'p, 'tcx> fmt::Debug for PatStack<'p, 'tcx> { impl<'a, 'p, 'tcx> fmt::Debug for PatStack<'a, 'p, 'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// We pretty-print similarly to the `Debug` impl of `Matrix`. // We pretty-print similarly to the `Debug` impl of `Matrix`.
write!(f, "+")?; write!(f, "+")?;
@ -716,9 +727,9 @@ impl<'p, 'tcx> fmt::Debug for PatStack<'p, 'tcx> {
/// A row of the matrix. /// A row of the matrix.
#[derive(Clone)] #[derive(Clone)]
struct MatrixRow<'p, 'tcx> { struct MatrixRow<'a, 'p, 'tcx> {
// The patterns in the row. // The patterns in the row.
pats: PatStack<'p, 'tcx>, pats: PatStack<'a, 'p, 'tcx>,
/// Whether the original arm had a guard. This is inherited when specializing. /// Whether the original arm had a guard. This is inherited when specializing.
is_under_guard: bool, is_under_guard: bool,
/// When we specialize, we remember which row of the original matrix produced a given row of the /// When we specialize, we remember which row of the original matrix produced a given row of the
@ -731,7 +742,7 @@ struct MatrixRow<'p, 'tcx> {
useful: bool, useful: bool,
} }
impl<'p, 'tcx> MatrixRow<'p, 'tcx> { impl<'a, 'p, 'tcx> MatrixRow<'a, 'p, 'tcx> {
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
self.pats.is_empty() self.pats.is_empty()
} }
@ -740,17 +751,17 @@ impl<'p, 'tcx> MatrixRow<'p, 'tcx> {
self.pats.len() self.pats.len()
} }
fn head(&self) -> &'p DeconstructedPat<'p, 'tcx> { fn head(&self) -> &'a DeconstructedPat<'p, 'tcx> {
self.pats.head() self.pats.head()
} }
fn iter(&self) -> impl Iterator<Item = &DeconstructedPat<'p, 'tcx>> { fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, 'tcx>> + Captures<'b> {
self.pats.iter() self.pats.iter()
} }
// Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is // Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is
// an or-pattern. Panics if `self` is empty. // an or-pattern. Panics if `self` is empty.
fn expand_or_pat<'a>(&'a self) -> impl Iterator<Item = MatrixRow<'p, 'tcx>> + Captures<'a> { fn expand_or_pat<'b>(&'b self) -> impl Iterator<Item = MatrixRow<'a, 'p, 'tcx>> + Captures<'b> {
self.pats.expand_or_pat().map(|patstack| MatrixRow { self.pats.expand_or_pat().map(|patstack| MatrixRow {
pats: patstack, pats: patstack,
parent_row: self.parent_row, parent_row: self.parent_row,
@ -763,10 +774,10 @@ impl<'p, 'tcx> MatrixRow<'p, 'tcx> {
/// Only call if `ctor.is_covered_by(self.head().ctor())` is true. /// Only call if `ctor.is_covered_by(self.head().ctor())` is true.
fn pop_head_constructor( fn pop_head_constructor(
&self, &self,
pcx: &PatCtxt<'_, 'p, 'tcx>, pcx: &PatCtxt<'a, 'p, 'tcx>,
ctor: &Constructor<'tcx>, ctor: &Constructor<'tcx>,
parent_row: usize, parent_row: usize,
) -> MatrixRow<'p, 'tcx> { ) -> MatrixRow<'a, 'p, 'tcx> {
MatrixRow { MatrixRow {
pats: self.pats.pop_head_constructor(pcx, ctor), pats: self.pats.pop_head_constructor(pcx, ctor),
parent_row, parent_row,
@ -776,7 +787,7 @@ impl<'p, 'tcx> MatrixRow<'p, 'tcx> {
} }
} }
impl<'p, 'tcx> fmt::Debug for MatrixRow<'p, 'tcx> { impl<'a, 'p, 'tcx> fmt::Debug for MatrixRow<'a, 'p, 'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.pats.fmt(f) self.pats.fmt(f)
} }
@ -793,22 +804,22 @@ impl<'p, 'tcx> fmt::Debug for MatrixRow<'p, 'tcx> {
/// specializing `(,)` and `Some` on a pattern of type `(Option<u32>, bool)`, the first column of /// specializing `(,)` and `Some` on a pattern of type `(Option<u32>, bool)`, the first column of
/// the matrix will correspond to `scrutinee.0.Some.0` and the second column to `scrutinee.1`. /// the matrix will correspond to `scrutinee.0.Some.0` and the second column to `scrutinee.1`.
#[derive(Clone)] #[derive(Clone)]
struct Matrix<'p, 'tcx> { struct Matrix<'a, 'p, 'tcx> {
/// Vector of rows. The rows must form a rectangular 2D array. Moreover, all the patterns of /// Vector of rows. The rows must form a rectangular 2D array. Moreover, all the patterns of
/// each column must have the same type. Each column corresponds to a place within the /// each column must have the same type. Each column corresponds to a place within the
/// scrutinee. /// scrutinee.
rows: Vec<MatrixRow<'p, 'tcx>>, rows: Vec<MatrixRow<'a, 'p, 'tcx>>,
/// Stores an extra fictitious row full of wildcards. Mostly used to keep track of the type of /// Stores an extra fictitious row full of wildcards. Mostly used to keep track of the type of
/// each column. This must obey the same invariants as the real rows. /// each column. This must obey the same invariants as the real rows.
wildcard_row: PatStack<'p, 'tcx>, wildcard_row: PatStack<'a, 'p, 'tcx>,
/// Track for each column/place whether it contains a known valid value. /// Track for each column/place whether it contains a known valid value.
place_validity: SmallVec<[ValidityConstraint; 2]>, place_validity: SmallVec<[ValidityConstraint; 2]>,
} }
impl<'p, 'tcx> Matrix<'p, 'tcx> { impl<'a, 'p, 'tcx> Matrix<'a, 'p, 'tcx> {
/// Pushes a new row to the matrix. If the row starts with an or-pattern, this recursively /// Pushes a new row to the matrix. If the row starts with an or-pattern, this recursively
/// expands it. Internal method, prefer [`Matrix::new`]. /// expands it. Internal method, prefer [`Matrix::new`].
fn expand_and_push(&mut self, row: MatrixRow<'p, 'tcx>) { fn expand_and_push(&mut self, row: MatrixRow<'a, 'p, 'tcx>) {
if !row.is_empty() && row.head().is_or_pat() { if !row.is_empty() && row.head().is_or_pat() {
// Expand nested or-patterns. // Expand nested or-patterns.
for new_row in row.expand_or_pat() { for new_row in row.expand_or_pat() {
@ -820,16 +831,14 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
} }
/// Build a new matrix from an iterator of `MatchArm`s. /// Build a new matrix from an iterator of `MatchArm`s.
fn new<'a>( fn new(
cx: &MatchCheckCtxt<'p, 'tcx>, wildcard_arena: &'a TypedArena<DeconstructedPat<'p, 'tcx>>,
arms: &[MatchArm<'p, 'tcx>], arms: &'a [MatchArm<'p, 'tcx>],
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
scrut_validity: ValidityConstraint, scrut_validity: ValidityConstraint,
) -> Self ) -> Self {
where let wild_pattern =
'p: 'a, wildcard_arena.alloc(DeconstructedPat::wildcard(scrut_ty, Span::default()));
{
let wild_pattern = cx.pattern_arena.alloc(DeconstructedPat::wildcard(scrut_ty, DUMMY_SP));
let wildcard_row = PatStack::from_pattern(wild_pattern); let wildcard_row = PatStack::from_pattern(wild_pattern);
let mut matrix = Matrix { let mut matrix = Matrix {
rows: Vec::with_capacity(arms.len()), rows: Vec::with_capacity(arms.len()),
@ -871,32 +880,34 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
self.wildcard_row.len() self.wildcard_row.len()
} }
fn rows<'a>( fn rows<'b>(
&'a self, &'b self,
) -> impl Iterator<Item = &'a MatrixRow<'p, 'tcx>> + Clone + DoubleEndedIterator + ExactSizeIterator ) -> impl Iterator<Item = &'b MatrixRow<'a, 'p, 'tcx>>
{ + Clone
+ DoubleEndedIterator
+ ExactSizeIterator {
self.rows.iter() self.rows.iter()
} }
fn rows_mut<'a>( fn rows_mut<'b>(
&'a mut self, &'b mut self,
) -> impl Iterator<Item = &'a mut MatrixRow<'p, 'tcx>> + DoubleEndedIterator + ExactSizeIterator ) -> impl Iterator<Item = &'b mut MatrixRow<'a, 'p, 'tcx>> + DoubleEndedIterator + ExactSizeIterator
{ {
self.rows.iter_mut() self.rows.iter_mut()
} }
/// Iterate over the first pattern of each row. /// Iterate over the first pattern of each row.
fn heads<'a>( fn heads<'b>(
&'a self, &'b self,
) -> impl Iterator<Item = &'p DeconstructedPat<'p, 'tcx>> + Clone + Captures<'a> { ) -> impl Iterator<Item = &'b DeconstructedPat<'p, 'tcx>> + Clone + Captures<'a> {
self.rows().map(|r| r.head()) self.rows().map(|r| r.head())
} }
/// This computes `specialize(ctor, self)`. See top of the file for explanations. /// This computes `specialize(ctor, self)`. See top of the file for explanations.
fn specialize_constructor( fn specialize_constructor(
&self, &self,
pcx: &PatCtxt<'_, 'p, 'tcx>, pcx: &PatCtxt<'a, 'p, 'tcx>,
ctor: &Constructor<'tcx>, ctor: &Constructor<'tcx>,
) -> Matrix<'p, 'tcx> { ) -> Matrix<'a, 'p, 'tcx> {
let wildcard_row = self.wildcard_row.pop_head_constructor(pcx, ctor); let wildcard_row = self.wildcard_row.pop_head_constructor(pcx, ctor);
let new_validity = self.place_validity[0].specialize(ctor); let new_validity = self.place_validity[0].specialize(ctor);
let new_place_validity = std::iter::repeat(new_validity) let new_place_validity = std::iter::repeat(new_validity)
@ -925,7 +936,7 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
/// + _ + [_, _, tail @ ..] + /// + _ + [_, _, tail @ ..] +
/// | ✓ | ? | // column validity /// | ✓ | ? | // column validity
/// ``` /// ```
impl<'p, 'tcx> fmt::Debug for Matrix<'p, 'tcx> { impl<'a, 'p, 'tcx> fmt::Debug for Matrix<'a, 'p, 'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\n")?; write!(f, "\n")?;
@ -1156,10 +1167,11 @@ impl<'tcx> WitnessMatrix<'tcx> {
/// - unspecialization, where we lift the results from the previous step into results for this step /// - unspecialization, where we lift the results from the previous step into results for this step
/// (using `apply_constructor` and by updating `row.useful` for each parent row). /// (using `apply_constructor` and by updating `row.useful` for each parent row).
/// This is all explained at the top of the file. /// This is all explained at the top of the file.
#[instrument(level = "debug", skip(cx, is_top_level), ret)] #[instrument(level = "debug", skip(cx, is_top_level, wildcard_arena), ret)]
fn compute_exhaustiveness_and_usefulness<'p, 'tcx>( fn compute_exhaustiveness_and_usefulness<'a, 'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &'a MatchCheckCtxt<'p, 'tcx>,
matrix: &mut Matrix<'p, 'tcx>, matrix: &mut Matrix<'a, 'p, 'tcx>,
wildcard_arena: &'a TypedArena<DeconstructedPat<'p, 'tcx>>,
is_top_level: bool, is_top_level: bool,
) -> WitnessMatrix<'tcx> { ) -> WitnessMatrix<'tcx> {
debug_assert!(matrix.rows().all(|r| r.len() == matrix.column_count())); debug_assert!(matrix.rows().all(|r| r.len() == matrix.column_count()));
@ -1181,7 +1193,7 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
}; };
debug!("ty: {ty:?}"); debug!("ty: {ty:?}");
let pcx = &PatCtxt { cx, ty, is_top_level }; let pcx = &PatCtxt { cx, ty, is_top_level, wildcard_arena };
// Whether the place/column we are inspecting is known to contain valid data. // Whether the place/column we are inspecting is known to contain valid data.
let place_validity = matrix.place_validity[0]; let place_validity = matrix.place_validity[0];
@ -1224,7 +1236,7 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
// Dig into rows that match `ctor`. // Dig into rows that match `ctor`.
let mut spec_matrix = matrix.specialize_constructor(pcx, &ctor); let mut spec_matrix = matrix.specialize_constructor(pcx, &ctor);
let mut witnesses = ensure_sufficient_stack(|| { let mut witnesses = ensure_sufficient_stack(|| {
compute_exhaustiveness_and_usefulness(cx, &mut spec_matrix, false) compute_exhaustiveness_and_usefulness(cx, &mut spec_matrix, wildcard_arena, false)
}); });
let counts_for_exhaustiveness = match ctor { let counts_for_exhaustiveness = match ctor {
@ -1286,15 +1298,17 @@ pub struct UsefulnessReport<'p, 'tcx> {
} }
/// Computes whether a match is exhaustive and which of its arms are useful. /// Computes whether a match is exhaustive and which of its arms are useful.
#[instrument(skip(cx, arms), level = "debug")] #[instrument(skip(cx, arms, wildcard_arena), level = "debug")]
pub(crate) 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>,
wildcard_arena: &TypedArena<DeconstructedPat<'p, 'tcx>>,
) -> UsefulnessReport<'p, 'tcx> { ) -> UsefulnessReport<'p, 'tcx> {
let scrut_validity = ValidityConstraint::from_bool(cx.known_valid_scrutinee); let scrut_validity = ValidityConstraint::from_bool(cx.known_valid_scrutinee);
let mut matrix = Matrix::new(cx, arms, scrut_ty, scrut_validity); let mut matrix = Matrix::new(wildcard_arena, arms, scrut_ty, scrut_validity);
let non_exhaustiveness_witnesses = compute_exhaustiveness_and_usefulness(cx, &mut matrix, true); let non_exhaustiveness_witnesses =
compute_exhaustiveness_and_usefulness(cx, &mut matrix, wildcard_arena, true);
let non_exhaustiveness_witnesses: Vec<_> = non_exhaustiveness_witnesses.single_column(); let non_exhaustiveness_witnesses: Vec<_> = non_exhaustiveness_witnesses.single_column();
let arm_usefulness: Vec<_> = arms let arm_usefulness: Vec<_> = arms