Skip to content

Commit

Permalink
thir::Visitor only needs to visit &'thir data
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Dec 24, 2023
1 parent f736079 commit 0292eb9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 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
20 changes: 10 additions & 10 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'thir, 'tcx> Visitor<'thir, 'tcx> for MatchVisitor<'thir, '_, 'tcx> {
}

#[instrument(level = "trace", skip(self))]
fn visit_arm(&mut self, arm: &Arm<'tcx>) {
fn visit_arm(&mut self, arm: &'thir 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: &'thir 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: &'thir Stmt<'tcx>) {
match stmt.kind {
StmtKind::Let {
box ref pattern, initializer, else_block, lint_level, span, ..
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: &'thir 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: &'thir 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: &'thir 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: &'thir 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: &'thir 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: &'thir 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: &'thir Pat<'tcx>,
origin: &str,
scrut: Option<&Expr<'tcx>>,
sp: Option<Span>,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ty_utils/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,15 @@ impl<'a, 'tcx> visit::Visitor<'a, 'tcx> for IsThirPolymorphic<'a, 'tcx> {
}

#[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);
if !self.is_poly {
visit::walk_expr(self, expr)
}
}

#[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);
if !self.is_poly {
visit::walk_pat(self, pat);
Expand Down

0 comments on commit 0292eb9

Please sign in to comment.