1
Fork 0

Rollup merge of #119307 - compiler-errors:pat-lifetimes, r=Nadrieril

Clean up some lifetimes in `rustc_pattern_analysis`

This PR removes some redundant lifetimes. I figured out that we were shortening the lifetime of an arena-allocated `&'p DeconstructedPat<'p>` to `'a DeconstructedPat<'p>`, which forced us to carry both lifetimes when we could otherwise carry just one.

This PR also removes and elides some unnecessary lifetimes.

I also cherry-picked 0292eb9bb9b897f5c0926c6a8530877f67e7cc9b, and then simplified more lifetimes in `MatchVisitor`, which should make #119233 a very simple PR!

r? Nadrieril
This commit is contained in:
Michael Goulet 2023-12-26 13:29:14 -05:00 committed by GitHub
commit e1be642b41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 111 additions and 104 deletions

View file

@ -3,26 +3,26 @@ use super::{
PatKind, Stmt, StmtKind, Thir, PatKind, Stmt, StmtKind, Thir,
}; };
pub trait Visitor<'a, 'tcx: 'a>: Sized { pub trait Visitor<'thir, 'tcx: 'thir>: Sized {
fn thir(&self) -> &'a Thir<'tcx>; fn thir(&self) -> &'thir Thir<'tcx>;
fn visit_expr(&mut self, expr: &Expr<'tcx>) { fn visit_expr(&mut self, expr: &'thir Expr<'tcx>) {
walk_expr(self, expr); walk_expr(self, expr);
} }
fn visit_stmt(&mut self, stmt: &Stmt<'tcx>) { fn visit_stmt(&mut self, stmt: &'thir Stmt<'tcx>) {
walk_stmt(self, stmt); walk_stmt(self, stmt);
} }
fn visit_block(&mut self, block: &Block) { fn visit_block(&mut self, block: &'thir Block) {
walk_block(self, block); walk_block(self, block);
} }
fn visit_arm(&mut self, arm: &Arm<'tcx>) { fn visit_arm(&mut self, arm: &'thir Arm<'tcx>) {
walk_arm(self, arm); walk_arm(self, arm);
} }
fn visit_pat(&mut self, pat: &Pat<'tcx>) { fn visit_pat(&mut self, pat: &'thir Pat<'tcx>) {
walk_pat(self, pat); walk_pat(self, pat);
} }
@ -36,7 +36,10 @@ pub trait Visitor<'a, 'tcx: 'a>: Sized {
// other `visit*` functions. // other `visit*` functions.
} }
pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Expr<'tcx>) { pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor: &mut V,
expr: &'thir Expr<'tcx>,
) {
use ExprKind::*; use ExprKind::*;
match expr.kind { match expr.kind {
Scope { value, region_scope: _, lint_level: _ } => { Scope { value, region_scope: _, lint_level: _ } => {
@ -168,7 +171,10 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
} }
} }
pub fn walk_stmt<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, stmt: &Stmt<'tcx>) { pub fn walk_stmt<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor: &mut V,
stmt: &'thir Stmt<'tcx>,
) {
match &stmt.kind { match &stmt.kind {
StmtKind::Expr { expr, scope: _ } => visitor.visit_expr(&visitor.thir()[*expr]), StmtKind::Expr { expr, scope: _ } => visitor.visit_expr(&visitor.thir()[*expr]),
StmtKind::Let { StmtKind::Let {
@ -191,7 +197,10 @@ pub fn walk_stmt<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, stmt: &Stm
} }
} }
pub fn walk_block<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, block: &Block) { pub fn walk_block<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor: &mut V,
block: &'thir Block,
) {
for &stmt in &*block.stmts { for &stmt in &*block.stmts {
visitor.visit_stmt(&visitor.thir()[stmt]); visitor.visit_stmt(&visitor.thir()[stmt]);
} }
@ -200,7 +209,10 @@ pub fn walk_block<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, block: &B
} }
} }
pub fn walk_arm<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, arm: &Arm<'tcx>) { pub fn walk_arm<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor: &mut V,
arm: &'thir Arm<'tcx>,
) {
match arm.guard { match arm.guard {
Some(Guard::If(expr)) => visitor.visit_expr(&visitor.thir()[expr]), Some(Guard::If(expr)) => visitor.visit_expr(&visitor.thir()[expr]),
Some(Guard::IfLet(ref pat, expr)) => { Some(Guard::IfLet(ref pat, expr)) => {
@ -213,7 +225,10 @@ pub fn walk_arm<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, arm: &Arm<'
visitor.visit_expr(&visitor.thir()[arm.body]); visitor.visit_expr(&visitor.thir()[arm.body]);
} }
pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'tcx>) { pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor: &mut V,
pat: &'thir Pat<'tcx>,
) {
use PatKind::*; use PatKind::*;
match &pat.kind { match &pat.kind {
AscribeUserType { subpattern, ascription: _ } AscribeUserType { subpattern, ascription: _ }

View file

@ -175,7 +175,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for LayoutConstrainedPlaceVisitor<'a, 'tcx> {
self.thir self.thir
} }
fn visit_expr(&mut self, expr: &Expr<'tcx>) { fn visit_expr(&mut self, expr: &'a Expr<'tcx>) {
match expr.kind { match expr.kind {
ExprKind::Field { lhs, .. } => { ExprKind::Field { lhs, .. } => {
if let ty::Adt(adt_def, _) = self.thir[lhs].ty.kind() { if let ty::Adt(adt_def, _) = self.thir[lhs].ty.kind() {
@ -206,7 +206,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
self.thir self.thir
} }
fn visit_block(&mut self, block: &Block) { fn visit_block(&mut self, block: &'a Block) {
match block.safety_mode { match block.safety_mode {
// compiler-generated unsafe code should not count towards the usefulness of // compiler-generated unsafe code should not count towards the usefulness of
// an outer unsafe block // an outer unsafe block
@ -234,7 +234,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
} }
} }
fn visit_pat(&mut self, pat: &Pat<'tcx>) { fn visit_pat(&mut self, pat: &'a Pat<'tcx>) {
if self.in_union_destructure { if self.in_union_destructure {
match pat.kind { match pat.kind {
// binding to a variable allows getting stuff out of variable // binding to a variable allows getting stuff out of variable
@ -319,7 +319,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
} }
} }
fn visit_expr(&mut self, expr: &Expr<'tcx>) { fn visit_expr(&mut self, expr: &'a Expr<'tcx>) {
// could we be in the LHS of an assignment to a field? // could we be in the LHS of an assignment to a field?
match expr.kind { match expr.kind {
ExprKind::Field { .. } ExprKind::Field { .. }

View file

@ -75,11 +75,11 @@ enum LetSource {
WhileLet, WhileLet,
} }
struct MatchVisitor<'thir, 'p, 'tcx> { struct MatchVisitor<'p, 'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
typeck_results: &'tcx ty::TypeckResults<'tcx>, typeck_results: &'tcx ty::TypeckResults<'tcx>,
thir: &'thir Thir<'tcx>, thir: &'p Thir<'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>>,
@ -92,13 +92,13 @@ struct MatchVisitor<'thir, 'p, 'tcx> {
// Visitor for a thir body. This calls `check_match`, `check_let` and `check_let_chain` as // Visitor for a thir body. This calls `check_match`, `check_let` and `check_let_chain` as
// appropriate. // appropriate.
impl<'thir, 'tcx> Visitor<'thir, 'tcx> for MatchVisitor<'thir, '_, 'tcx> { impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
fn thir(&self) -> &'thir Thir<'tcx> { fn thir(&self) -> &'p Thir<'tcx> {
self.thir self.thir
} }
#[instrument(level = "trace", skip(self))] #[instrument(level = "trace", skip(self))]
fn visit_arm(&mut self, arm: &Arm<'tcx>) { fn visit_arm(&mut self, arm: &'p Arm<'tcx>) {
self.with_lint_level(arm.lint_level, |this| { self.with_lint_level(arm.lint_level, |this| {
match arm.guard { match arm.guard {
Some(Guard::If(expr)) => { Some(Guard::If(expr)) => {
@ -121,7 +121,7 @@ impl<'thir, 'tcx> Visitor<'thir, 'tcx> for MatchVisitor<'thir, '_, 'tcx> {
} }
#[instrument(level = "trace", skip(self))] #[instrument(level = "trace", skip(self))]
fn visit_expr(&mut self, ex: &Expr<'tcx>) { fn visit_expr(&mut self, ex: &'p Expr<'tcx>) {
match ex.kind { match ex.kind {
ExprKind::Scope { value, lint_level, .. } => { ExprKind::Scope { value, lint_level, .. } => {
self.with_lint_level(lint_level, |this| { self.with_lint_level(lint_level, |this| {
@ -174,7 +174,7 @@ impl<'thir, 'tcx> Visitor<'thir, 'tcx> for MatchVisitor<'thir, '_, 'tcx> {
self.with_let_source(LetSource::None, |this| visit::walk_expr(this, ex)); self.with_let_source(LetSource::None, |this| visit::walk_expr(this, ex));
} }
fn visit_stmt(&mut self, stmt: &Stmt<'tcx>) { fn visit_stmt(&mut self, stmt: &'p Stmt<'tcx>) {
match stmt.kind { match stmt.kind {
StmtKind::Let { StmtKind::Let {
box ref pattern, initializer, else_block, lint_level, span, .. box ref pattern, initializer, else_block, lint_level, span, ..
@ -195,7 +195,7 @@ impl<'thir, 'tcx> Visitor<'thir, 'tcx> for MatchVisitor<'thir, '_, 'tcx> {
} }
} }
impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> { impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
#[instrument(level = "trace", skip(self, f))] #[instrument(level = "trace", skip(self, f))]
fn with_let_source(&mut self, let_source: LetSource, f: impl FnOnce(&mut Self)) { fn with_let_source(&mut self, let_source: LetSource, f: impl FnOnce(&mut Self)) {
let old_let_source = self.let_source; let old_let_source = self.let_source;
@ -224,7 +224,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
/// subexpressions we are not handling ourselves. /// subexpressions we are not handling ourselves.
fn visit_land( fn visit_land(
&mut self, &mut self,
ex: &Expr<'tcx>, ex: &'p Expr<'tcx>,
accumulator: &mut Vec<Option<(Span, RefutableFlag)>>, accumulator: &mut Vec<Option<(Span, RefutableFlag)>>,
) -> Result<(), ErrorGuaranteed> { ) -> Result<(), ErrorGuaranteed> {
match ex.kind { match ex.kind {
@ -251,7 +251,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
/// expression. This must call `visit_expr` on the subexpressions we are not handling ourselves. /// expression. This must call `visit_expr` on the subexpressions we are not handling ourselves.
fn visit_land_rhs( fn visit_land_rhs(
&mut self, &mut self,
ex: &Expr<'tcx>, ex: &'p Expr<'tcx>,
) -> Result<Option<(Span, RefutableFlag)>, ErrorGuaranteed> { ) -> Result<Option<(Span, RefutableFlag)>, ErrorGuaranteed> {
match ex.kind { match ex.kind {
ExprKind::Scope { value, lint_level, .. } => { ExprKind::Scope { value, lint_level, .. } => {
@ -276,7 +276,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
fn lower_pattern( fn lower_pattern(
&mut self, &mut self,
cx: &MatchCheckCtxt<'p, 'tcx>, cx: &MatchCheckCtxt<'p, 'tcx>,
pat: &Pat<'tcx>, pat: &'p Pat<'tcx>,
) -> Result<&'p DeconstructedPat<'p, 'tcx>, ErrorGuaranteed> { ) -> Result<&'p DeconstructedPat<'p, 'tcx>, ErrorGuaranteed> {
if let Err(err) = pat.pat_error_reported() { if let Err(err) = pat.pat_error_reported() {
self.error = Err(err); self.error = Err(err);
@ -395,7 +395,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
} }
#[instrument(level = "trace", skip(self))] #[instrument(level = "trace", skip(self))]
fn check_let(&mut self, pat: &Pat<'tcx>, scrutinee: Option<ExprId>, span: Span) { fn check_let(&mut self, pat: &'p Pat<'tcx>, scrutinee: Option<ExprId>, span: Span) {
assert!(self.let_source != LetSource::None); assert!(self.let_source != LetSource::None);
let scrut = scrutinee.map(|id| &self.thir[id]); let scrut = scrutinee.map(|id| &self.thir[id]);
if let LetSource::PlainLet = self.let_source { if let LetSource::PlainLet = self.let_source {
@ -547,7 +547,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
fn analyze_binding( fn analyze_binding(
&mut self, &mut self,
pat: &Pat<'tcx>, pat: &'p Pat<'tcx>,
refutability: RefutableFlag, refutability: RefutableFlag,
scrut: Option<&Expr<'tcx>>, scrut: Option<&Expr<'tcx>>,
) -> Result<(MatchCheckCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> { ) -> Result<(MatchCheckCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> {
@ -560,7 +560,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
fn is_let_irrefutable( fn is_let_irrefutable(
&mut self, &mut self,
pat: &Pat<'tcx>, pat: &'p Pat<'tcx>,
scrut: Option<&Expr<'tcx>>, scrut: Option<&Expr<'tcx>>,
) -> Result<RefutableFlag, ErrorGuaranteed> { ) -> Result<RefutableFlag, ErrorGuaranteed> {
let (cx, report) = self.analyze_binding(pat, Refutable, scrut)?; let (cx, report) = self.analyze_binding(pat, Refutable, scrut)?;
@ -575,7 +575,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
#[instrument(level = "trace", skip(self))] #[instrument(level = "trace", skip(self))]
fn check_binding_is_irrefutable( fn check_binding_is_irrefutable(
&mut self, &mut self,
pat: &Pat<'tcx>, pat: &'p Pat<'tcx>,
origin: &str, origin: &str,
scrut: Option<&Expr<'tcx>>, scrut: Option<&Expr<'tcx>>,
sp: Option<Span>, sp: Option<Span>,
@ -677,7 +677,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
/// - `x @ Some(ref mut? y)`. /// - `x @ Some(ref mut? y)`.
/// ///
/// This analysis is *not* subsumed by NLL. /// This analysis is *not* subsumed by NLL.
fn check_borrow_conflicts_in_at_patterns<'tcx>(cx: &MatchVisitor<'_, '_, 'tcx>, pat: &Pat<'tcx>) { fn check_borrow_conflicts_in_at_patterns<'tcx>(cx: &MatchVisitor<'_, 'tcx>, pat: &Pat<'tcx>) {
// Extract `sub` in `binding @ sub`. // Extract `sub` in `binding @ sub`.
let PatKind::Binding { name, mode, ty, subpattern: Some(box ref sub), .. } = pat.kind else { let PatKind::Binding { name, mode, ty, subpattern: Some(box ref sub), .. } = pat.kind else {
return; return;
@ -772,7 +772,7 @@ fn check_borrow_conflicts_in_at_patterns<'tcx>(cx: &MatchVisitor<'_, '_, 'tcx>,
} }
fn check_for_bindings_named_same_as_variants( fn check_for_bindings_named_same_as_variants(
cx: &MatchVisitor<'_, '_, '_>, cx: &MatchVisitor<'_, '_>,
pat: &Pat<'_>, pat: &Pat<'_>,
rf: RefutableFlag, rf: RefutableFlag,
) { ) {

View file

@ -861,12 +861,9 @@ impl<Cx: TypeCx> ConstructorSet<Cx> {
#[instrument(level = "debug", skip(self, pcx, ctors), ret)] #[instrument(level = "debug", skip(self, pcx, ctors), ret)]
pub(crate) fn split<'a>( pub(crate) fn split<'a>(
&self, &self,
pcx: &PlaceCtxt<'_, '_, Cx>, pcx: &PlaceCtxt<'a, '_, Cx>,
ctors: impl Iterator<Item = &'a Constructor<Cx>> + Clone, ctors: impl Iterator<Item = &'a Constructor<Cx>> + Clone,
) -> SplitConstructorSet<Cx> ) -> SplitConstructorSet<Cx> {
where
Cx: 'a,
{
let mut present: SmallVec<[_; 1]> = SmallVec::new(); let mut present: SmallVec<[_; 1]> = SmallVec::new();
// Empty constructors found missing. // Empty constructors found missing.
let mut missing_empty = Vec::new(); let mut missing_empty = Vec::new();

View file

@ -91,7 +91,7 @@ pub struct MatchCtxt<'a, 'p, Cx: TypeCx> {
/// The context for type information. /// The context for type information.
pub tycx: &'a Cx, pub tycx: &'a Cx,
/// An arena to store the wildcards we produce during analysis. /// An arena to store the wildcards we produce during analysis.
pub wildcard_arena: &'a TypedArena<DeconstructedPat<'p, Cx>>, pub wildcard_arena: &'p TypedArena<DeconstructedPat<'p, Cx>>,
} }
/// The arm of a match expression. /// The arm of a match expression.

View file

@ -28,11 +28,11 @@ use crate::TypeCx;
/// ///
/// 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<'a, 'p, 'tcx> { pub(crate) struct PatternColumn<'p, 'tcx> {
patterns: Vec<&'a DeconstructedPat<'p, 'tcx>>, patterns: Vec<&'p DeconstructedPat<'p, 'tcx>>,
} }
impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> { impl<'p, 'tcx> PatternColumn<'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 {
@ -48,7 +48,7 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
self.patterns.is_empty() self.patterns.is_empty()
} }
fn head_ty(&self, cx: MatchCtxt<'a, 'p, 'tcx>) -> Option<Ty<'tcx>> { fn head_ty(&self, cx: MatchCtxt<'_, 'p, 'tcx>) -> Option<Ty<'tcx>> {
if self.patterns.len() == 0 { if self.patterns.len() == 0 {
return None; return None;
} }
@ -64,7 +64,7 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
pcx.ctors_for_ty().split(pcx, column_ctors) pcx.ctors_for_ty().split(pcx, column_ctors)
} }
fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, 'tcx>> + Captures<'b> { fn iter(&self) -> impl Iterator<Item = &'p DeconstructedPat<'p, 'tcx>> + Captures<'_> {
self.patterns.iter().copied() self.patterns.iter().copied()
} }
@ -75,9 +75,9 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
/// which may change the lengths. /// which may change the lengths.
fn specialize( fn specialize(
&self, &self,
pcx: &PlaceCtxt<'a, 'p, 'tcx>, pcx: &PlaceCtxt<'_, 'p, 'tcx>,
ctor: &Constructor<'p, 'tcx>, ctor: &Constructor<'p, 'tcx>,
) -> Vec<PatternColumn<'a, 'p, 'tcx>> { ) -> Vec<PatternColumn<'p, 'tcx>> {
let arity = ctor.arity(pcx); let arity = ctor.arity(pcx);
if arity == 0 { if arity == 0 {
return Vec::new(); return Vec::new();
@ -115,7 +115,7 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
#[instrument(level = "debug", skip(cx), ret)] #[instrument(level = "debug", skip(cx), ret)]
fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>( fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: MatchCtxt<'a, 'p, 'tcx>, cx: MatchCtxt<'a, 'p, 'tcx>,
column: &PatternColumn<'a, 'p, 'tcx>, column: &PatternColumn<'p, 'tcx>,
) -> Vec<WitnessPat<'p, 'tcx>> { ) -> Vec<WitnessPat<'p, 'tcx>> {
let Some(ty) = column.head_ty(cx) else { let Some(ty) = column.head_ty(cx) else {
return Vec::new(); return Vec::new();
@ -163,7 +163,7 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>( pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: MatchCtxt<'a, 'p, 'tcx>, cx: MatchCtxt<'a, 'p, 'tcx>,
arms: &[MatchArm<'p, 'tcx>], arms: &[MatchArm<'p, 'tcx>],
pat_column: &PatternColumn<'a, 'p, 'tcx>, pat_column: &PatternColumn<'p, 'tcx>,
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
) { ) {
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx; let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
@ -216,7 +216,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
#[instrument(level = "debug", skip(cx))] #[instrument(level = "debug", skip(cx))]
pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>( pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>(
cx: MatchCtxt<'a, 'p, 'tcx>, cx: MatchCtxt<'a, 'p, 'tcx>,
column: &PatternColumn<'a, 'p, 'tcx>, column: &PatternColumn<'p, 'tcx>,
) { ) {
let Some(ty) = column.head_ty(cx) else { let Some(ty) = column.head_ty(cx) else {
return; return;

View file

@ -71,19 +71,17 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
self.data.as_ref() self.data.as_ref()
} }
pub fn iter_fields<'a>( pub fn iter_fields(&self) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'_> {
&'a self,
) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'a> {
self.fields.iter() self.fields.iter()
} }
/// 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(
&self, &self,
pcx: &PlaceCtxt<'a, 'p, Cx>, pcx: &PlaceCtxt<'_, 'p, Cx>,
other_ctor: &Constructor<Cx>, other_ctor: &Constructor<Cx>,
) -> SmallVec<[&'a DeconstructedPat<'p, Cx>; 2]> { ) -> SmallVec<[&'p DeconstructedPat<'p, Cx>; 2]> {
let wildcard_sub_tys = || { let wildcard_sub_tys = || {
let tys = pcx.ctor_sub_tys(other_ctor); let tys = pcx.ctor_sub_tys(other_ctor);
tys.iter() tys.iter()
@ -196,7 +194,7 @@ impl<Cx: TypeCx> WitnessPat<Cx> {
self.ty self.ty
} }
pub fn iter_fields<'a>(&'a self) -> impl Iterator<Item = &'a WitnessPat<Cx>> { pub fn iter_fields(&self) -> impl Iterator<Item = &WitnessPat<Cx>> {
self.fields.iter() self.fields.iter()
} }
} }

View file

@ -128,11 +128,11 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
// 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.
pub(crate) fn list_variant_nonhidden_fields<'a>( pub(crate) fn list_variant_nonhidden_fields(
&'a self, &self,
ty: Ty<'tcx>, ty: Ty<'tcx>,
variant: &'a VariantDef, variant: &'tcx VariantDef,
) -> impl Iterator<Item = (FieldIdx, Ty<'tcx>)> + Captures<'p> + Captures<'a> { ) -> impl Iterator<Item = (FieldIdx, Ty<'tcx>)> + Captures<'p> + Captures<'_> {
let cx = self; let cx = self;
let ty::Adt(adt, args) = ty.kind() else { bug!() }; let ty::Adt(adt, args) = ty.kind() else { bug!() };
// Whether we must not match the fields of this variant exhaustively. // Whether we must not match the fields of this variant exhaustively.
@ -399,7 +399,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
/// Note: the input patterns must have been lowered through /// Note: the input patterns must have been lowered through
/// `rustc_mir_build::thir::pattern::check_match::MatchVisitor::lower_pattern`. /// `rustc_mir_build::thir::pattern::check_match::MatchVisitor::lower_pattern`.
pub fn lower_pat(&self, pat: &Pat<'tcx>) -> DeconstructedPat<'p, 'tcx> { pub fn lower_pat(&self, pat: &'p Pat<'tcx>) -> DeconstructedPat<'p, 'tcx> {
let singleton = |pat| std::slice::from_ref(self.pattern_arena.alloc(pat)); let singleton = |pat| std::slice::from_ref(self.pattern_arena.alloc(pat));
let cx = self; let cx = self;
let ctor; let ctor;

View file

@ -821,22 +821,21 @@ impl fmt::Display for ValidityConstraint {
/// Represents a pattern-tuple under investigation. /// Represents a pattern-tuple under investigation.
// The three lifetimes are: // The three lifetimes are:
// - 'a allocated by us
// - 'p coming from the input // - 'p coming from the input
// - Cx global compilation context // - Cx global compilation context
#[derive(derivative::Derivative)] #[derive(derivative::Derivative)]
#[derivative(Clone(bound = ""))] #[derivative(Clone(bound = ""))]
struct PatStack<'a, 'p, Cx: TypeCx> { struct PatStack<'p, Cx: TypeCx> {
// 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<[&'a DeconstructedPat<'p, Cx>; 2]>, pats: SmallVec<[&'p DeconstructedPat<'p, Cx>; 2]>,
/// Sometimes we know that as far as this row is concerned, the current case is already handled /// Sometimes we know that as far as this row is concerned, the current case is already handled
/// by a different, more general, case. When the case is irrelevant for all rows this allows us /// by a different, more general, case. When the case is irrelevant for all rows this allows us
/// to skip a case entirely. This is purely an optimization. See at the top for details. /// to skip a case entirely. This is purely an optimization. See at the top for details.
relevant: bool, relevant: bool,
} }
impl<'a, 'p, Cx: TypeCx> PatStack<'a, 'p, Cx> { impl<'p, Cx: TypeCx> PatStack<'p, Cx> {
fn from_pattern(pat: &'a DeconstructedPat<'p, Cx>) -> Self { fn from_pattern(pat: &'p DeconstructedPat<'p, Cx>) -> Self {
PatStack { pats: smallvec![pat], relevant: true } PatStack { pats: smallvec![pat], relevant: true }
} }
@ -848,17 +847,17 @@ impl<'a, 'p, Cx: TypeCx> PatStack<'a, 'p, Cx> {
self.pats.len() self.pats.len()
} }
fn head(&self) -> &'a DeconstructedPat<'p, Cx> { fn head(&self) -> &'p DeconstructedPat<'p, Cx> {
self.pats[0] self.pats[0]
} }
fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, Cx>> + Captures<'b> { fn iter(&self) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'_> {
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<'b>(&'b self) -> impl Iterator<Item = PatStack<'a, 'p, Cx>> + Captures<'b> { fn expand_or_pat(&self) -> impl Iterator<Item = PatStack<'p, Cx>> + Captures<'_> {
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;
@ -870,10 +869,10 @@ impl<'a, 'p, Cx: TypeCx> PatStack<'a, 'p, Cx> {
/// 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: &PlaceCtxt<'a, 'p, Cx>, pcx: &PlaceCtxt<'_, 'p, Cx>,
ctor: &Constructor<Cx>, ctor: &Constructor<Cx>,
ctor_is_relevant: bool, ctor_is_relevant: bool,
) -> PatStack<'a, 'p, Cx> { ) -> PatStack<'p, Cx> {
// 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);
@ -886,7 +885,7 @@ impl<'a, 'p, Cx: TypeCx> PatStack<'a, 'p, Cx> {
} }
} }
impl<'a, 'p, Cx: TypeCx> fmt::Debug for PatStack<'a, 'p, Cx> { impl<'p, Cx: TypeCx> fmt::Debug for PatStack<'p, Cx> {
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, "+")?;
@ -899,9 +898,9 @@ impl<'a, 'p, Cx: TypeCx> fmt::Debug for PatStack<'a, 'p, Cx> {
/// A row of the matrix. /// A row of the matrix.
#[derive(Clone)] #[derive(Clone)]
struct MatrixRow<'a, 'p, Cx: TypeCx> { struct MatrixRow<'p, Cx: TypeCx> {
// The patterns in the row. // The patterns in the row.
pats: PatStack<'a, 'p, Cx>, pats: PatStack<'p, Cx>,
/// 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
@ -914,7 +913,7 @@ struct MatrixRow<'a, 'p, Cx: TypeCx> {
useful: bool, useful: bool,
} }
impl<'a, 'p, Cx: TypeCx> MatrixRow<'a, 'p, Cx> { impl<'p, Cx: TypeCx> MatrixRow<'p, Cx> {
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
self.pats.is_empty() self.pats.is_empty()
} }
@ -923,17 +922,17 @@ impl<'a, 'p, Cx: TypeCx> MatrixRow<'a, 'p, Cx> {
self.pats.len() self.pats.len()
} }
fn head(&self) -> &'a DeconstructedPat<'p, Cx> { fn head(&self) -> &'p DeconstructedPat<'p, Cx> {
self.pats.head() self.pats.head()
} }
fn iter<'b>(&'b self) -> impl Iterator<Item = &'a DeconstructedPat<'p, Cx>> + Captures<'b> { fn iter(&self) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'_> {
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<'b>(&'b self) -> impl Iterator<Item = MatrixRow<'a, 'p, Cx>> + Captures<'b> { fn expand_or_pat(&self) -> impl Iterator<Item = MatrixRow<'p, Cx>> + Captures<'_> {
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,
@ -946,11 +945,11 @@ impl<'a, 'p, Cx: TypeCx> MatrixRow<'a, 'p, Cx> {
/// 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: &PlaceCtxt<'a, 'p, Cx>, pcx: &PlaceCtxt<'_, 'p, Cx>,
ctor: &Constructor<Cx>, ctor: &Constructor<Cx>,
ctor_is_relevant: bool, ctor_is_relevant: bool,
parent_row: usize, parent_row: usize,
) -> MatrixRow<'a, 'p, Cx> { ) -> MatrixRow<'p, Cx> {
MatrixRow { MatrixRow {
pats: self.pats.pop_head_constructor(pcx, ctor, ctor_is_relevant), pats: self.pats.pop_head_constructor(pcx, ctor, ctor_is_relevant),
parent_row, parent_row,
@ -960,7 +959,7 @@ impl<'a, 'p, Cx: TypeCx> MatrixRow<'a, 'p, Cx> {
} }
} }
impl<'a, 'p, Cx: TypeCx> fmt::Debug for MatrixRow<'a, 'p, Cx> { impl<'p, Cx: TypeCx> fmt::Debug for MatrixRow<'p, Cx> {
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)
} }
@ -977,22 +976,22 @@ impl<'a, 'p, Cx: TypeCx> fmt::Debug for MatrixRow<'a, 'p, Cx> {
/// 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<'a, 'p, Cx: TypeCx> { struct Matrix<'p, Cx: TypeCx> {
/// 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<'a, 'p, Cx>>, rows: Vec<MatrixRow<'p, Cx>>,
/// 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<'a, 'p, Cx>, wildcard_row: PatStack<'p, Cx>,
/// 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<'a, 'p, Cx: TypeCx> Matrix<'a, 'p, Cx> { impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
/// 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<'a, 'p, Cx>) { fn expand_and_push(&mut self, row: MatrixRow<'p, Cx>) {
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() {
@ -1005,8 +1004,8 @@ impl<'a, 'p, Cx: TypeCx> Matrix<'a, 'p, Cx> {
/// Build a new matrix from an iterator of `MatchArm`s. /// Build a new matrix from an iterator of `MatchArm`s.
fn new( fn new(
wildcard_arena: &'a TypedArena<DeconstructedPat<'p, Cx>>, wildcard_arena: &'p TypedArena<DeconstructedPat<'p, Cx>>,
arms: &'a [MatchArm<'p, Cx>], arms: &[MatchArm<'p, Cx>],
scrut_ty: Cx::Ty, scrut_ty: Cx::Ty,
scrut_validity: ValidityConstraint, scrut_validity: ValidityConstraint,
) -> Self { ) -> Self {
@ -1029,7 +1028,7 @@ impl<'a, 'p, Cx: TypeCx> Matrix<'a, 'p, Cx> {
matrix matrix
} }
fn head_ty(&self, mcx: MatchCtxt<'a, 'p, Cx>) -> Option<Cx::Ty> { fn head_ty(&self, mcx: MatchCtxt<'_, 'p, Cx>) -> Option<Cx::Ty> {
if self.column_count() == 0 { if self.column_count() == 0 {
return None; return None;
} }
@ -1042,33 +1041,31 @@ impl<'a, 'p, Cx: TypeCx> Matrix<'a, 'p, Cx> {
self.wildcard_row.len() self.wildcard_row.len()
} }
fn rows<'b>( fn rows(
&'b self, &self,
) -> impl Iterator<Item = &'b MatrixRow<'a, 'p, Cx>> + Clone + DoubleEndedIterator + ExactSizeIterator ) -> impl Iterator<Item = &MatrixRow<'p, Cx>> + Clone + DoubleEndedIterator + ExactSizeIterator
{ {
self.rows.iter() self.rows.iter()
} }
fn rows_mut<'b>( fn rows_mut(
&'b mut self, &mut self,
) -> impl Iterator<Item = &'b mut MatrixRow<'a, 'p, Cx>> + DoubleEndedIterator + ExactSizeIterator ) -> impl Iterator<Item = &mut MatrixRow<'p, Cx>> + 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<'b>( fn heads(&self) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Clone + Captures<'_> {
&'b self,
) -> impl Iterator<Item = &'b DeconstructedPat<'p, Cx>> + 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: &PlaceCtxt<'a, 'p, Cx>, pcx: &PlaceCtxt<'_, 'p, Cx>,
ctor: &Constructor<Cx>, ctor: &Constructor<Cx>,
ctor_is_relevant: bool, ctor_is_relevant: bool,
) -> Matrix<'a, 'p, Cx> { ) -> Matrix<'p, Cx> {
let wildcard_row = self.wildcard_row.pop_head_constructor(pcx, ctor, ctor_is_relevant); let wildcard_row = self.wildcard_row.pop_head_constructor(pcx, ctor, ctor_is_relevant);
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)
@ -1097,7 +1094,7 @@ impl<'a, 'p, Cx: TypeCx> Matrix<'a, 'p, Cx> {
/// + _ + [_, _, tail @ ..] + /// + _ + [_, _, tail @ ..] +
/// | ✓ | ? | // column validity /// | ✓ | ? | // column validity
/// ``` /// ```
impl<'a, 'p, Cx: TypeCx> fmt::Debug for Matrix<'a, 'p, Cx> { impl<'p, Cx: TypeCx> fmt::Debug for Matrix<'p, Cx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\n")?; write!(f, "\n")?;
@ -1336,7 +1333,7 @@ impl<Cx: TypeCx> WitnessMatrix<Cx> {
#[instrument(level = "debug", skip(mcx, is_top_level), ret)] #[instrument(level = "debug", skip(mcx, is_top_level), ret)]
fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>( fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
mcx: MatchCtxt<'a, 'p, Cx>, mcx: MatchCtxt<'a, 'p, Cx>,
matrix: &mut Matrix<'a, 'p, Cx>, matrix: &mut Matrix<'p, Cx>,
is_top_level: bool, is_top_level: bool,
) -> WitnessMatrix<Cx> { ) -> WitnessMatrix<Cx> {
debug_assert!(matrix.rows().all(|r| r.len() == matrix.column_count())); debug_assert!(matrix.rows().all(|r| r.len() == matrix.column_count()));

View file

@ -379,7 +379,7 @@ impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
} }
#[instrument(skip(self), level = "debug")] #[instrument(skip(self), level = "debug")]
fn visit_expr(&mut self, expr: &thir::Expr<'tcx>) { fn visit_expr(&mut self, expr: &'a thir::Expr<'tcx>) {
self.is_poly |= self.expr_is_poly(expr); self.is_poly |= self.expr_is_poly(expr);
if !self.is_poly { if !self.is_poly {
visit::walk_expr(self, expr) visit::walk_expr(self, expr)
@ -387,7 +387,7 @@ impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
} }
#[instrument(skip(self), level = "debug")] #[instrument(skip(self), level = "debug")]
fn visit_pat(&mut self, pat: &thir::Pat<'tcx>) { fn visit_pat(&mut self, pat: &'a thir::Pat<'tcx>) {
self.is_poly |= self.pat_is_poly(pat); self.is_poly |= self.pat_is_poly(pat);
if !self.is_poly { if !self.is_poly {
visit::walk_pat(self, pat); visit::walk_pat(self, pat);