Skip to content

Commit 1b9ac00

Browse files
committed
Make function items in mut visitors all go through the same visit_fn function, just like with immutable visitors
1 parent c064b36 commit 1b9ac00

File tree

5 files changed

+64
-42
lines changed

5 files changed

+64
-42
lines changed

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 52 additions & 30 deletions
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};
14+
use crate::visit::{AssocCtxt, BoundKind, FnCtxt};
1515

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

3838
pub trait NoopVisitItemKind {
39-
fn noop_visit(&mut self, visitor: &mut impl MutVisitor);
39+
fn noop_visit(&mut self, ctxt: Option<AssocCtxt>, visitor: &mut impl MutVisitor);
4040
}
4141

4242
pub trait MutVisitor: Sized {
@@ -95,11 +95,11 @@ pub trait MutVisitor: Sized {
9595
}
9696

9797
fn flat_map_foreign_item(&mut self, ni: P<ForeignItem>) -> SmallVec<[P<ForeignItem>; 1]> {
98-
noop_flat_map_item(ni, self)
98+
noop_flat_map_item(ni, None, self)
9999
}
100100

101101
fn flat_map_item(&mut self, i: P<Item>) -> SmallVec<[P<Item>; 1]> {
102-
noop_flat_map_item(i, self)
102+
noop_flat_map_item(i, None, self)
103103
}
104104

105105
fn visit_fn_header(&mut self, header: &mut FnHeader) {
@@ -113,15 +113,19 @@ pub trait MutVisitor: Sized {
113113
fn flat_map_assoc_item(
114114
&mut self,
115115
i: P<AssocItem>,
116-
_ctxt: AssocCtxt,
116+
ctxt: AssocCtxt,
117117
) -> SmallVec<[P<AssocItem>; 1]> {
118-
noop_flat_map_item(i, self)
118+
noop_flat_map_item(i, Some(ctxt), self)
119119
}
120120

121121
fn visit_fn_decl(&mut self, d: &mut P<FnDecl>) {
122122
noop_visit_fn_decl(d, self);
123123
}
124124

125+
fn visit_fn(&mut self, fk: FnKind<'_>) {
126+
noop_visit_fn(fk, self)
127+
}
128+
125129
fn visit_coroutine_kind(&mut self, a: &mut CoroutineKind) {
126130
noop_visit_coroutine_kind(a, self);
127131
}
@@ -379,13 +383,6 @@ fn visit_bounds<T: MutVisitor>(bounds: &mut GenericBounds, ctxt: BoundKind, vis:
379383
visit_vec(bounds, |bound| vis.visit_param_bound(bound, ctxt));
380384
}
381385

382-
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
383-
fn visit_fn_sig<T: MutVisitor>(FnSig { header, decl, span }: &mut FnSig, vis: &mut T) {
384-
vis.visit_fn_header(header);
385-
vis.visit_fn_decl(decl);
386-
vis.visit_span(span);
387-
}
388-
389386
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
390387
fn visit_attr_args<T: MutVisitor>(args: &mut AttrArgs, vis: &mut T) {
391388
match args {
@@ -880,6 +877,26 @@ fn noop_visit_coroutine_kind<T: MutVisitor>(coroutine_kind: &mut CoroutineKind,
880877
}
881878
}
882879

880+
fn noop_visit_fn<T: MutVisitor>(kind: FnKind<'_>, vis: &mut T) {
881+
match kind {
882+
FnKind::Fn(_ctxt, FnSig { header, decl, span }, generics, body) => {
883+
// Identifier and visibility are visited as a part of the item.
884+
vis.visit_fn_header(header);
885+
vis.visit_generics(generics);
886+
vis.visit_fn_decl(decl);
887+
if let Some(body) = body {
888+
vis.visit_block(body);
889+
}
890+
vis.visit_span(span);
891+
}
892+
FnKind::Closure(binder, decl, body) => {
893+
vis.visit_closure_binder(binder);
894+
vis.visit_fn_decl(decl);
895+
vis.visit_expr(body);
896+
}
897+
}
898+
}
899+
883900
fn noop_visit_fn_decl<T: MutVisitor>(decl: &mut P<FnDecl>, vis: &mut T) {
884901
let FnDecl { inputs, output } = decl.deref_mut();
885902
inputs.flat_map_in_place(|param| vis.flat_map_param(param));
@@ -1062,11 +1079,12 @@ pub fn noop_visit_block<T: MutVisitor>(block: &mut P<Block>, vis: &mut T) {
10621079
}
10631080

10641081
pub fn noop_visit_item_kind(kind: &mut impl NoopVisitItemKind, vis: &mut impl MutVisitor) {
1065-
kind.noop_visit(vis)
1082+
kind.noop_visit(None, vis)
10661083
}
10671084

10681085
impl NoopVisitItemKind for ItemKind {
1069-
fn noop_visit(&mut self, vis: &mut impl MutVisitor) {
1086+
fn noop_visit(&mut self, ctxt: Option<AssocCtxt>, vis: &mut impl MutVisitor) {
1087+
assert_eq!(ctxt, None);
10701088
match self {
10711089
ItemKind::ExternCrate(_orig_name) => {}
10721090
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
@@ -1079,9 +1097,7 @@ impl NoopVisitItemKind for ItemKind {
10791097
}
10801098
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
10811099
visit_defaultness(defaultness, vis);
1082-
vis.visit_generics(generics);
1083-
visit_fn_sig(sig, vis);
1084-
visit_opt(body, |body| vis.visit_block(body));
1100+
vis.visit_fn(FnKind::Fn(FnCtxt::Free, sig, generics, body));
10851101
}
10861102
ItemKind::Mod(safety, mod_kind) => {
10871103
visit_safety(safety, vis);
@@ -1180,16 +1196,15 @@ impl NoopVisitItemKind for ItemKind {
11801196
}
11811197

11821198
impl NoopVisitItemKind for AssocItemKind {
1183-
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
1199+
fn noop_visit(&mut self, ctxt: Option<AssocCtxt>, visitor: &mut impl MutVisitor) {
1200+
let ctxt = ctxt.unwrap();
11841201
match self {
11851202
AssocItemKind::Const(item) => {
11861203
visit_const_item(item, visitor);
11871204
}
11881205
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
11891206
visit_defaultness(defaultness, visitor);
1190-
visitor.visit_generics(generics);
1191-
visit_fn_sig(sig, visitor);
1192-
visit_opt(body, |body| visitor.visit_block(body));
1207+
visitor.visit_fn(FnKind::Fn(FnCtxt::Assoc(ctxt), sig, generics, body));
11931208
}
11941209
AssocItemKind::Type(box TyAlias {
11951210
defaultness,
@@ -1272,31 +1287,31 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
12721287
// Mutates one item into possibly many items.
12731288
pub fn noop_flat_map_item<K: NoopVisitItemKind>(
12741289
mut item: P<Item<K>>,
1290+
ctxt: Option<AssocCtxt>,
12751291
visitor: &mut impl MutVisitor,
12761292
) -> SmallVec<[P<Item<K>>; 1]> {
12771293
let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
12781294
visitor.visit_id(id);
12791295
visit_attrs(attrs, visitor);
12801296
visitor.visit_vis(vis);
12811297
visitor.visit_ident(ident);
1282-
kind.noop_visit(visitor);
1298+
kind.noop_visit(ctxt, visitor);
12831299
visit_lazy_tts(tokens, visitor);
12841300
visitor.visit_span(span);
12851301
smallvec![item]
12861302
}
12871303

12881304
impl NoopVisitItemKind for ForeignItemKind {
1289-
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
1305+
fn noop_visit(&mut self, ctxt: Option<AssocCtxt>, visitor: &mut impl MutVisitor) {
1306+
assert_eq!(ctxt, None);
12901307
match self {
12911308
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
12921309
visitor.visit_ty(ty);
12931310
visit_opt(expr, |expr| visitor.visit_expr(expr));
12941311
}
12951312
ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
12961313
visit_defaultness(defaultness, visitor);
1297-
visitor.visit_generics(generics);
1298-
visit_fn_sig(sig, visitor);
1299-
visit_opt(body, |body| visitor.visit_block(body));
1314+
visitor.visit_fn(FnKind::Fn(FnCtxt::Foreign, sig, generics, body));
13001315
}
13011316
ForeignItemKind::TyAlias(box TyAlias {
13021317
defaultness,
@@ -1506,12 +1521,10 @@ pub fn noop_visit_expr<T: MutVisitor>(
15061521
fn_decl_span,
15071522
fn_arg_span,
15081523
}) => {
1509-
vis.visit_closure_binder(binder);
15101524
visit_constness(constness, vis);
15111525
coroutine_kind.as_mut().map(|coroutine_kind| vis.visit_coroutine_kind(coroutine_kind));
15121526
vis.visit_capture_by(capture_clause);
1513-
vis.visit_fn_decl(fn_decl);
1514-
vis.visit_expr(body);
1527+
vis.visit_fn(FnKind::Closure(binder, fn_decl, body));
15151528
vis.visit_span(fn_decl_span);
15161529
vis.visit_span(fn_arg_span);
15171530
}
@@ -1768,3 +1781,12 @@ impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNo
17681781
crate::ast_traits::AstNodeWrapper::new(N::dummy(), T::dummy())
17691782
}
17701783
}
1784+
1785+
#[derive(Debug)]
1786+
pub enum FnKind<'a> {
1787+
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
1788+
Fn(FnCtxt, &'a mut FnSig, &'a mut Generics, &'a mut Option<P<Block>>),
1789+
1790+
/// E.g., `|x, y| body`.
1791+
Closure(&'a mut ClosureBinder, &'a mut P<FnDecl>, &'a mut P<Expr>),
1792+
}

compiler/rustc_builtin_macros/src/cfg_eval.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,22 @@ impl MutVisitor for CfgEval<'_> {
239239
}
240240

241241
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
242-
mut_visit::noop_flat_map_item(configure!(self, item), self)
242+
mut_visit::noop_flat_map_item(configure!(self, item), None, self)
243243
}
244244

245245
fn flat_map_assoc_item(
246246
&mut self,
247247
item: P<ast::AssocItem>,
248-
_ctxt: AssocCtxt,
248+
ctxt: AssocCtxt,
249249
) -> SmallVec<[P<ast::AssocItem>; 1]> {
250-
mut_visit::noop_flat_map_item(configure!(self, item), self)
250+
mut_visit::noop_flat_map_item(configure!(self, item), Some(ctxt), self)
251251
}
252252

253253
fn flat_map_foreign_item(
254254
&mut self,
255255
foreign_item: P<ast::ForeignItem>,
256256
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
257-
mut_visit::noop_flat_map_item(configure!(self, foreign_item), self)
257+
mut_visit::noop_flat_map_item(configure!(self, foreign_item), None, self)
258258
}
259259

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

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 = noop_flat_map_item(i, self).expect_one("noop did something");
195+
let item = noop_flat_map_item(i, None, self).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

Lines changed: 4 additions & 4 deletions
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 noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1152-
noop_flat_map_item(self, visitor)
1152+
noop_flat_map_item(self, None, visitor)
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 noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1296-
noop_flat_map_item(self.wrapped, visitor)
1296+
noop_flat_map_item(self.wrapped, Some(AssocCtxt::Trait), visitor)
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 noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1337-
noop_flat_map_item(self.wrapped, visitor)
1337+
noop_flat_map_item(self.wrapped, Some(AssocCtxt::Impl), visitor)
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 noop_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1375-
noop_flat_map_item(self, visitor)
1375+
noop_flat_map_item(self, None, visitor)
13761376
}
13771377
fn is_mac_call(&self) -> bool {
13781378
matches!(self.kind, ForeignItemKind::MacCall(..))

compiler/rustc_expand/src/placeholders.rs

Lines changed: 3 additions & 3 deletions
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-
_ => noop_flat_map_item(item, self),
270+
_ => noop_flat_map_item(item, None, self),
271271
}
272272
}
273273

@@ -284,7 +284,7 @@ impl MutVisitor for PlaceholderExpander {
284284
AssocCtxt::Impl => it.make_impl_items(),
285285
}
286286
}
287-
_ => noop_flat_map_item(item, self),
287+
_ => noop_flat_map_item(item, Some(ctxt), self),
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-
_ => noop_flat_map_item(item, self),
297+
_ => noop_flat_map_item(item, None, self),
298298
}
299299
}
300300

0 commit comments

Comments
 (0)