Skip to content

avoid some usages of &mut P<T> in AST visitors #141636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ pub trait MutVisitor: Sized {
walk_use_tree(self, use_tree);
}

fn visit_foreign_item(&mut self, ni: &mut P<ForeignItem>) {
fn visit_foreign_item(&mut self, ni: &mut ForeignItem) {
walk_item(self, ni);
}

fn flat_map_foreign_item(&mut self, ni: P<ForeignItem>) -> SmallVec<[P<ForeignItem>; 1]> {
walk_flat_map_foreign_item(self, ni)
}

fn visit_item(&mut self, i: &mut P<Item>) {
fn visit_item(&mut self, i: &mut Item) {
walk_item(self, i);
}

Expand All @@ -105,7 +105,7 @@ pub trait MutVisitor: Sized {
walk_flat_map_field_def(self, fd)
}

fn visit_assoc_item(&mut self, i: &mut P<AssocItem>, ctxt: AssocCtxt) {
fn visit_assoc_item(&mut self, i: &mut AssocItem, ctxt: AssocCtxt) {
walk_assoc_item(self, i, ctxt)
}

Expand All @@ -117,11 +117,11 @@ pub trait MutVisitor: Sized {
walk_flat_map_assoc_item(self, i, ctxt)
}

fn visit_contract(&mut self, c: &mut P<FnContract>) {
fn visit_contract(&mut self, c: &mut FnContract) {
walk_contract(self, c);
}

fn visit_fn_decl(&mut self, d: &mut P<FnDecl>) {
fn visit_fn_decl(&mut self, d: &mut FnDecl) {
walk_fn_decl(self, d);
}

Expand All @@ -138,7 +138,7 @@ pub trait MutVisitor: Sized {
walk_closure_binder(self, b);
}

fn visit_block(&mut self, b: &mut P<Block>) {
fn visit_block(&mut self, b: &mut Block) {
walk_block(self, b);
}

Expand Down Expand Up @@ -184,7 +184,7 @@ pub trait MutVisitor: Sized {
walk_ty(self, t);
}

fn visit_ty_pat(&mut self, t: &mut P<TyPat>) {
fn visit_ty_pat(&mut self, t: &mut TyPat) {
walk_ty_pat(self, t);
}

Expand Down Expand Up @@ -240,7 +240,7 @@ pub trait MutVisitor: Sized {
walk_parenthesized_parameter_data(self, p);
}

fn visit_local(&mut self, l: &mut P<Local>) {
fn visit_local(&mut self, l: &mut Local) {
walk_local(self, l);
}

Expand Down Expand Up @@ -507,8 +507,8 @@ fn walk_assoc_item_constraint<T: MutVisitor>(
vis.visit_span(span);
}

pub fn walk_ty<T: MutVisitor>(vis: &mut T, ty: &mut P<Ty>) {
let Ty { id, kind, span, tokens: _ } = ty.deref_mut();
pub fn walk_ty<T: MutVisitor>(vis: &mut T, ty: &mut Ty) {
let Ty { id, kind, span, tokens: _ } = ty;
vis.visit_id(id);
match kind {
TyKind::Err(_guar) => {}
Expand Down Expand Up @@ -559,8 +559,8 @@ pub fn walk_ty<T: MutVisitor>(vis: &mut T, ty: &mut P<Ty>) {
vis.visit_span(span);
}

pub fn walk_ty_pat<T: MutVisitor>(vis: &mut T, ty: &mut P<TyPat>) {
let TyPat { id, kind, span, tokens: _ } = ty.deref_mut();
pub fn walk_ty_pat<T: MutVisitor>(vis: &mut T, ty: &mut TyPat) {
let TyPat { id, kind, span, tokens: _ } = ty;
vis.visit_id(id);
match kind {
TyPatKind::Range(start, end, _include_end) => {
Expand Down Expand Up @@ -651,8 +651,8 @@ fn walk_parenthesized_parameter_data<T: MutVisitor>(vis: &mut T, args: &mut Pare
vis.visit_span(inputs_span);
}

fn walk_local<T: MutVisitor>(vis: &mut T, local: &mut P<Local>) {
let Local { id, super_, pat, ty, kind, span, colon_sp, attrs, tokens: _ } = local.deref_mut();
fn walk_local<T: MutVisitor>(vis: &mut T, local: &mut Local) {
let Local { id, super_, pat, ty, kind, span, colon_sp, attrs, tokens: _ } = local;
visit_opt(super_, |sp| vis.visit_span(sp));
vis.visit_id(id);
visit_attrs(vis, attrs);
Expand Down Expand Up @@ -789,8 +789,8 @@ fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
}
}

fn walk_contract<T: MutVisitor>(vis: &mut T, contract: &mut P<FnContract>) {
let FnContract { requires, ensures } = contract.deref_mut();
fn walk_contract<T: MutVisitor>(vis: &mut T, contract: &mut FnContract) {
let FnContract { requires, ensures } = contract;
if let Some(pred) = requires {
vis.visit_expr(pred);
}
Expand All @@ -799,8 +799,8 @@ fn walk_contract<T: MutVisitor>(vis: &mut T, contract: &mut P<FnContract>) {
}
}

fn walk_fn_decl<T: MutVisitor>(vis: &mut T, decl: &mut P<FnDecl>) {
let FnDecl { inputs, output } = decl.deref_mut();
fn walk_fn_decl<T: MutVisitor>(vis: &mut T, decl: &mut FnDecl) {
let FnDecl { inputs, output } = decl;
inputs.flat_map_in_place(|param| vis.flat_map_param(param));
vis.visit_fn_ret_ty(output);
}
Expand Down Expand Up @@ -999,8 +999,8 @@ pub fn walk_flat_map_expr_field<T: MutVisitor>(
smallvec![f]
}

pub fn walk_block<T: MutVisitor>(vis: &mut T, block: &mut P<Block>) {
let Block { id, stmts, rules: _, span, tokens: _ } = block.deref_mut();
pub fn walk_block<T: MutVisitor>(vis: &mut T, block: &mut Block) {
let Block { id, stmts, rules: _, span, tokens: _ } = block;
vis.visit_id(id);
stmts.flat_map_in_place(|stmt| vis.flat_map_stmt(stmt));
vis.visit_span(span);
Expand Down Expand Up @@ -1049,8 +1049,8 @@ pub fn walk_flat_map_assoc_item(
smallvec![item]
}

pub fn walk_pat<T: MutVisitor>(vis: &mut T, pat: &mut P<Pat>) {
let Pat { id, kind, span, tokens: _ } = pat.deref_mut();
pub fn walk_pat<T: MutVisitor>(vis: &mut T, pat: &mut Pat) {
let Pat { id, kind, span, tokens: _ } = pat;
vis.visit_id(id);
match kind {
PatKind::Err(_guar) => {}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,10 @@ macro_rules! common_visitor_and_walkers {

fn walk_item_ctxt<$($lt,)? V: $Visitor$(<$lt>)?, K: WalkItemKind>(
visitor: &mut V,
item: &$($mut P<Item<K>>)? $($lt Item<K>)?,
item: &$($mut)? $($lt)? Item<K>,
ctxt: K::Ctxt,
) $(-> <V as Visitor<$lt>>::Result)? {
let Item { attrs, id, kind, vis, span, tokens: _ } = &$($mut *)? *item;
let Item { attrs, id, kind, vis, span, tokens: _ } = item;
try_visit!(visit_id(visitor, id));
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_vis(vis));
Expand All @@ -417,14 +417,14 @@ macro_rules! common_visitor_and_walkers {

pub fn walk_item<$($lt,)? V: $Visitor$(<$lt>)?, K: WalkItemKind<Ctxt = ()>>(
visitor: &mut V,
item: &$($mut P<Item<K>>)? $($lt Item<K>)?,
item: &$($mut)? $($lt)? Item<K>,
) $(-> <V as Visitor<$lt>>::Result)? {
walk_item_ctxt(visitor, item, ())
}

pub fn walk_assoc_item<$($lt,)? V: $Visitor$(<$lt>)?>(
visitor: &mut V,
item: &$($mut P<AssocItem>)? $($lt AssocItem)?,
item: &$($mut)? $($lt)? AssocItem,
ctxt: AssocCtxt,
) $(-> <V as Visitor<$lt>>::Result)? {
walk_item_ctxt(visitor, item, ctxt)
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_builtin_macros/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
c.items.push(mk_main(&mut self.cx));
}

fn visit_item(&mut self, item: &mut P<ast::Item>) {
let item = &mut **item;

fn visit_item(&mut self, item: &mut ast::Item) {
if let Some(name) = get_test_name(&item) {
debug!("this is a test item");

Expand Down Expand Up @@ -193,7 +191,7 @@ struct EntryPointCleaner<'a> {
}

impl<'a> MutVisitor for EntryPointCleaner<'a> {
fn visit_item(&mut self, item: &mut P<ast::Item>) {
fn visit_item(&mut self, item: &mut ast::Item) {
self.depth += 1;
ast::mut_visit::walk_item(self, item);
self.depth -= 1;
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::iter;

use rustc_ast::ptr::P;
use rustc_ast::token::{Delimiter, Token, TokenKind};
use rustc_ast::tokenstream::{
AttrTokenStream, AttrTokenTree, LazyAttrTokenStream, Spacing, TokenTree,
Expand Down Expand Up @@ -433,7 +432,7 @@ impl<'a> StripUnconfigured<'a> {
}

#[instrument(level = "trace", skip(self))]
pub fn configure_expr(&self, expr: &mut P<ast::Expr>, method_receiver: bool) {
pub fn configure_expr(&self, expr: &mut ast::Expr, method_receiver: bool) {
if !method_receiver {
for attr in expr.attrs.iter() {
self.maybe_emit_expr_attr_err(attr);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2304,7 +2304,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
self.flat_map_node(AstNodeWrapper::new(node, OptExprTag))
}

fn visit_block(&mut self, node: &mut P<ast::Block>) {
fn visit_block(&mut self, node: &mut ast::Block) {
let orig_dir_ownership = mem::replace(
&mut self.cx.current_expansion.dir_ownership,
DirOwnership::UnownedViaBlock,
Expand Down
Loading