about summary refs log tree commit diff
path: root/compiler/rustc_pattern_analysis/src
diff options
context:
space:
mode:
authorNadrieril <nadrieril+git@gmail.com>2023-12-11 17:57:53 +0100
committerNadrieril <nadrieril+git@gmail.com>2023-12-15 16:57:36 +0100
commit3ad76f93256c0869aafeb1404f494f00e6d5b5ae (patch)
tree262a396288e1b7b0cb3724f195056951e787317d /compiler/rustc_pattern_analysis/src
parent081c3dcf43e31ea2c5226ead3639a500b3ac3049 (diff)
downloadrust-3ad76f93256c0869aafeb1404f494f00e6d5b5ae.tar.gz
rust-3ad76f93256c0869aafeb1404f494f00e6d5b5ae.zip
Disentangle the arena from `MatchCheckCtxt`
Diffstat (limited to 'compiler/rustc_pattern_analysis/src')
-rw-r--r--compiler/rustc_pattern_analysis/src/cx.rs33
-rw-r--r--compiler/rustc_pattern_analysis/src/lib.rs8
-rw-r--r--compiler/rustc_pattern_analysis/src/lints.rs47
-rw-r--r--compiler/rustc_pattern_analysis/src/pat.rs35
-rw-r--r--compiler/rustc_pattern_analysis/src/usefulness.rs128
5 files changed, 135 insertions, 116 deletions
diff --git a/compiler/rustc_pattern_analysis/src/cx.rs b/compiler/rustc_pattern_analysis/src/cx.rs
index e4acf317a0d..a4bc99c8013 100644
--- a/compiler/rustc_pattern_analysis/src/cx.rs
+++ b/compiler/rustc_pattern_analysis/src/cx.rs
@@ -1,15 +1,15 @@
 use std::fmt;
 use std::iter::once;
 
-use rustc_arena::TypedArena;
+use rustc_arena::{DroplessArena, TypedArena};
 use rustc_data_structures::captures::Captures;
 use rustc_hir::def_id::DefId;
 use rustc_hir::{HirId, RangeEnd};
 use rustc_index::Idx;
 use rustc_index::IndexVec;
 use rustc_middle::middle::stability::EvalResult;
-use rustc_middle::mir;
 use rustc_middle::mir::interpret::Scalar;
+use rustc_middle::mir::{self};
 use rustc_middle::thir::{FieldPat, Pat, PatKind, PatRange, PatRangeBoundary};
 use rustc_middle::ty::layout::IntegerExt;
 use rustc_middle::ty::{self, Ty, TyCtxt, VariantDef};
@@ -35,6 +35,7 @@ pub struct MatchCheckCtxt<'p, 'tcx> {
     pub module: DefId,
     pub param_env: ty::ParamEnv<'tcx>,
     pub pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>,
+    pub dropless_arena: &'p DroplessArena,
     /// Lint level at the match.
     pub match_lint_level: HirId,
     /// 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
     // uninhabited fields in order not to reveal the uninhabitedness of the whole variant.
     // 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
-    /// of `ctor.arity()`.
+    /// Returns the types of the fields for a given constructor. The result must have a length of
+    /// `ctor.arity()`.
     #[instrument(level = "trace", skip(self))]
-    pub(crate) fn ctor_wildcard_fields(
-        &self,
-        ctor: &Constructor<'tcx>,
-        ty: Ty<'tcx>,
-    ) -> &'p [DeconstructedPat<'p, 'tcx>] {
+    pub(crate) fn ctor_sub_tys(&self, ctor: &Constructor<'tcx>, ty: Ty<'tcx>) -> &[Ty<'tcx>] {
         let cx = self;
         match ctor {
             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) => {
                     if adt.is_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.
-                        cx.alloc_wildcard_slice(once(args.type_at(0)))
+                        cx.dropless_arena.alloc_from_iter(once(args.type_at(0)))
                     } else {
                         let variant =
                             &adt.variant(MatchCheckCtxt::variant_index_for_adt(&ctor, *adt));
                         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:?}"),
             },
             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:?}"),
             },
             Slice(slice) => match *ty.kind() {
                 ty::Slice(ty) | ty::Array(ty, _) => {
                     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),
             },
diff --git a/compiler/rustc_pattern_analysis/src/lib.rs b/compiler/rustc_pattern_analysis/src/lib.rs
index 07730aa49d3..f19dc7345fc 100644
--- a/compiler/rustc_pattern_analysis/src/lib.rs
+++ b/compiler/rustc_pattern_analysis/src/lib.rs
@@ -39,17 +39,19 @@ pub fn analyze_match<'p, 'tcx>(
     arms: &[MatchArm<'p, 'tcx>],
     scrut_ty: Ty<'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 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_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
     // `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)
+        lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty, wildcard_arena)
     }
 
     report
diff --git a/compiler/rustc_pattern_analysis/src/lints.rs b/compiler/rustc_pattern_analysis/src/lints.rs
index aaa859f33fa..858e28ce897 100644
--- a/compiler/rustc_pattern_analysis/src/lints.rs
+++ b/compiler/rustc_pattern_analysis/src/lints.rs
@@ -1,3 +1,4 @@
+use rustc_arena::TypedArena;
 use smallvec::SmallVec;
 
 use rustc_data_structures::captures::Captures;
@@ -27,11 +28,11 @@ use crate::MatchArm;
 ///
 /// 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>>,
+pub(crate) struct PatternColumn<'a, '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 {
         let mut patterns = Vec::with_capacity(arms.len());
         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)
     }
 
-    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()
     }
 
@@ -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
     /// (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> {
+    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);
         if arity == 0 {
             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
 /// in a given column.
-#[instrument(level = "debug", skip(cx), ret)]
-fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
+#[instrument(level = "debug", skip(cx, wildcard_arena), ret)]
+fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
     cx: &MatchCheckCtxt<'p, 'tcx>,
-    column: &PatternColumn<'p, 'tcx>,
+    column: &PatternColumn<'a, 'p, 'tcx>,
+    wildcard_arena: &TypedArena<DeconstructedPat<'p, 'tcx>>,
 ) -> Vec<WitnessPat<'tcx>> {
     let Some(ty) = column.head_ty() else {
         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);
     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);
         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);
+            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, _, _)`,
             // adding enough wildcards to match `arity`.
             for wit in wits_for_col_i {
@@ -163,17 +172,18 @@ fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
     witnesses
 }
 
-pub(crate) fn lint_nonexhaustive_missing_variants<'p, 'tcx>(
+pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
     cx: &MatchCheckCtxt<'p, 'tcx>,
     arms: &[MatchArm<'p, 'tcx>],
-    pat_column: &PatternColumn<'p, 'tcx>,
+    pat_column: &PatternColumn<'a, 'p, 'tcx>,
     scrut_ty: Ty<'tcx>,
+    wildcard_arena: &TypedArena<DeconstructedPat<'p, '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);
+        let witnesses = collect_nonexhaustive_missing_variants(cx, pat_column, wildcard_arena);
         if !witnesses.is_empty() {
             // Report that a match of a `non_exhaustive` enum marked with `non_exhaustive_omitted_patterns`
             // 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.
-#[instrument(level = "debug", skip(cx))]
-pub(crate) fn lint_overlapping_range_endpoints<'p, 'tcx>(
+#[instrument(level = "debug", skip(cx, wildcard_arena))]
+pub(crate) fn lint_overlapping_range_endpoints<'a, '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 {
         return;
     };
-    let pcx = &PatCtxt::new_dummy(cx, ty);
+    let pcx = &PatCtxt::new_dummy(cx, ty, wildcard_arena);
 
     let set = column.analyze_ctors(pcx);
 
@@ -282,7 +293,7 @@ pub(crate) fn lint_overlapping_range_endpoints<'p, 'tcx>(
         // Recurse into the fields.
         for ctor in set.present {
             for col in column.specialize(pcx, &ctor) {
-                lint_overlapping_range_endpoints(cx, &col);
+                lint_overlapping_range_endpoints(cx, &col, wildcard_arena);
             }
         }
     }
diff --git a/compiler/rustc_pattern_analysis/src/pat.rs b/compiler/rustc_pattern_analysis/src/pat.rs
index 7f33a1ab40c..29230c390ef 100644
--- a/compiler/rustc_pattern_analysis/src/pat.rs
+++ b/compiler/rustc_pattern_analysis/src/pat.rs
@@ -53,7 +53,7 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
         matches!(self.ctor, Or)
     }
     /// 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() {
             self.iter_fields().flat_map(|p| p.flatten_or_pat()).collect()
         } else {
@@ -80,25 +80,29 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
     /// Specialize this pattern with a constructor.
     /// `other_ctor` can be different from `self.ctor`, but must be covered by it.
     pub(crate) fn specialize<'a>(
-        &'a self,
-        pcx: &PatCtxt<'_, 'p, 'tcx>,
+        &self,
+        pcx: &PatCtxt<'a, 'p, '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) {
-            (Wildcard, _) => {
-                // We return a wildcard for each field of `other_ctor`.
-                pcx.cx.ctor_wildcard_fields(other_ctor, pcx.ty).iter().collect()
-            }
+            // Return a wildcard for each field of `other_ctor`.
+            (Wildcard, _) => wildcard_sub_tys(),
+            // 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(other_slice),
             ) 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.
-                let mut fields: SmallVec<[_; 2]> =
-                    pcx.cx.ctor_wildcard_fields(other_ctor, pcx.ty).iter().collect();
+                let mut fields: SmallVec<[_; 2]> = wildcard_sub_tys();
                 // Fill in the fields from both ends.
                 let new_arity = fields.len();
                 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
     /// `Some(_)`.
     pub(crate) fn wild_from_ctor(pcx: &PatCtxt<'_, '_, 'tcx>, ctor: Constructor<'tcx>) -> Self {
-        let field_tys =
-            pcx.cx.ctor_wildcard_fields(&ctor, pcx.ty).iter().map(|deco_pat| deco_pat.ty());
-        let fields = field_tys.map(|ty| Self::wildcard(ty)).collect();
+        let field_tys = pcx.cx.ctor_sub_tys(&ctor, pcx.ty);
+        let fields = field_tys.iter().map(|ty| Self::wildcard(*ty)).collect();
         Self::new(ctor, fields, pcx.ty)
     }
 
diff --git a/compiler/rustc_pattern_analysis/src/usefulness.rs b/compiler/rustc_pattern_analysis/src/usefulness.rs
index d007e382000..3300013805d 100644
--- a/compiler/rustc_pattern_analysis/src/usefulness.rs
+++ b/compiler/rustc_pattern_analysis/src/usefulness.rs
@@ -555,9 +555,10 @@
 use smallvec::{smallvec, SmallVec};
 use std::fmt;
 
+use rustc_arena::TypedArena;
 use rustc_data_structures::{captures::Captures, stack::ensure_sufficient_stack};
 use rustc_middle::ty::Ty;
-use rustc_span::{Span, DUMMY_SP};
+use rustc_span::Span;
 
 use crate::constructor::{Constructor, ConstructorSet};
 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
     /// subpattern.
     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> {
     /// A `PatCtxt` when code other than `is_useful` needs one.
-    pub(crate) fn new_dummy(cx: &'a MatchCheckCtxt<'p, 'tcx>, ty: Ty<'tcx>) -> Self {
-        PatCtxt { cx, ty, is_top_level: false }
+    pub(crate) fn new_dummy(
+        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.
+// The three lifetimes are:
+// - 'a allocated by us
+// - 'p coming from the input
+// - 'tcx global compilation context
 #[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.
-    pats: SmallVec<[&'p DeconstructedPat<'p, 'tcx>; 2]>,
+    pats: SmallVec<[&'a DeconstructedPat<'p, 'tcx>; 2]>,
 }
 
-impl<'p, 'tcx> PatStack<'p, 'tcx> {
-    fn from_pattern(pat: &'p DeconstructedPat<'p, 'tcx>) -> Self {
+impl<'a, 'p, 'tcx> PatStack<'a, 'p, 'tcx> {
+    fn from_pattern(pat: &'a DeconstructedPat<'p, 'tcx>) -> Self {
         PatStack { pats: smallvec![pat] }
     }
 
@@ -670,17 +681,17 @@ impl<'p, 'tcx> PatStack<'p, 'tcx> {
         self.pats.len()
     }
 
-    fn head(&self) -> &'p DeconstructedPat<'p, 'tcx> {
+    fn head(&self) -> &'a DeconstructedPat<'p, 'tcx> {
         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()
     }
 
     // Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is
     // 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| {
             let mut new = self.clone();
             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.
     fn pop_head_constructor(
         &self,
-        pcx: &PatCtxt<'_, 'p, 'tcx>,
+        pcx: &PatCtxt<'a, 'p, '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
         // `self.head()`.
         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 {
         // We pretty-print similarly to the `Debug` impl of `Matrix`.
         write!(f, "+")?;
@@ -716,9 +727,9 @@ impl<'p, 'tcx> fmt::Debug for PatStack<'p, 'tcx> {
 
 /// A row of the matrix.
 #[derive(Clone)]
-struct MatrixRow<'p, 'tcx> {
+struct MatrixRow<'a, 'p, 'tcx> {
     // 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.
     is_under_guard: bool,
     /// 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,
 }
 
-impl<'p, 'tcx> MatrixRow<'p, 'tcx> {
+impl<'a, 'p, 'tcx> MatrixRow<'a, 'p, 'tcx> {
     fn is_empty(&self) -> bool {
         self.pats.is_empty()
     }
@@ -740,17 +751,17 @@ impl<'p, 'tcx> MatrixRow<'p, 'tcx> {
         self.pats.len()
     }
 
-    fn head(&self) -> &'p DeconstructedPat<'p, 'tcx> {
+    fn head(&self) -> &'a DeconstructedPat<'p, 'tcx> {
         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()
     }
 
     // Recursively expand the first or-pattern into its subpatterns. Only useful if the pattern is
     // 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 {
             pats: patstack,
             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.
     fn pop_head_constructor(
         &self,
-        pcx: &PatCtxt<'_, 'p, 'tcx>,
+        pcx: &PatCtxt<'a, 'p, 'tcx>,
         ctor: &Constructor<'tcx>,
         parent_row: usize,
-    ) -> MatrixRow<'p, 'tcx> {
+    ) -> MatrixRow<'a, 'p, 'tcx> {
         MatrixRow {
             pats: self.pats.pop_head_constructor(pcx, ctor),
             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 {
         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
 /// the matrix will correspond to `scrutinee.0.Some.0` and the second column to `scrutinee.1`.
 #[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
     /// each column must have the same type. Each column corresponds to a place within the
     /// 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
     /// 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.
     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
     /// 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() {
             // Expand nested or-patterns.
             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.
-    fn new<'a>(
-        cx: &MatchCheckCtxt<'p, 'tcx>,
-        arms: &[MatchArm<'p, 'tcx>],
+    fn new(
+        wildcard_arena: &'a TypedArena<DeconstructedPat<'p, 'tcx>>,
+        arms: &'a [MatchArm<'p, 'tcx>],
         scrut_ty: Ty<'tcx>,
         scrut_validity: ValidityConstraint,
-    ) -> Self
-    where
-        'p: 'a,
-    {
-        let wild_pattern = cx.pattern_arena.alloc(DeconstructedPat::wildcard(scrut_ty, DUMMY_SP));
+    ) -> Self {
+        let wild_pattern =
+            wildcard_arena.alloc(DeconstructedPat::wildcard(scrut_ty, Span::default()));
         let wildcard_row = PatStack::from_pattern(wild_pattern);
         let mut matrix = Matrix {
             rows: Vec::with_capacity(arms.len()),
@@ -871,32 +880,34 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
         self.wildcard_row.len()
     }
 
-    fn rows<'a>(
-        &'a self,
-    ) -> impl Iterator<Item = &'a MatrixRow<'p, 'tcx>> + Clone + DoubleEndedIterator + ExactSizeIterator
-    {
+    fn rows<'b>(
+        &'b self,
+    ) -> impl Iterator<Item = &'b MatrixRow<'a, 'p, 'tcx>>
+    + Clone
+    + DoubleEndedIterator
+    + ExactSizeIterator {
         self.rows.iter()
     }
-    fn rows_mut<'a>(
-        &'a mut self,
-    ) -> impl Iterator<Item = &'a mut MatrixRow<'p, 'tcx>> + DoubleEndedIterator + ExactSizeIterator
+    fn rows_mut<'b>(
+        &'b mut self,
+    ) -> impl Iterator<Item = &'b mut MatrixRow<'a, 'p, 'tcx>> + DoubleEndedIterator + ExactSizeIterator
     {
         self.rows.iter_mut()
     }
 
     /// Iterate over the first pattern of each row.
-    fn heads<'a>(
-        &'a self,
-    ) -> impl Iterator<Item = &'p DeconstructedPat<'p, 'tcx>> + Clone + Captures<'a> {
+    fn heads<'b>(
+        &'b self,
+    ) -> impl Iterator<Item = &'b DeconstructedPat<'p, 'tcx>> + Clone + Captures<'a> {
         self.rows().map(|r| r.head())
     }
 
     /// This computes `specialize(ctor, self)`. See top of the file for explanations.
     fn specialize_constructor(
         &self,
-        pcx: &PatCtxt<'_, 'p, 'tcx>,
+        pcx: &PatCtxt<'a, 'p, 'tcx>,
         ctor: &Constructor<'tcx>,
-    ) -> Matrix<'p, 'tcx> {
+    ) -> Matrix<'a, 'p, 'tcx> {
         let wildcard_row = self.wildcard_row.pop_head_constructor(pcx, ctor);
         let new_validity = self.place_validity[0].specialize(ctor);
         let new_place_validity = std::iter::repeat(new_validity)
@@ -925,7 +936,7 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
 /// + _     + [_, _, tail @ ..] +
 /// | ✓     | ?                 | // 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 {
         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
 ///     (using `apply_constructor` and by updating `row.useful` for each parent row).
 /// This is all explained at the top of the file.
-#[instrument(level = "debug", skip(cx, is_top_level), ret)]
-fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
-    cx: &MatchCheckCtxt<'p, 'tcx>,
-    matrix: &mut Matrix<'p, 'tcx>,
+#[instrument(level = "debug", skip(cx, is_top_level, wildcard_arena), ret)]
+fn compute_exhaustiveness_and_usefulness<'a, 'p, 'tcx>(
+    cx: &'a MatchCheckCtxt<'p, 'tcx>,
+    matrix: &mut Matrix<'a, 'p, 'tcx>,
+    wildcard_arena: &'a TypedArena<DeconstructedPat<'p, 'tcx>>,
     is_top_level: bool,
 ) -> WitnessMatrix<'tcx> {
     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:?}");
-    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.
     let place_validity = matrix.place_validity[0];
@@ -1224,7 +1236,7 @@ fn compute_exhaustiveness_and_usefulness<'p, 'tcx>(
         // Dig into rows that match `ctor`.
         let mut spec_matrix = matrix.specialize_constructor(pcx, &ctor);
         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 {
@@ -1286,15 +1298,17 @@ pub struct UsefulnessReport<'p, 'tcx> {
 }
 
 /// 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>(
     cx: &MatchCheckCtxt<'p, 'tcx>,
     arms: &[MatchArm<'p, 'tcx>],
     scrut_ty: Ty<'tcx>,
+    wildcard_arena: &TypedArena<DeconstructedPat<'p, 'tcx>>,
 ) -> UsefulnessReport<'p, 'tcx> {
     let scrut_validity = ValidityConstraint::from_bool(cx.known_valid_scrutinee);
-    let mut matrix = Matrix::new(cx, arms, scrut_ty, scrut_validity);
-    let non_exhaustiveness_witnesses = compute_exhaustiveness_and_usefulness(cx, &mut matrix, true);
+    let mut matrix = Matrix::new(wildcard_arena, arms, scrut_ty, scrut_validity);
+    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 arm_usefulness: Vec<_> = arms