Skip to content

Commit

Permalink
Avoid passing state that will not be visited
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jul 22, 2024
1 parent 91b26fc commit e9f32d0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 66 deletions.
69 changes: 16 additions & 53 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::ast::*;
use crate::ptr::P;
use crate::token::{self, Token};
use crate::tokenstream::*;
use crate::visit::{AssocCtxt, BoundKind, FnCtxt};
use crate::visit::{AssocCtxt, BoundKind};

use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
use rustc_data_structures::stack::ensure_sufficient_stack;
Expand All @@ -36,14 +36,7 @@ impl<A: Array> ExpectOne<A> for SmallVec<A> {
}

pub trait WalkItemKind {
fn walk(
&mut self,
ctxt: Option<AssocCtxt>,
ident: Ident,
span: Span,
id: NodeId,
visitor: &mut impl MutVisitor,
);
fn walk(&mut self, span: Span, id: NodeId, visitor: &mut impl MutVisitor);
}

pub trait MutVisitor: Sized {
Expand Down Expand Up @@ -102,11 +95,11 @@ pub trait MutVisitor: Sized {
}

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

fn flat_map_item(&mut self, i: P<Item>) -> SmallVec<[P<Item>; 1]> {
walk_flat_map_item(self, i, None)
walk_flat_map_item(self, i)
}

fn visit_fn_header(&mut self, header: &mut FnHeader) {
Expand All @@ -120,9 +113,9 @@ pub trait MutVisitor: Sized {
fn flat_map_assoc_item(
&mut self,
i: P<AssocItem>,
ctxt: AssocCtxt,
_ctxt: AssocCtxt,
) -> SmallVec<[P<AssocItem>; 1]> {
walk_flat_map_item(self, i, Some(ctxt))
walk_flat_map_item(self, i)
}

fn visit_fn_decl(&mut self, d: &mut P<FnDecl>) {
Expand Down Expand Up @@ -890,7 +883,7 @@ fn walk_coroutine_kind<T: MutVisitor>(vis: &mut T, coroutine_kind: &mut Coroutin

fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
match kind {
FnKind::Fn(_ctxt, _ident, FnSig { header, decl, span }, generics, body) => {
FnKind::Fn(FnSig { header, decl, span }, generics, body) => {
// Identifier and visibility are visited as a part of the item.
vis.visit_fn_header(header);
vis.visit_generics(generics);
Expand Down Expand Up @@ -1091,24 +1084,15 @@ pub fn walk_block<T: MutVisitor>(vis: &mut T, block: &mut P<Block>) {

pub fn walk_item_kind(
kind: &mut impl WalkItemKind,
ident: Ident,
span: Span,
id: NodeId,
vis: &mut impl MutVisitor,
) {
kind.walk(None, ident, span, id, vis)
kind.walk(span, id, vis)
}

impl WalkItemKind for ItemKind {
fn walk(
&mut self,
ctxt: Option<AssocCtxt>,
ident: Ident,
span: Span,
id: NodeId,
vis: &mut impl MutVisitor,
) {
assert_eq!(ctxt, None);
fn walk(&mut self, span: Span, id: NodeId, vis: &mut impl MutVisitor) {
match self {
ItemKind::ExternCrate(_orig_name) => {}
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
Expand All @@ -1121,7 +1105,7 @@ impl WalkItemKind for ItemKind {
}
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(vis, defaultness);
vis.visit_fn(FnKind::Fn(FnCtxt::Free, ident, sig, generics, body), span, id);
vis.visit_fn(FnKind::Fn(sig, generics, body), span, id);
}
ItemKind::Mod(safety, mod_kind) => {
visit_safety(vis, safety);
Expand Down Expand Up @@ -1220,26 +1204,14 @@ impl WalkItemKind for ItemKind {
}

impl WalkItemKind for AssocItemKind {
fn walk(
&mut self,
ctxt: Option<AssocCtxt>,
ident: Ident,
span: Span,
id: NodeId,
visitor: &mut impl MutVisitor,
) {
let ctxt = ctxt.unwrap();
fn walk(&mut self, span: Span, id: NodeId, visitor: &mut impl MutVisitor) {
match self {
AssocItemKind::Const(item) => {
visit_const_item(item, visitor);
}
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(visitor, defaultness);
visitor.visit_fn(
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, generics, body),
span,
id,
);
visitor.visit_fn(FnKind::Fn(sig, generics, body), span, id);
}
AssocItemKind::Type(box TyAlias {
defaultness,
Expand Down Expand Up @@ -1323,37 +1295,28 @@ pub fn walk_crate<T: MutVisitor>(vis: &mut T, krate: &mut Crate) {
pub fn walk_flat_map_item<K: WalkItemKind>(
visitor: &mut impl MutVisitor,
mut item: P<Item<K>>,
ctxt: Option<AssocCtxt>,
) -> SmallVec<[P<Item<K>>; 1]> {
let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
visitor.visit_id(id);
visit_attrs(visitor, attrs);
visitor.visit_vis(vis);
visitor.visit_ident(ident);
kind.walk(ctxt, *ident, *span, *id, visitor);
kind.walk(*span, *id, visitor);
visit_lazy_tts(visitor, tokens);
visitor.visit_span(span);
smallvec![item]
}

impl WalkItemKind for ForeignItemKind {
fn walk(
&mut self,
ctxt: Option<AssocCtxt>,
ident: Ident,
span: Span,
id: NodeId,
visitor: &mut impl MutVisitor,
) {
assert_eq!(ctxt, None);
fn walk(&mut self, span: Span, id: NodeId, visitor: &mut impl MutVisitor) {
match self {
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr));
}
ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(visitor, defaultness);
visitor.visit_fn(FnKind::Fn(FnCtxt::Foreign, ident, sig, generics, body), span, id);
visitor.visit_fn(FnKind::Fn(sig, generics, body), span, id);
}
ForeignItemKind::TyAlias(box TyAlias {
defaultness,
Expand Down Expand Up @@ -1824,7 +1787,7 @@ impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNo
#[derive(Debug)]
pub enum FnKind<'a> {
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
Fn(FnCtxt, Ident, &'a mut FnSig, &'a mut Generics, &'a mut Option<P<Block>>),
Fn(&'a mut FnSig, &'a mut Generics, &'a mut Option<P<Block>>),

/// E.g., `|x, y| body`.
Closure(&'a mut ClosureBinder, &'a mut P<FnDecl>, &'a mut P<Expr>),
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_builtin_macros/src/cfg_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,24 @@ impl MutVisitor for CfgEval<'_> {

fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
let item = configure!(self, item);
mut_visit::walk_flat_map_item(self, item, None)
mut_visit::walk_flat_map_item(self, item)
}

fn flat_map_assoc_item(
&mut self,
item: P<ast::AssocItem>,
ctxt: AssocCtxt,
_ctxt: AssocCtxt,
) -> SmallVec<[P<ast::AssocItem>; 1]> {
let item = configure!(self, item);
mut_visit::walk_flat_map_item(self, item, Some(ctxt))
mut_visit::walk_flat_map_item(self, item)
}

fn flat_map_foreign_item(
&mut self,
foreign_item: P<ast::ForeignItem>,
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
let foreign_item = configure!(self, foreign_item);
mut_visit::walk_flat_map_item(self, foreign_item, None)
mut_visit::walk_flat_map_item(self, foreign_item)
}

fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
item.kind
{
let prev_tests = mem::take(&mut self.tests);
walk_item_kind(&mut item.kind, item.ident, item.span, item.id, self);
walk_item_kind(&mut item.kind, item.span, item.id, self);
self.add_test_cases(item.id, span, prev_tests);
} else {
// But in those cases, we emit a lint to warn the user of these missing tests.
Expand Down Expand Up @@ -192,7 +192,7 @@ struct EntryPointCleaner<'a> {
impl<'a> MutVisitor for EntryPointCleaner<'a> {
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
self.depth += 1;
let item = walk_flat_map_item(self, i, None).expect_one("noop did something");
let item = walk_flat_map_item(self, i).expect_one("noop did something");
self.depth -= 1;

// Remove any #[rustc_main] or #[start] from the AST so it doesn't
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ impl InvocationCollectorNode for P<ast::Item> {
fragment.make_items()
}
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
walk_flat_map_item(visitor, self, None)
walk_flat_map_item(visitor, self)
}
fn is_mac_call(&self) -> bool {
matches!(self.kind, ItemKind::MacCall(..))
Expand Down Expand Up @@ -1293,7 +1293,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag>
fragment.make_trait_items()
}
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
walk_flat_map_item(visitor, self.wrapped, Some(AssocCtxt::Trait))
walk_flat_map_item(visitor, self.wrapped)
}
fn is_mac_call(&self) -> bool {
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
Expand Down Expand Up @@ -1334,7 +1334,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag>
fragment.make_impl_items()
}
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
walk_flat_map_item(visitor, self.wrapped, Some(AssocCtxt::Impl))
walk_flat_map_item(visitor, self.wrapped)
}
fn is_mac_call(&self) -> bool {
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
Expand Down Expand Up @@ -1372,7 +1372,7 @@ impl InvocationCollectorNode for P<ast::ForeignItem> {
fragment.make_foreign_items()
}
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
walk_flat_map_item(visitor, self, None)
walk_flat_map_item(visitor, self)
}
fn is_mac_call(&self) -> bool {
matches!(self.kind, ForeignItemKind::MacCall(..))
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_expand/src/placeholders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl MutVisitor for PlaceholderExpander {
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
match item.kind {
ast::ItemKind::MacCall(_) => self.remove(item.id).make_items(),
_ => walk_flat_map_item(self, item, None),
_ => walk_flat_map_item(self, item),
}
}

Expand All @@ -284,7 +284,7 @@ impl MutVisitor for PlaceholderExpander {
AssocCtxt::Impl => it.make_impl_items(),
}
}
_ => walk_flat_map_item(self, item, Some(ctxt)),
_ => walk_flat_map_item(self, item),
}
}

Expand All @@ -294,7 +294,7 @@ impl MutVisitor for PlaceholderExpander {
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
match item.kind {
ast::ForeignItemKind::MacCall(_) => self.remove(item.id).make_foreign_items(),
_ => walk_flat_map_item(self, item, None),
_ => walk_flat_map_item(self, item),
}
}

Expand Down

0 comments on commit e9f32d0

Please sign in to comment.