Skip to content

Commit

Permalink
Unrolled build for rust-lang#119307
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#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 0292eb9, and then simplified more lifetimes in `MatchVisitor`, which should make rust-lang#119233 a very simple PR!

r? Nadrieril
  • Loading branch information
rust-timer authored Dec 26, 2023
2 parents a75fed7 + 48d089a commit ebde888
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 104 deletions.
39 changes: 27 additions & 12 deletions compiler/rustc_middle/src/thir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ use super::{
PatKind, Stmt, StmtKind, Thir,
};

pub trait Visitor<'a, 'tcx: 'a>: Sized {
fn thir(&self) -> &'a Thir<'tcx>;
pub trait Visitor<'thir, 'tcx: 'thir>: Sized {
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);
}

fn visit_stmt(&mut self, stmt: &Stmt<'tcx>) {
fn visit_stmt(&mut self, stmt: &'thir Stmt<'tcx>) {
walk_stmt(self, stmt);
}

fn visit_block(&mut self, block: &Block) {
fn visit_block(&mut self, block: &'thir 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);
}

fn visit_pat(&mut self, pat: &Pat<'tcx>) {
fn visit_pat(&mut self, pat: &'thir Pat<'tcx>) {
walk_pat(self, pat);
}

Expand All @@ -36,7 +36,10 @@ pub trait Visitor<'a, 'tcx: 'a>: Sized {
// 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::*;
match expr.kind {
Scope { value, region_scope: _, lint_level: _ } => {
Expand Down Expand Up @@ -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 {
StmtKind::Expr { expr, scope: _ } => visitor.visit_expr(&visitor.thir()[*expr]),
StmtKind::Let {
Expand All @@ -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 {
visitor.visit_stmt(&visitor.thir()[stmt]);
}
Expand All @@ -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 {
Some(Guard::If(expr)) => visitor.visit_expr(&visitor.thir()[expr]),
Some(Guard::IfLet(ref pat, expr)) => {
Expand All @@ -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]);
}

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::*;
match &pat.kind {
AscribeUserType { subpattern, ascription: _ }
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for LayoutConstrainedPlaceVisitor<'a, 'tcx> {
self.thir
}

fn visit_expr(&mut self, expr: &Expr<'tcx>) {
fn visit_expr(&mut self, expr: &'a Expr<'tcx>) {
match expr.kind {
ExprKind::Field { lhs, .. } => {
if let ty::Adt(adt_def, _) = self.thir[lhs].ty.kind() {
Expand Down Expand Up @@ -206,7 +206,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
self.thir
}

fn visit_block(&mut self, block: &Block) {
fn visit_block(&mut self, block: &'a Block) {
match block.safety_mode {
// compiler-generated unsafe code should not count towards the usefulness of
// an outer unsafe block
Expand Down Expand Up @@ -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 {
match pat.kind {
// binding to a variable allows getting stuff out of variable
Expand Down Expand Up @@ -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?
match expr.kind {
ExprKind::Field { .. }
Expand Down
34 changes: 17 additions & 17 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ enum LetSource {
WhileLet,
}

struct MatchVisitor<'thir, 'p, 'tcx> {
struct MatchVisitor<'p, 'tcx> {
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
typeck_results: &'tcx ty::TypeckResults<'tcx>,
thir: &'thir Thir<'tcx>,
thir: &'p Thir<'tcx>,
lint_level: HirId,
let_source: LetSource,
pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>,
Expand All @@ -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
// appropriate.
impl<'thir, 'tcx> Visitor<'thir, 'tcx> for MatchVisitor<'thir, '_, 'tcx> {
fn thir(&self) -> &'thir Thir<'tcx> {
impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
fn thir(&self) -> &'p Thir<'tcx> {
self.thir
}

#[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| {
match arm.guard {
Some(Guard::If(expr)) => {
Expand All @@ -121,7 +121,7 @@ impl<'thir, 'tcx> Visitor<'thir, 'tcx> for MatchVisitor<'thir, '_, 'tcx> {
}

#[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 {
ExprKind::Scope { value, lint_level, .. } => {
self.with_lint_level(lint_level, |this| {
Expand Down Expand Up @@ -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));
}

fn visit_stmt(&mut self, stmt: &Stmt<'tcx>) {
fn visit_stmt(&mut self, stmt: &'p Stmt<'tcx>) {
match stmt.kind {
StmtKind::Let {
box ref pattern, initializer, else_block, lint_level, span, ..
Expand All @@ -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))]
fn with_let_source(&mut self, let_source: LetSource, f: impl FnOnce(&mut Self)) {
let old_let_source = self.let_source;
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
/// subexpressions we are not handling ourselves.
fn visit_land(
&mut self,
ex: &Expr<'tcx>,
ex: &'p Expr<'tcx>,
accumulator: &mut Vec<Option<(Span, RefutableFlag)>>,
) -> Result<(), ErrorGuaranteed> {
match ex.kind {
Expand All @@ -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.
fn visit_land_rhs(
&mut self,
ex: &Expr<'tcx>,
ex: &'p Expr<'tcx>,
) -> Result<Option<(Span, RefutableFlag)>, ErrorGuaranteed> {
match ex.kind {
ExprKind::Scope { value, lint_level, .. } => {
Expand All @@ -276,7 +276,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
fn lower_pattern(
&mut self,
cx: &MatchCheckCtxt<'p, 'tcx>,
pat: &Pat<'tcx>,
pat: &'p Pat<'tcx>,
) -> Result<&'p DeconstructedPat<'p, 'tcx>, ErrorGuaranteed> {
if let Err(err) = pat.pat_error_reported() {
self.error = Err(err);
Expand Down Expand Up @@ -395,7 +395,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
}

#[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);
let scrut = scrutinee.map(|id| &self.thir[id]);
if let LetSource::PlainLet = self.let_source {
Expand Down Expand Up @@ -547,7 +547,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {

fn analyze_binding(
&mut self,
pat: &Pat<'tcx>,
pat: &'p Pat<'tcx>,
refutability: RefutableFlag,
scrut: Option<&Expr<'tcx>>,
) -> Result<(MatchCheckCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> {
Expand All @@ -560,7 +560,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {

fn is_let_irrefutable(
&mut self,
pat: &Pat<'tcx>,
pat: &'p Pat<'tcx>,
scrut: Option<&Expr<'tcx>>,
) -> Result<RefutableFlag, ErrorGuaranteed> {
let (cx, report) = self.analyze_binding(pat, Refutable, scrut)?;
Expand All @@ -575,7 +575,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
#[instrument(level = "trace", skip(self))]
fn check_binding_is_irrefutable(
&mut self,
pat: &Pat<'tcx>,
pat: &'p Pat<'tcx>,
origin: &str,
scrut: Option<&Expr<'tcx>>,
sp: Option<Span>,
Expand Down Expand Up @@ -677,7 +677,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
/// - `x @ Some(ref mut? y)`.
///
/// 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`.
let PatKind::Binding { name, mode, ty, subpattern: Some(box ref sub), .. } = pat.kind else {
return;
Expand Down Expand Up @@ -772,7 +772,7 @@ fn check_borrow_conflicts_in_at_patterns<'tcx>(cx: &MatchVisitor<'_, '_, 'tcx>,
}

fn check_for_bindings_named_same_as_variants(
cx: &MatchVisitor<'_, '_, '_>,
cx: &MatchVisitor<'_, '_>,
pat: &Pat<'_>,
rf: RefutableFlag,
) {
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_pattern_analysis/src/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,9 @@ impl<Cx: TypeCx> ConstructorSet<Cx> {
#[instrument(level = "debug", skip(self, pcx, ctors), ret)]
pub(crate) fn split<'a>(
&self,
pcx: &PlaceCtxt<'_, '_, Cx>,
pcx: &PlaceCtxt<'a, '_, Cx>,
ctors: impl Iterator<Item = &'a Constructor<Cx>> + Clone,
) -> SplitConstructorSet<Cx>
where
Cx: 'a,
{
) -> SplitConstructorSet<Cx> {
let mut present: SmallVec<[_; 1]> = SmallVec::new();
// Empty constructors found missing.
let mut missing_empty = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_pattern_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub struct MatchCtxt<'a, 'p, Cx: TypeCx> {
/// The context for type information.
pub tycx: &'a Cx,
/// 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.
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_pattern_analysis/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ use crate::TypeCx;
///
/// This is not used in the main algorithm; only in lints.
#[derive(Debug)]
pub(crate) struct PatternColumn<'a, 'p, 'tcx> {
patterns: Vec<&'a DeconstructedPat<'p, 'tcx>>,
pub(crate) struct PatternColumn<'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 {
let mut patterns = Vec::with_capacity(arms.len());
for arm in arms {
Expand All @@ -48,7 +48,7 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
fn is_empty(&self) -> bool {
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 {
return None;
}
Expand All @@ -64,7 +64,7 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
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()
}

Expand All @@ -75,9 +75,9 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
/// which may change the lengths.
fn specialize(
&self,
pcx: &PlaceCtxt<'a, 'p, 'tcx>,
pcx: &PlaceCtxt<'_, 'p, 'tcx>,
ctor: &Constructor<'p, 'tcx>,
) -> Vec<PatternColumn<'a, 'p, 'tcx>> {
) -> Vec<PatternColumn<'p, 'tcx>> {
let arity = ctor.arity(pcx);
if arity == 0 {
return Vec::new();
Expand Down Expand Up @@ -115,7 +115,7 @@ impl<'a, 'p, 'tcx> PatternColumn<'a, 'p, 'tcx> {
#[instrument(level = "debug", skip(cx), ret)]
fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: MatchCtxt<'a, 'p, 'tcx>,
column: &PatternColumn<'a, 'p, 'tcx>,
column: &PatternColumn<'p, 'tcx>,
) -> Vec<WitnessPat<'p, 'tcx>> {
let Some(ty) = column.head_ty(cx) else {
return Vec::new();
Expand Down Expand Up @@ -163,7 +163,7 @@ fn collect_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
cx: MatchCtxt<'a, 'p, 'tcx>,
arms: &[MatchArm<'p, 'tcx>],
pat_column: &PatternColumn<'a, 'p, 'tcx>,
pat_column: &PatternColumn<'p, 'tcx>,
scrut_ty: Ty<'tcx>,
) {
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
Expand Down Expand Up @@ -216,7 +216,7 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
#[instrument(level = "debug", skip(cx))]
pub(crate) fn lint_overlapping_range_endpoints<'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 {
return;
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_pattern_analysis/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,17 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
self.data.as_ref()
}

pub fn iter_fields<'a>(
&'a self,
) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'a> {
pub fn iter_fields(&self) -> impl Iterator<Item = &'p DeconstructedPat<'p, Cx>> + Captures<'_> {
self.fields.iter()
}

/// 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>(
pub(crate) fn specialize(
&self,
pcx: &PlaceCtxt<'a, 'p, Cx>,
pcx: &PlaceCtxt<'_, 'p, Cx>,
other_ctor: &Constructor<Cx>,
) -> SmallVec<[&'a DeconstructedPat<'p, Cx>; 2]> {
) -> SmallVec<[&'p DeconstructedPat<'p, Cx>; 2]> {
let wildcard_sub_tys = || {
let tys = pcx.ctor_sub_tys(other_ctor);
tys.iter()
Expand Down Expand Up @@ -196,7 +194,7 @@ impl<Cx: TypeCx> WitnessPat<Cx> {
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()
}
}
Loading

0 comments on commit ebde888

Please sign in to comment.