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::ptr::P;
use crate::token::{self, Token}; use crate::token::{self, Token};
use crate::tokenstream::*; 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::flat_map_in_place::FlatMapInPlace;
use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::stack::ensure_sufficient_stack;
@ -36,14 +36,7 @@ impl<A: Array> ExpectOne<A> for SmallVec<A> {
} }
pub trait WalkItemKind { pub trait WalkItemKind {
fn walk( fn walk(&mut self, span: Span, id: NodeId, visitor: &mut impl MutVisitor);
&mut self,
ctxt: Option<AssocCtxt>,
ident: Ident,
span: Span,
id: NodeId,
visitor: &mut impl MutVisitor,
);
} }
pub trait MutVisitor: Sized { 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]> { 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]> { 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) { fn visit_fn_header(&mut self, header: &mut FnHeader) {
@ -120,9 +113,9 @@ pub trait MutVisitor: Sized {
fn flat_map_assoc_item( fn flat_map_assoc_item(
&mut self, &mut self,
i: P<AssocItem>, i: P<AssocItem>,
ctxt: AssocCtxt, _ctxt: AssocCtxt,
) -> SmallVec<[P<AssocItem>; 1]> { ) -> 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>) { 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<'_>) { fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
match kind { 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. // Identifier and visibility are visited as a part of the item.
vis.visit_fn_header(header); vis.visit_fn_header(header);
vis.visit_generics(generics); 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( pub fn walk_item_kind(
kind: &mut impl WalkItemKind, kind: &mut impl WalkItemKind,
ident: Ident,
span: Span, span: Span,
id: NodeId, id: NodeId,
vis: &mut impl MutVisitor, vis: &mut impl MutVisitor,
) { ) {
kind.walk(None, ident, span, id, vis) kind.walk(span, id, vis)
} }
impl WalkItemKind for ItemKind { impl WalkItemKind for ItemKind {
fn walk( fn walk(&mut self, span: Span, id: NodeId, vis: &mut impl MutVisitor) {
&mut self,
ctxt: Option<AssocCtxt>,
ident: Ident,
span: Span,
id: NodeId,
vis: &mut impl MutVisitor,
) {
assert_eq!(ctxt, None);
match self { match self {
ItemKind::ExternCrate(_orig_name) => {} ItemKind::ExternCrate(_orig_name) => {}
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree), 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 }) => { ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(vis, defaultness); 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) => { ItemKind::Mod(safety, mod_kind) => {
visit_safety(vis, safety); visit_safety(vis, safety);
@ -1220,26 +1204,14 @@ impl WalkItemKind for ItemKind {
} }
impl WalkItemKind for AssocItemKind { impl WalkItemKind for AssocItemKind {
fn walk( fn walk(&mut self, span: Span, id: NodeId, visitor: &mut impl MutVisitor) {
&mut self,
ctxt: Option<AssocCtxt>,
ident: Ident,
span: Span,
id: NodeId,
visitor: &mut impl MutVisitor,
) {
let ctxt = ctxt.unwrap();
match self { match self {
AssocItemKind::Const(item) => { AssocItemKind::Const(item) => {
visit_const_item(item, visitor); visit_const_item(item, visitor);
} }
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => { AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(visitor, defaultness); visit_defaultness(visitor, defaultness);
visitor.visit_fn( visitor.visit_fn(FnKind::Fn(sig, generics, body), span, id);
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, generics, body),
span,
id,
);
} }
AssocItemKind::Type(box TyAlias { AssocItemKind::Type(box TyAlias {
defaultness, 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>( pub fn walk_flat_map_item<K: WalkItemKind>(
visitor: &mut impl MutVisitor, visitor: &mut impl MutVisitor,
mut item: P<Item<K>>, mut item: P<Item<K>>,
ctxt: Option<AssocCtxt>,
) -> SmallVec<[P<Item<K>>; 1]> { ) -> SmallVec<[P<Item<K>>; 1]> {
let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut(); let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
visitor.visit_id(id); visitor.visit_id(id);
visit_attrs(visitor, attrs); visit_attrs(visitor, attrs);
visitor.visit_vis(vis); visitor.visit_vis(vis);
visitor.visit_ident(ident); visitor.visit_ident(ident);
kind.walk(ctxt, *ident, *span, *id, visitor); kind.walk(*span, *id, visitor);
visit_lazy_tts(visitor, tokens); visit_lazy_tts(visitor, tokens);
visitor.visit_span(span); visitor.visit_span(span);
smallvec![item] smallvec![item]
} }
impl WalkItemKind for ForeignItemKind { impl WalkItemKind for ForeignItemKind {
fn walk( fn walk(&mut self, span: Span, id: NodeId, visitor: &mut impl MutVisitor) {
&mut self,
ctxt: Option<AssocCtxt>,
ident: Ident,
span: Span,
id: NodeId,
visitor: &mut impl MutVisitor,
) {
assert_eq!(ctxt, None);
match self { match self {
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => { ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
visitor.visit_ty(ty); visitor.visit_ty(ty);
@ -1353,7 +1316,7 @@ impl WalkItemKind for ForeignItemKind {
} }
ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => { ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(visitor, defaultness); 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 { ForeignItemKind::TyAlias(box TyAlias {
defaultness, defaultness,
@ -1824,7 +1787,7 @@ impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNo
#[derive(Debug)] #[derive(Debug)]
pub enum FnKind<'a> { pub enum FnKind<'a> {
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`. /// 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`. /// E.g., `|x, y| body`.
Closure(&'a mut ClosureBinder, &'a mut P<FnDecl>, &'a mut P<Expr>), 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]> { fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
let item = configure!(self, item); 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( fn flat_map_assoc_item(
&mut self, &mut self,
item: P<ast::AssocItem>, item: P<ast::AssocItem>,
ctxt: AssocCtxt, _ctxt: AssocCtxt,
) -> SmallVec<[P<ast::AssocItem>; 1]> { ) -> SmallVec<[P<ast::AssocItem>; 1]> {
let item = configure!(self, item); 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( fn flat_map_foreign_item(
@ -259,7 +259,7 @@ impl MutVisitor for CfgEval<'_> {
foreign_item: P<ast::ForeignItem>, foreign_item: P<ast::ForeignItem>,
) -> SmallVec<[P<ast::ForeignItem>; 1]> { ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
let foreign_item = configure!(self, foreign_item); 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]> { 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 item.kind
{ {
let prev_tests = mem::take(&mut self.tests); 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); self.add_test_cases(item.id, span, prev_tests);
} else { } else {
// But in those cases, we emit a lint to warn the user of these missing tests. // 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> { impl<'a> MutVisitor for EntryPointCleaner<'a> {
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> { fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
self.depth += 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; self.depth -= 1;
// Remove any #[rustc_main] or #[start] from the AST so it doesn't // 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() fragment.make_items()
} }
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy { 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 { fn is_mac_call(&self) -> bool {
matches!(self.kind, ItemKind::MacCall(..)) matches!(self.kind, ItemKind::MacCall(..))
@ -1293,7 +1293,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag>
fragment.make_trait_items() fragment.make_trait_items()
} }
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy { 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 { fn is_mac_call(&self) -> bool {
matches!(self.wrapped.kind, AssocItemKind::MacCall(..)) matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
@ -1334,7 +1334,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag>
fragment.make_impl_items() fragment.make_impl_items()
} }
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy { 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 { fn is_mac_call(&self) -> bool {
matches!(self.wrapped.kind, AssocItemKind::MacCall(..)) matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
@ -1372,7 +1372,7 @@ impl InvocationCollectorNode for P<ast::ForeignItem> {
fragment.make_foreign_items() fragment.make_foreign_items()
} }
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy { 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 { fn is_mac_call(&self) -> bool {
matches!(self.kind, ForeignItemKind::MacCall(..)) 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]> { fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
match item.kind { match item.kind {
ast::ItemKind::MacCall(_) => self.remove(item.id).make_items(), 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(), 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]> { ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
match item.kind { match item.kind {
ast::ForeignItemKind::MacCall(_) => self.remove(item.id).make_foreign_items(), ast::ForeignItemKind::MacCall(_) => self.remove(item.id).make_foreign_items(),
_ => walk_flat_map_item(self, item, None), _ => walk_flat_map_item(self, item),
} }
} }