Avoid passing state that will not be visited

This commit is contained in:
Oli Scherer 2024-07-22 14:34:45 +00:00
parent 91b26fcb89
commit e9f32d0ca6
5 changed files with 29 additions and 66 deletions

View File

@ -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;
@ -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 {
@ -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) {
@ -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>) {
@ -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);
@ -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),
@ -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);
@ -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,
@ -1323,29 +1295,20 @@ 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);
@ -1353,7 +1316,7 @@ impl WalkItemKind for ForeignItemKind {
}
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,
@ -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>),

View File

@ -242,16 +242,16 @@ 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(
@ -259,7 +259,7 @@ impl MutVisitor for CfgEval<'_> {
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]> {

View File

@ -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.
@ -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

View File

@ -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(..))
@ -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(..))
@ -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(..))
@ -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(..))

View File

@ -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),
}
}
@ -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),
}
}
@ -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),
}
}