Skip to content

Commit e9f32d0

Browse files
committed
Avoid passing state that will not be visited
1 parent 91b26fc commit e9f32d0

File tree

5 files changed

+29
-66
lines changed

5 files changed

+29
-66
lines changed

compiler/rustc_ast/src/mut_visit.rs

+16-53
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::ast::*;
1111
use crate::ptr::P;
1212
use crate::token::{self, Token};
1313
use crate::tokenstream::*;
14-
use crate::visit::{AssocCtxt, BoundKind, FnCtxt};
14+
use crate::visit::{AssocCtxt, BoundKind};
1515

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

3838
pub trait WalkItemKind {
39-
fn walk(
40-
&mut self,
41-
ctxt: Option<AssocCtxt>,
42-
ident: Ident,
43-
span: Span,
44-
id: NodeId,
45-
visitor: &mut impl MutVisitor,
46-
);
39+
fn walk(&mut self, span: Span, id: NodeId, visitor: &mut impl MutVisitor);
4740
}
4841

4942
pub trait MutVisitor: Sized {
@@ -102,11 +95,11 @@ pub trait MutVisitor: Sized {
10295
}
10396

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

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

112105
fn visit_fn_header(&mut self, header: &mut FnHeader) {
@@ -120,9 +113,9 @@ pub trait MutVisitor: Sized {
120113
fn flat_map_assoc_item(
121114
&mut self,
122115
i: P<AssocItem>,
123-
ctxt: AssocCtxt,
116+
_ctxt: AssocCtxt,
124117
) -> SmallVec<[P<AssocItem>; 1]> {
125-
walk_flat_map_item(self, i, Some(ctxt))
118+
walk_flat_map_item(self, i)
126119
}
127120

128121
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
890883

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

10921085
pub fn walk_item_kind(
10931086
kind: &mut impl WalkItemKind,
1094-
ident: Ident,
10951087
span: Span,
10961088
id: NodeId,
10971089
vis: &mut impl MutVisitor,
10981090
) {
1099-
kind.walk(None, ident, span, id, vis)
1091+
kind.walk(span, id, vis)
11001092
}
11011093

11021094
impl WalkItemKind for ItemKind {
1103-
fn walk(
1104-
&mut self,
1105-
ctxt: Option<AssocCtxt>,
1106-
ident: Ident,
1107-
span: Span,
1108-
id: NodeId,
1109-
vis: &mut impl MutVisitor,
1110-
) {
1111-
assert_eq!(ctxt, None);
1095+
fn walk(&mut self, span: Span, id: NodeId, vis: &mut impl MutVisitor) {
11121096
match self {
11131097
ItemKind::ExternCrate(_orig_name) => {}
11141098
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
@@ -1121,7 +1105,7 @@ impl WalkItemKind for ItemKind {
11211105
}
11221106
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
11231107
visit_defaultness(vis, defaultness);
1124-
vis.visit_fn(FnKind::Fn(FnCtxt::Free, ident, sig, generics, body), span, id);
1108+
vis.visit_fn(FnKind::Fn(sig, generics, body), span, id);
11251109
}
11261110
ItemKind::Mod(safety, mod_kind) => {
11271111
visit_safety(vis, safety);
@@ -1220,26 +1204,14 @@ impl WalkItemKind for ItemKind {
12201204
}
12211205

12221206
impl WalkItemKind for AssocItemKind {
1223-
fn walk(
1224-
&mut self,
1225-
ctxt: Option<AssocCtxt>,
1226-
ident: Ident,
1227-
span: Span,
1228-
id: NodeId,
1229-
visitor: &mut impl MutVisitor,
1230-
) {
1231-
let ctxt = ctxt.unwrap();
1207+
fn walk(&mut self, span: Span, id: NodeId, visitor: &mut impl MutVisitor) {
12321208
match self {
12331209
AssocItemKind::Const(item) => {
12341210
visit_const_item(item, visitor);
12351211
}
12361212
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
12371213
visit_defaultness(visitor, defaultness);
1238-
visitor.visit_fn(
1239-
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, generics, body),
1240-
span,
1241-
id,
1242-
);
1214+
visitor.visit_fn(FnKind::Fn(sig, generics, body), span, id);
12431215
}
12441216
AssocItemKind::Type(box TyAlias {
12451217
defaultness,
@@ -1323,37 +1295,28 @@ pub fn walk_crate<T: MutVisitor>(vis: &mut T, krate: &mut Crate) {
13231295
pub fn walk_flat_map_item<K: WalkItemKind>(
13241296
visitor: &mut impl MutVisitor,
13251297
mut item: P<Item<K>>,
1326-
ctxt: Option<AssocCtxt>,
13271298
) -> SmallVec<[P<Item<K>>; 1]> {
13281299
let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
13291300
visitor.visit_id(id);
13301301
visit_attrs(visitor, attrs);
13311302
visitor.visit_vis(vis);
13321303
visitor.visit_ident(ident);
1333-
kind.walk(ctxt, *ident, *span, *id, visitor);
1304+
kind.walk(*span, *id, visitor);
13341305
visit_lazy_tts(visitor, tokens);
13351306
visitor.visit_span(span);
13361307
smallvec![item]
13371308
}
13381309

13391310
impl WalkItemKind for ForeignItemKind {
1340-
fn walk(
1341-
&mut self,
1342-
ctxt: Option<AssocCtxt>,
1343-
ident: Ident,
1344-
span: Span,
1345-
id: NodeId,
1346-
visitor: &mut impl MutVisitor,
1347-
) {
1348-
assert_eq!(ctxt, None);
1311+
fn walk(&mut self, span: Span, id: NodeId, visitor: &mut impl MutVisitor) {
13491312
match self {
13501313
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
13511314
visitor.visit_ty(ty);
13521315
visit_opt(expr, |expr| visitor.visit_expr(expr));
13531316
}
13541317
ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
13551318
visit_defaultness(visitor, defaultness);
1356-
visitor.visit_fn(FnKind::Fn(FnCtxt::Foreign, ident, sig, generics, body), span, id);
1319+
visitor.visit_fn(FnKind::Fn(sig, generics, body), span, id);
13571320
}
13581321
ForeignItemKind::TyAlias(box TyAlias {
13591322
defaultness,
@@ -1824,7 +1787,7 @@ impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNo
18241787
#[derive(Debug)]
18251788
pub enum FnKind<'a> {
18261789
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
1827-
Fn(FnCtxt, Ident, &'a mut FnSig, &'a mut Generics, &'a mut Option<P<Block>>),
1790+
Fn(&'a mut FnSig, &'a mut Generics, &'a mut Option<P<Block>>),
18281791

18291792
/// E.g., `|x, y| body`.
18301793
Closure(&'a mut ClosureBinder, &'a mut P<FnDecl>, &'a mut P<Expr>),

compiler/rustc_builtin_macros/src/cfg_eval.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -242,24 +242,24 @@ impl MutVisitor for CfgEval<'_> {
242242

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

248248
fn flat_map_assoc_item(
249249
&mut self,
250250
item: P<ast::AssocItem>,
251-
ctxt: AssocCtxt,
251+
_ctxt: AssocCtxt,
252252
) -> SmallVec<[P<ast::AssocItem>; 1]> {
253253
let item = configure!(self, item);
254-
mut_visit::walk_flat_map_item(self, item, Some(ctxt))
254+
mut_visit::walk_flat_map_item(self, item)
255255
}
256256

257257
fn flat_map_foreign_item(
258258
&mut self,
259259
foreign_item: P<ast::ForeignItem>,
260260
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
261261
let foreign_item = configure!(self, foreign_item);
262-
mut_visit::walk_flat_map_item(self, foreign_item, None)
262+
mut_visit::walk_flat_map_item(self, foreign_item)
263263
}
264264

265265
fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {

compiler/rustc_builtin_macros/src/test_harness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
144144
item.kind
145145
{
146146
let prev_tests = mem::take(&mut self.tests);
147-
walk_item_kind(&mut item.kind, item.ident, item.span, item.id, self);
147+
walk_item_kind(&mut item.kind, item.span, item.id, self);
148148
self.add_test_cases(item.id, span, prev_tests);
149149
} else {
150150
// But in those cases, we emit a lint to warn the user of these missing tests.
@@ -192,7 +192,7 @@ struct EntryPointCleaner<'a> {
192192
impl<'a> MutVisitor for EntryPointCleaner<'a> {
193193
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
194194
self.depth += 1;
195-
let item = walk_flat_map_item(self, i, None).expect_one("noop did something");
195+
let item = walk_flat_map_item(self, i).expect_one("noop did something");
196196
self.depth -= 1;
197197

198198
// Remove any #[rustc_main] or #[start] from the AST so it doesn't

compiler/rustc_expand/src/expand.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ impl InvocationCollectorNode for P<ast::Item> {
11491149
fragment.make_items()
11501150
}
11511151
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1152-
walk_flat_map_item(visitor, self, None)
1152+
walk_flat_map_item(visitor, self)
11531153
}
11541154
fn is_mac_call(&self) -> bool {
11551155
matches!(self.kind, ItemKind::MacCall(..))
@@ -1293,7 +1293,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag>
12931293
fragment.make_trait_items()
12941294
}
12951295
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1296-
walk_flat_map_item(visitor, self.wrapped, Some(AssocCtxt::Trait))
1296+
walk_flat_map_item(visitor, self.wrapped)
12971297
}
12981298
fn is_mac_call(&self) -> bool {
12991299
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
@@ -1334,7 +1334,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag>
13341334
fragment.make_impl_items()
13351335
}
13361336
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1337-
walk_flat_map_item(visitor, self.wrapped, Some(AssocCtxt::Impl))
1337+
walk_flat_map_item(visitor, self.wrapped)
13381338
}
13391339
fn is_mac_call(&self) -> bool {
13401340
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
@@ -1372,7 +1372,7 @@ impl InvocationCollectorNode for P<ast::ForeignItem> {
13721372
fragment.make_foreign_items()
13731373
}
13741374
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1375-
walk_flat_map_item(visitor, self, None)
1375+
walk_flat_map_item(visitor, self)
13761376
}
13771377
fn is_mac_call(&self) -> bool {
13781378
matches!(self.kind, ForeignItemKind::MacCall(..))

compiler/rustc_expand/src/placeholders.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl MutVisitor for PlaceholderExpander {
267267
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
268268
match item.kind {
269269
ast::ItemKind::MacCall(_) => self.remove(item.id).make_items(),
270-
_ => walk_flat_map_item(self, item, None),
270+
_ => walk_flat_map_item(self, item),
271271
}
272272
}
273273

@@ -284,7 +284,7 @@ impl MutVisitor for PlaceholderExpander {
284284
AssocCtxt::Impl => it.make_impl_items(),
285285
}
286286
}
287-
_ => walk_flat_map_item(self, item, Some(ctxt)),
287+
_ => walk_flat_map_item(self, item),
288288
}
289289
}
290290

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

0 commit comments

Comments
 (0)