Skip to content

Commit 5241d8b

Browse files
committed
Merge impl and trait item mut visitor methods to mirror immut visitor
1 parent aee3dc4 commit 5241d8b

File tree

6 files changed

+62
-65
lines changed

6 files changed

+62
-65
lines changed

compiler/rustc_ast/src/mut_visit.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +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;
1415

1516
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1617
use rustc_data_structures::stack::ensure_sufficient_stack;
@@ -109,11 +110,11 @@ pub trait MutVisitor: Sized {
109110
noop_flat_map_field_def(fd, self)
110111
}
111112

112-
fn flat_map_trait_item(&mut self, i: P<AssocItem>) -> SmallVec<[P<AssocItem>; 1]> {
113-
noop_flat_map_item(i, self)
114-
}
115-
116-
fn flat_map_impl_item(&mut self, i: P<AssocItem>) -> SmallVec<[P<AssocItem>; 1]> {
113+
fn flat_map_assoc_item(
114+
&mut self,
115+
i: P<AssocItem>,
116+
_ctxt: AssocCtxt,
117+
) -> SmallVec<[P<AssocItem>; 1]> {
117118
noop_flat_map_item(i, self)
118119
}
119120

@@ -1127,13 +1128,13 @@ impl NoopVisitItemKind for ItemKind {
11271128
visit_polarity(polarity, vis);
11281129
visit_opt(of_trait, |trait_ref| vis.visit_trait_ref(trait_ref));
11291130
vis.visit_ty(self_ty);
1130-
items.flat_map_in_place(|item| vis.flat_map_impl_item(item));
1131+
items.flat_map_in_place(|item| vis.flat_map_assoc_item(item, AssocCtxt::Impl));
11311132
}
11321133
ItemKind::Trait(box Trait { safety, is_auto: _, generics, bounds, items }) => {
11331134
visit_safety(safety, vis);
11341135
vis.visit_generics(generics);
11351136
visit_bounds(bounds, vis);
1136-
items.flat_map_in_place(|item| vis.flat_map_trait_item(item));
1137+
items.flat_map_in_place(|item| vis.flat_map_assoc_item(item, AssocCtxt::Trait));
11371138
}
11381139
ItemKind::TraitAlias(generics, bounds) => {
11391140
vis.visit_generics(generics);

compiler/rustc_builtin_macros/src/cfg_eval.rs

+15-17
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| {
@@ -244,11 +242,11 @@ impl MutVisitor for CfgEval<'_> {
244242
mut_visit::noop_flat_map_item(configure!(self, item), self)
245243
}
246244

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)
249-
}
250-
251-
fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
245+
fn flat_map_assoc_item(
246+
&mut self,
247+
item: P<ast::AssocItem>,
248+
_ctxt: AssocCtxt,
249+
) -> SmallVec<[P<ast::AssocItem>; 1]> {
252250
mut_visit::noop_flat_map_item(configure!(self, item), self)
253251
}
254252

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
}

compiler/rustc_expand/src/expand.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ macro_rules! ast_fragments {
140140
AstFragment::MethodReceiverExpr(expr) => vis.visit_method_receiver_expr(expr),
141141
$($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
142142
$($(AstFragment::$Kind(ast) =>
143-
ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast)),)?)*
143+
ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast, $($args)*)),)?)*
144144
}
145145
}
146146

@@ -177,13 +177,13 @@ ast_fragments! {
177177
}
178178
TraitItems(SmallVec<[P<ast::AssocItem>; 1]>) {
179179
"trait item";
180-
many fn flat_map_trait_item;
180+
many fn flat_map_assoc_item;
181181
fn visit_assoc_item(AssocCtxt::Trait);
182182
fn make_trait_items;
183183
}
184184
ImplItems(SmallVec<[P<ast::AssocItem>; 1]>) {
185185
"impl item";
186-
many fn flat_map_impl_item;
186+
many fn flat_map_assoc_item;
187187
fn visit_assoc_item(AssocCtxt::Impl);
188188
fn make_impl_items;
189189
}
@@ -833,7 +833,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
833833
self.cx, deleg, &item, &suffixes, item.span, true,
834834
);
835835
fragment_kind.expect_from_annotatables(
836-
single_delegations.map(|item| Annotatable::ImplItem(P(item))),
836+
single_delegations.map(|item| Annotatable::AssocItem(P(item), AssocCtxt::Impl)),
837837
)
838838
}
839839
})
@@ -843,8 +843,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
843843
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
844844
let kind = match item {
845845
Annotatable::Item(_)
846-
| Annotatable::TraitItem(_)
847-
| Annotatable::ImplItem(_)
846+
| Annotatable::AssocItem(..)
848847
| Annotatable::ForeignItem(_)
849848
| Annotatable::Crate(..) => return,
850849
Annotatable::Stmt(stmt) => {
@@ -1288,7 +1287,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag>
12881287
type ItemKind = AssocItemKind;
12891288
const KIND: AstFragmentKind = AstFragmentKind::TraitItems;
12901289
fn to_annotatable(self) -> Annotatable {
1291-
Annotatable::TraitItem(self.wrapped)
1290+
Annotatable::AssocItem(self.wrapped, AssocCtxt::Trait)
12921291
}
12931292
fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
12941293
fragment.make_trait_items()
@@ -1329,7 +1328,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag>
13291328
type ItemKind = AssocItemKind;
13301329
const KIND: AstFragmentKind = AstFragmentKind::ImplItems;
13311330
fn to_annotatable(self) -> Annotatable {
1332-
Annotatable::ImplItem(self.wrapped)
1331+
Annotatable::AssocItem(self.wrapped, AssocCtxt::Impl)
13331332
}
13341333
fn fragment_to_output(fragment: AstFragment) -> Self::OutputTy {
13351334
fragment.make_impl_items()
@@ -1993,9 +1992,9 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
19931992
let traitless_qself =
19941993
matches!(&deleg.qself, Some(qself) if qself.position == 0);
19951994
let item = match node.to_annotatable() {
1996-
Annotatable::ImplItem(item) => item,
1995+
Annotatable::AssocItem(item, AssocCtxt::Impl) => item,
19971996
ann @ (Annotatable::Item(_)
1998-
| Annotatable::TraitItem(_)
1997+
| Annotatable::AssocItem(..)
19991998
| Annotatable::Stmt(_)) => {
20001999
let span = ann.span();
20012000
self.cx.dcx().emit_err(GlobDelegationOutsideImpls { span });
@@ -2081,12 +2080,15 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
20812080
self.flat_map_node(node)
20822081
}
20832082

2084-
fn flat_map_trait_item(&mut self, node: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
2085-
self.flat_map_node(AstNodeWrapper::new(node, TraitItemTag))
2086-
}
2087-
2088-
fn flat_map_impl_item(&mut self, node: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
2089-
self.flat_map_node(AstNodeWrapper::new(node, ImplItemTag))
2083+
fn flat_map_assoc_item(
2084+
&mut self,
2085+
node: P<ast::AssocItem>,
2086+
ctxt: AssocCtxt,
2087+
) -> SmallVec<[P<ast::AssocItem>; 1]> {
2088+
match ctxt {
2089+
AssocCtxt::Trait => self.flat_map_node(AstNodeWrapper::new(node, TraitItemTag)),
2090+
AssocCtxt::Impl => self.flat_map_node(AstNodeWrapper::new(node, ImplItemTag)),
2091+
}
20902092
}
20912093

20922094
fn flat_map_foreign_item(

compiler/rustc_expand/src/placeholders.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::expand::{AstFragment, AstFragmentKind};
2-
use rustc_ast as ast;
32
use rustc_ast::mut_visit::*;
43
use rustc_ast::ptr::P;
54
use rustc_ast::token::Delimiter;
5+
use rustc_ast::{self as ast, visit::AssocCtxt};
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_span::symbol::Ident;
88
use rustc_span::DUMMY_SP;
@@ -271,16 +271,19 @@ impl MutVisitor for PlaceholderExpander {
271271
}
272272
}
273273

274-
fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
275-
match item.kind {
276-
ast::AssocItemKind::MacCall(_) => self.remove(item.id).make_trait_items(),
277-
_ => noop_flat_map_item(item, self),
278-
}
279-
}
280-
281-
fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
274+
fn flat_map_assoc_item(
275+
&mut self,
276+
item: P<ast::AssocItem>,
277+
ctxt: AssocCtxt,
278+
) -> SmallVec<[P<ast::AssocItem>; 1]> {
282279
match item.kind {
283-
ast::AssocItemKind::MacCall(_) => self.remove(item.id).make_impl_items(),
280+
ast::AssocItemKind::MacCall(_) => {
281+
let it = self.remove(item.id);
282+
match ctxt {
283+
AssocCtxt::Trait => it.make_trait_items(),
284+
AssocCtxt::Impl => it.make_impl_items(),
285+
}
286+
}
284287
_ => noop_flat_map_item(item, self),
285288
}
286289
}

0 commit comments

Comments
 (0)