Skip to content

Commit d24930c

Browse files
committed
Auto merge of #127524 - oli-obk:feed_item_attrs2, r=petrochenkov
Make ast `MutVisitor` have the same method name and style as `Visitor` It doesn't map 100% because some `MutVisitor` methods can filter or even expand to multiple items, but consistency seems nicer. tracking issue: #127615
2 parents 08a9ca7 + e9f32d0 commit d24930c

File tree

12 files changed

+478
-445
lines changed

12 files changed

+478
-445
lines changed

compiler/rustc_ast/src/mut_visit.rs

+298-273
Large diffs are not rendered by default.

compiler/rustc_builtin_macros/src/cfg_eval.rs

+40-31
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::ops::ControlFlow;
44
use rustc_ast as ast;
55
use rustc_ast::mut_visit::MutVisitor;
66
use rustc_ast::ptr::P;
7-
use rustc_ast::visit::Visitor;
7+
use rustc_ast::visit::{AssocCtxt, Visitor};
88
use rustc_ast::NodeId;
99
use rustc_ast::{mut_visit, visit};
1010
use rustc_ast::{Attribute, HasAttrs, HasTokens};
@@ -53,11 +53,8 @@ fn flat_map_annotatable(
5353
) -> Option<Annotatable> {
5454
match annotatable {
5555
Annotatable::Item(item) => vis.flat_map_item(item).pop().map(Annotatable::Item),
56-
Annotatable::TraitItem(item) => {
57-
vis.flat_map_trait_item(item).pop().map(Annotatable::TraitItem)
58-
}
59-
Annotatable::ImplItem(item) => {
60-
vis.flat_map_impl_item(item).pop().map(Annotatable::ImplItem)
56+
Annotatable::AssocItem(item, ctxt) => {
57+
Some(Annotatable::AssocItem(vis.flat_map_assoc_item(item, ctxt).pop()?, ctxt))
6158
}
6259
Annotatable::ForeignItem(item) => {
6360
vis.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem)
@@ -106,8 +103,7 @@ fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {
106103

107104
let res = match annotatable {
108105
Annotatable::Item(item) => CfgFinder.visit_item(item),
109-
Annotatable::TraitItem(item) => CfgFinder.visit_assoc_item(item, visit::AssocCtxt::Trait),
110-
Annotatable::ImplItem(item) => CfgFinder.visit_assoc_item(item, visit::AssocCtxt::Impl),
106+
Annotatable::AssocItem(item, ctxt) => CfgFinder.visit_assoc_item(item, *ctxt),
111107
Annotatable::ForeignItem(item) => CfgFinder.visit_foreign_item(item),
112108
Annotatable::Stmt(stmt) => CfgFinder.visit_stmt(stmt),
113109
Annotatable::Expr(expr) => CfgFinder.visit_expr(expr),
@@ -150,14 +146,16 @@ impl CfgEval<'_> {
150146
Annotatable::Item(_) => {
151147
|parser| Ok(Annotatable::Item(parser.parse_item(ForceCollect::Yes)?.unwrap()))
152148
}
153-
Annotatable::TraitItem(_) => |parser| {
154-
Ok(Annotatable::TraitItem(
149+
Annotatable::AssocItem(_, AssocCtxt::Trait) => |parser| {
150+
Ok(Annotatable::AssocItem(
155151
parser.parse_trait_item(ForceCollect::Yes)?.unwrap().unwrap(),
152+
AssocCtxt::Trait,
156153
))
157154
},
158-
Annotatable::ImplItem(_) => |parser| {
159-
Ok(Annotatable::ImplItem(
155+
Annotatable::AssocItem(_, AssocCtxt::Impl) => |parser| {
156+
Ok(Annotatable::AssocItem(
160157
parser.parse_impl_item(ForceCollect::Yes)?.unwrap().unwrap(),
158+
AssocCtxt::Impl,
161159
))
162160
},
163161
Annotatable::ForeignItem(_) => |parser| {
@@ -214,72 +212,83 @@ impl MutVisitor for CfgEval<'_> {
214212
#[instrument(level = "trace", skip(self))]
215213
fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
216214
self.0.configure_expr(expr, false);
217-
mut_visit::noop_visit_expr(expr, self);
215+
mut_visit::walk_expr(self, expr);
218216
}
219217

220218
#[instrument(level = "trace", skip(self))]
221219
fn visit_method_receiver_expr(&mut self, expr: &mut P<ast::Expr>) {
222220
self.0.configure_expr(expr, true);
223-
mut_visit::noop_visit_expr(expr, self);
221+
mut_visit::walk_expr(self, expr);
224222
}
225223

226224
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
227225
let mut expr = configure!(self, expr);
228-
mut_visit::noop_visit_expr(&mut expr, self);
226+
mut_visit::walk_expr(self, &mut expr);
229227
Some(expr)
230228
}
231229

232230
fn flat_map_generic_param(
233231
&mut self,
234232
param: ast::GenericParam,
235233
) -> SmallVec<[ast::GenericParam; 1]> {
236-
mut_visit::noop_flat_map_generic_param(configure!(self, param), self)
234+
let param = configure!(self, param);
235+
mut_visit::walk_flat_map_generic_param(self, param)
237236
}
238237

239238
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
240-
mut_visit::noop_flat_map_stmt(configure!(self, stmt), self)
239+
let stmt = configure!(self, stmt);
240+
mut_visit::walk_flat_map_stmt(self, stmt)
241241
}
242242

243243
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
244-
mut_visit::noop_flat_map_item(configure!(self, item), self)
245-
}
246-
247-
fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
248-
mut_visit::noop_flat_map_item(configure!(self, item), self)
244+
let item = configure!(self, item);
245+
mut_visit::walk_flat_map_item(self, item)
249246
}
250247

251-
fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
252-
mut_visit::noop_flat_map_item(configure!(self, item), self)
248+
fn flat_map_assoc_item(
249+
&mut self,
250+
item: P<ast::AssocItem>,
251+
_ctxt: AssocCtxt,
252+
) -> SmallVec<[P<ast::AssocItem>; 1]> {
253+
let item = configure!(self, item);
254+
mut_visit::walk_flat_map_item(self, item)
253255
}
254256

255257
fn flat_map_foreign_item(
256258
&mut self,
257259
foreign_item: P<ast::ForeignItem>,
258260
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
259-
mut_visit::noop_flat_map_item(configure!(self, foreign_item), self)
261+
let foreign_item = configure!(self, foreign_item);
262+
mut_visit::walk_flat_map_item(self, foreign_item)
260263
}
261264

262265
fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
263-
mut_visit::noop_flat_map_arm(configure!(self, arm), self)
266+
let arm = configure!(self, arm);
267+
mut_visit::walk_flat_map_arm(self, arm)
264268
}
265269

266270
fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
267-
mut_visit::noop_flat_map_expr_field(configure!(self, field), self)
271+
let field = configure!(self, field);
272+
mut_visit::walk_flat_map_expr_field(self, field)
268273
}
269274

270275
fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
271-
mut_visit::noop_flat_map_pat_field(configure!(self, fp), self)
276+
let fp = configure!(self, fp);
277+
mut_visit::walk_flat_map_pat_field(self, fp)
272278
}
273279

274280
fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
275-
mut_visit::noop_flat_map_param(configure!(self, p), self)
281+
let p = configure!(self, p);
282+
mut_visit::walk_flat_map_param(self, p)
276283
}
277284

278285
fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
279-
mut_visit::noop_flat_map_field_def(configure!(self, sf), self)
286+
let sf = configure!(self, sf);
287+
mut_visit::walk_flat_map_field_def(self, sf)
280288
}
281289

282290
fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
283-
mut_visit::noop_flat_map_variant(configure!(self, variant), self)
291+
let variant = configure!(self, variant);
292+
mut_visit::walk_flat_map_variant(self, variant)
284293
}
285294
}

compiler/rustc_builtin_macros/src/test_harness.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ impl TestHarnessGenerator<'_> {
122122
impl<'a> MutVisitor for TestHarnessGenerator<'a> {
123123
fn visit_crate(&mut self, c: &mut ast::Crate) {
124124
let prev_tests = mem::take(&mut self.tests);
125-
noop_visit_crate(c, self);
125+
walk_crate(self, c);
126126
self.add_test_cases(ast::CRATE_NODE_ID, c.spans.inner_span, prev_tests);
127127

128128
// Create a main function to run our tests
129129
c.items.push(mk_main(&mut self.cx));
130130
}
131131

132-
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
133-
let mut item = i.into_inner();
132+
fn flat_map_item(&mut self, mut i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
133+
let item = &mut *i;
134134
if let Some(name) = get_test_name(&item) {
135135
debug!("this is a test item");
136136

@@ -144,13 +144,13 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
144144
item.kind
145145
{
146146
let prev_tests = mem::take(&mut self.tests);
147-
noop_visit_item_kind(&mut item.kind, 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.
151151
walk_item(&mut InnerItemLinter { sess: self.cx.ext_cx.sess }, &item);
152152
}
153-
smallvec![P(item)]
153+
smallvec![i]
154154
}
155155
}
156156

@@ -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 = 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_builtin_macros/src/util.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ pub(crate) fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaI
2727
pub(crate) fn warn_on_duplicate_attribute(ecx: &ExtCtxt<'_>, item: &Annotatable, name: Symbol) {
2828
let attrs: Option<&[Attribute]> = match item {
2929
Annotatable::Item(item) => Some(&item.attrs),
30-
Annotatable::TraitItem(item) => Some(&item.attrs),
31-
Annotatable::ImplItem(item) => Some(&item.attrs),
30+
Annotatable::AssocItem(item, _) => Some(&item.attrs),
3231
Annotatable::ForeignItem(item) => Some(&item.attrs),
3332
Annotatable::Expr(expr) => Some(&expr.attrs),
3433
Annotatable::Arm(arm) => Some(&arm.attrs),

compiler/rustc_expand/src/base.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ use thin_vec::ThinVec;
3737
#[derive(Debug, Clone)]
3838
pub enum Annotatable {
3939
Item(P<ast::Item>),
40-
TraitItem(P<ast::AssocItem>),
41-
ImplItem(P<ast::AssocItem>),
40+
AssocItem(P<ast::AssocItem>, AssocCtxt),
4241
ForeignItem(P<ast::ForeignItem>),
4342
Stmt(P<ast::Stmt>),
4443
Expr(P<ast::Expr>),
@@ -56,8 +55,7 @@ impl Annotatable {
5655
pub fn span(&self) -> Span {
5756
match self {
5857
Annotatable::Item(item) => item.span,
59-
Annotatable::TraitItem(trait_item) => trait_item.span,
60-
Annotatable::ImplItem(impl_item) => impl_item.span,
58+
Annotatable::AssocItem(assoc_item, _) => assoc_item.span,
6159
Annotatable::ForeignItem(foreign_item) => foreign_item.span,
6260
Annotatable::Stmt(stmt) => stmt.span,
6361
Annotatable::Expr(expr) => expr.span,
@@ -75,8 +73,7 @@ impl Annotatable {
7573
pub fn visit_attrs(&mut self, f: impl FnOnce(&mut AttrVec)) {
7674
match self {
7775
Annotatable::Item(item) => item.visit_attrs(f),
78-
Annotatable::TraitItem(trait_item) => trait_item.visit_attrs(f),
79-
Annotatable::ImplItem(impl_item) => impl_item.visit_attrs(f),
76+
Annotatable::AssocItem(assoc_item, _) => assoc_item.visit_attrs(f),
8077
Annotatable::ForeignItem(foreign_item) => foreign_item.visit_attrs(f),
8178
Annotatable::Stmt(stmt) => stmt.visit_attrs(f),
8279
Annotatable::Expr(expr) => expr.visit_attrs(f),
@@ -94,8 +91,7 @@ impl Annotatable {
9491
pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) -> V::Result {
9592
match self {
9693
Annotatable::Item(item) => visitor.visit_item(item),
97-
Annotatable::TraitItem(item) => visitor.visit_assoc_item(item, AssocCtxt::Trait),
98-
Annotatable::ImplItem(item) => visitor.visit_assoc_item(item, AssocCtxt::Impl),
94+
Annotatable::AssocItem(item, ctxt) => visitor.visit_assoc_item(item, *ctxt),
9995
Annotatable::ForeignItem(foreign_item) => visitor.visit_foreign_item(foreign_item),
10096
Annotatable::Stmt(stmt) => visitor.visit_stmt(stmt),
10197
Annotatable::Expr(expr) => visitor.visit_expr(expr),
@@ -113,9 +109,7 @@ impl Annotatable {
113109
pub fn to_tokens(&self) -> TokenStream {
114110
match self {
115111
Annotatable::Item(node) => TokenStream::from_ast(node),
116-
Annotatable::TraitItem(node) | Annotatable::ImplItem(node) => {
117-
TokenStream::from_ast(node)
118-
}
112+
Annotatable::AssocItem(node, _) => TokenStream::from_ast(node),
119113
Annotatable::ForeignItem(node) => TokenStream::from_ast(node),
120114
Annotatable::Stmt(node) => {
121115
assert!(!matches!(node.kind, ast::StmtKind::Empty));
@@ -142,14 +136,14 @@ impl Annotatable {
142136

143137
pub fn expect_trait_item(self) -> P<ast::AssocItem> {
144138
match self {
145-
Annotatable::TraitItem(i) => i,
139+
Annotatable::AssocItem(i, AssocCtxt::Trait) => i,
146140
_ => panic!("expected Item"),
147141
}
148142
}
149143

150144
pub fn expect_impl_item(self) -> P<ast::AssocItem> {
151145
match self {
152-
Annotatable::ImplItem(i) => i,
146+
Annotatable::AssocItem(i, AssocCtxt::Impl) => i,
153147
_ => panic!("expected Item"),
154148
}
155149
}

0 commit comments

Comments
 (0)