Skip to content

Commit a3e7510

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

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
}
@@ -390,13 +394,6 @@ fn visit_bounds<T: MutVisitor>(bounds: &mut GenericBounds, ctxt: BoundKind, vis:
390394
visit_vec(bounds, |bound| vis.visit_param_bound(bound, ctxt));
391395
}
392396

393-
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
394-
fn visit_fn_sig<T: MutVisitor>(FnSig { header, decl, span }: &mut FnSig, vis: &mut T) {
395-
vis.visit_fn_header(header);
396-
vis.visit_fn_decl(decl);
397-
vis.visit_span(span);
398-
}
399-
400397
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
401398
fn visit_attr_args<T: MutVisitor>(args: &mut AttrArgs, vis: &mut T) {
402399
match args {
@@ -891,6 +888,26 @@ fn noop_visit_coroutine_kind<T: MutVisitor>(coroutine_kind: &mut CoroutineKind,
891888
}
892889
}
893890

891+
fn noop_visit_fn<T: MutVisitor>(kind: FnKind<'_>, vis: &mut T) {
892+
match kind {
893+
FnKind::Fn(_ctxt, FnSig { header, decl, span }, generics, body) => {
894+
// Identifier and visibility are visited as a part of the item.
895+
vis.visit_fn_header(header);
896+
vis.visit_generics(generics);
897+
vis.visit_fn_decl(decl);
898+
if let Some(body) = body {
899+
vis.visit_block(body);
900+
}
901+
vis.visit_span(span);
902+
}
903+
FnKind::Closure(binder, decl, body) => {
904+
vis.visit_closure_binder(binder);
905+
vis.visit_fn_decl(decl);
906+
vis.visit_expr(body);
907+
}
908+
}
909+
}
910+
894911
fn noop_visit_fn_decl<T: MutVisitor>(decl: &mut P<FnDecl>, vis: &mut T) {
895912
let FnDecl { inputs, output } = decl.deref_mut();
896913
inputs.flat_map_in_place(|param| vis.flat_map_param(param));
@@ -1073,11 +1090,12 @@ pub fn noop_visit_block<T: MutVisitor>(block: &mut P<Block>, vis: &mut T) {
10731090
}
10741091

10751092
pub fn noop_visit_item_kind(kind: &mut impl NoopVisitItemKind, vis: &mut impl MutVisitor) {
1076-
kind.noop_visit(vis)
1093+
kind.noop_visit(None, vis)
10771094
}
10781095

10791096
impl NoopVisitItemKind for ItemKind {
1080-
fn noop_visit(&mut self, vis: &mut impl MutVisitor) {
1097+
fn noop_visit(&mut self, ctxt: Option<AssocCtxt>, vis: &mut impl MutVisitor) {
1098+
assert_eq!(ctxt, None);
10811099
match self {
10821100
ItemKind::ExternCrate(_orig_name) => {}
10831101
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
@@ -1090,9 +1108,7 @@ impl NoopVisitItemKind for ItemKind {
10901108
}
10911109
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
10921110
visit_defaultness(defaultness, vis);
1093-
vis.visit_generics(generics);
1094-
visit_fn_sig(sig, vis);
1095-
visit_opt(body, |body| vis.visit_block(body));
1111+
vis.visit_fn(FnKind::Fn(FnCtxt::Free, sig, generics, body));
10961112
}
10971113
ItemKind::Mod(safety, mod_kind) => {
10981114
visit_safety(safety, vis);
@@ -1191,16 +1207,15 @@ impl NoopVisitItemKind for ItemKind {
11911207
}
11921208

11931209
impl NoopVisitItemKind for AssocItemKind {
1194-
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
1210+
fn noop_visit(&mut self, ctxt: Option<AssocCtxt>, visitor: &mut impl MutVisitor) {
1211+
let ctxt = ctxt.unwrap();
11951212
match self {
11961213
AssocItemKind::Const(item) => {
11971214
visit_const_item(item, visitor);
11981215
}
11991216
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
12001217
visit_defaultness(defaultness, visitor);
1201-
visitor.visit_generics(generics);
1202-
visit_fn_sig(sig, visitor);
1203-
visit_opt(body, |body| visitor.visit_block(body));
1218+
visitor.visit_fn(FnKind::Fn(FnCtxt::Assoc(ctxt), sig, generics, body));
12041219
}
12051220
AssocItemKind::Type(box TyAlias {
12061221
defaultness,
@@ -1283,31 +1298,31 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
12831298
// Mutates one item into possibly many items.
12841299
pub fn noop_flat_map_item<K: NoopVisitItemKind>(
12851300
mut item: P<Item<K>>,
1301+
ctxt: Option<AssocCtxt>,
12861302
visitor: &mut impl MutVisitor,
12871303
) -> SmallVec<[P<Item<K>>; 1]> {
12881304
let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
12891305
visitor.visit_id(id);
12901306
visit_attrs(attrs, visitor);
12911307
visitor.visit_vis(vis);
12921308
visitor.visit_ident(ident);
1293-
kind.noop_visit(visitor);
1309+
kind.noop_visit(ctxt, visitor);
12941310
visit_lazy_tts(tokens, visitor);
12951311
visitor.visit_span(span);
12961312
smallvec![item]
12971313
}
12981314

12991315
impl NoopVisitItemKind for ForeignItemKind {
1300-
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
1316+
fn noop_visit(&mut self, ctxt: Option<AssocCtxt>, visitor: &mut impl MutVisitor) {
1317+
assert_eq!(ctxt, None);
13011318
match self {
13021319
ForeignItemKind::Static(box StaticItem { ty, mutability: _, expr, safety: _ }) => {
13031320
visitor.visit_ty(ty);
13041321
visit_opt(expr, |expr| visitor.visit_expr(expr));
13051322
}
13061323
ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
13071324
visit_defaultness(defaultness, visitor);
1308-
visitor.visit_generics(generics);
1309-
visit_fn_sig(sig, visitor);
1310-
visit_opt(body, |body| visitor.visit_block(body));
1325+
visitor.visit_fn(FnKind::Fn(FnCtxt::Foreign, sig, generics, body));
13111326
}
13121327
ForeignItemKind::TyAlias(box TyAlias {
13131328
defaultness,
@@ -1517,12 +1532,10 @@ pub fn noop_visit_expr<T: MutVisitor>(
15171532
fn_decl_span,
15181533
fn_arg_span,
15191534
}) => {
1520-
vis.visit_closure_binder(binder);
15211535
visit_constness(constness, vis);
15221536
coroutine_kind.as_mut().map(|coroutine_kind| vis.visit_coroutine_kind(coroutine_kind));
15231537
vis.visit_capture_by(capture_clause);
1524-
vis.visit_fn_decl(fn_decl);
1525-
vis.visit_expr(body);
1538+
vis.visit_fn(FnKind::Closure(binder, fn_decl, body));
15261539
vis.visit_span(fn_decl_span);
15271540
vis.visit_span(fn_arg_span);
15281541
}
@@ -1779,3 +1792,12 @@ impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNo
17791792
crate::ast_traits::AstNodeWrapper::new(N::dummy(), T::dummy())
17801793
}
17811794
}
1795+
1796+
#[derive(Debug)]
1797+
pub enum FnKind<'a> {
1798+
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
1799+
Fn(FnCtxt, &'a mut FnSig, &'a mut Generics, &'a mut Option<P<Block>>),
1800+
1801+
/// E.g., `|x, y| body`.
1802+
Closure(&'a mut ClosureBinder, &'a mut P<FnDecl>, &'a mut P<Expr>),
1803+
}

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)