Skip to content

Commit 6180173

Browse files
committed
Add WalkItemKind::Ctxt so AssocCtxt is not sent to non-Assoc ItemKinds
1 parent 1236656 commit 6180173

File tree

7 files changed

+35
-27
lines changed

7 files changed

+35
-27
lines changed

compiler/rustc_ast/src/mut_visit.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ impl<A: Array> ExpectOne<A> for SmallVec<A> {
3737
}
3838

3939
pub trait WalkItemKind {
40+
type Ctxt;
4041
fn walk(
4142
&mut self,
4243
span: Span,
4344
id: NodeId,
4445
ident: &mut Ident,
4546
visibility: &mut Visibility,
46-
ctxt: AssocCtxt,
47+
ctxt: Self::Ctxt,
4748
visitor: &mut impl MutVisitor,
4849
);
4950
}
@@ -1088,26 +1089,27 @@ pub fn walk_block<T: MutVisitor>(vis: &mut T, block: &mut P<Block>) {
10881089
vis.visit_span(span);
10891090
}
10901091

1091-
pub fn walk_item_kind(
1092-
kind: &mut impl WalkItemKind,
1092+
pub fn walk_item_kind<K: WalkItemKind>(
1093+
kind: &mut K,
10931094
span: Span,
10941095
id: NodeId,
10951096
ident: &mut Ident,
10961097
visibility: &mut Visibility,
1097-
ctxt: AssocCtxt,
1098+
ctxt: K::Ctxt,
10981099
vis: &mut impl MutVisitor,
10991100
) {
11001101
kind.walk(span, id, ident, visibility, ctxt, vis)
11011102
}
11021103

11031104
impl WalkItemKind for ItemKind {
1105+
type Ctxt = ();
11041106
fn walk(
11051107
&mut self,
11061108
span: Span,
11071109
id: NodeId,
11081110
ident: &mut Ident,
11091111
visibility: &mut Visibility,
1110-
_ctxt: AssocCtxt,
1112+
_ctxt: Self::Ctxt,
11111113
vis: &mut impl MutVisitor,
11121114
) {
11131115
match self {
@@ -1225,13 +1227,14 @@ impl WalkItemKind for ItemKind {
12251227
}
12261228

12271229
impl WalkItemKind for AssocItemKind {
1230+
type Ctxt = AssocCtxt;
12281231
fn walk(
12291232
&mut self,
12301233
span: Span,
12311234
id: NodeId,
12321235
ident: &mut Ident,
12331236
visibility: &mut Visibility,
1234-
ctxt: AssocCtxt,
1237+
ctxt: Self::Ctxt,
12351238
visitor: &mut impl MutVisitor,
12361239
) {
12371240
match self {
@@ -1324,17 +1327,17 @@ pub fn walk_crate<T: MutVisitor>(vis: &mut T, krate: &mut Crate) {
13241327
vis.visit_span(inject_use_span);
13251328
}
13261329

1327-
pub fn walk_flat_map_item<K: WalkItemKind>(
1330+
pub fn walk_flat_map_item<K: WalkItemKind<Ctxt = ()>>(
13281331
visitor: &mut impl MutVisitor,
13291332
item: P<Item<K>>,
13301333
) -> SmallVec<[P<Item<K>>; 1]> {
1331-
walk_flat_map_assoc_item(visitor, item, AssocCtxt::Trait /* ignored */)
1334+
walk_flat_map_assoc_item(visitor, item, ())
13321335
}
13331336

13341337
pub fn walk_flat_map_assoc_item<K: WalkItemKind>(
13351338
visitor: &mut impl MutVisitor,
13361339
mut item: P<Item<K>>,
1337-
ctxt: AssocCtxt,
1340+
ctxt: K::Ctxt,
13381341
) -> SmallVec<[P<Item<K>>; 1]> {
13391342
let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
13401343
visitor.visit_id(id);
@@ -1348,13 +1351,14 @@ pub fn walk_flat_map_assoc_item<K: WalkItemKind>(
13481351
}
13491352

13501353
impl WalkItemKind for ForeignItemKind {
1354+
type Ctxt = ();
13511355
fn walk(
13521356
&mut self,
13531357
span: Span,
13541358
id: NodeId,
13551359
ident: &mut Ident,
13561360
visibility: &mut Visibility,
1357-
_ctxt: AssocCtxt,
1361+
_ctxt: Self::Ctxt,
13581362
visitor: &mut impl MutVisitor,
13591363
) {
13601364
match self {

compiler/rustc_ast/src/visit.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ pub enum LifetimeCtxt {
113113
}
114114

115115
pub trait WalkItemKind: Sized {
116+
type Ctxt;
116117
fn walk<'a, V: Visitor<'a>>(
117118
&'a self,
118119
item: &'a Item<Self>,
119-
ctxt: AssocCtxt,
120+
ctxt: Self::Ctxt,
120121
visitor: &mut V,
121122
) -> V::Result;
122123
}
@@ -337,10 +338,11 @@ pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitR
337338
}
338339

339340
impl WalkItemKind for ItemKind {
341+
type Ctxt = ();
340342
fn walk<'a, V: Visitor<'a>>(
341343
&'a self,
342344
item: &'a Item<Self>,
343-
_ctxt: AssocCtxt,
345+
_ctxt: Self::Ctxt,
344346
visitor: &mut V,
345347
) -> V::Result {
346348
let Item { id, span, vis, ident, .. } = item;
@@ -449,9 +451,9 @@ impl WalkItemKind for ItemKind {
449451

450452
pub fn walk_item<'a, V: Visitor<'a>>(
451453
visitor: &mut V,
452-
item: &'a Item<impl WalkItemKind>,
454+
item: &'a Item<impl WalkItemKind<Ctxt = ()>>,
453455
) -> V::Result {
454-
walk_assoc_item(visitor, item, AssocCtxt::Trait /*ignored*/)
456+
walk_assoc_item(visitor, item, ())
455457
}
456458

457459
pub fn walk_enum_def<'a, V: Visitor<'a>>(
@@ -681,10 +683,11 @@ pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) -> V::Res
681683
}
682684

683685
impl WalkItemKind for ForeignItemKind {
686+
type Ctxt = ();
684687
fn walk<'a, V: Visitor<'a>>(
685688
&'a self,
686689
item: &'a Item<Self>,
687-
_ctxt: AssocCtxt,
690+
_ctxt: Self::Ctxt,
688691
visitor: &mut V,
689692
) -> V::Result {
690693
let Item { id, span, ident, vis, .. } = item;
@@ -844,10 +847,11 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Resu
844847
}
845848

846849
impl WalkItemKind for AssocItemKind {
850+
type Ctxt = AssocCtxt;
847851
fn walk<'a, V: Visitor<'a>>(
848852
&'a self,
849853
item: &'a Item<Self>,
850-
ctxt: AssocCtxt,
854+
ctxt: Self::Ctxt,
851855
visitor: &mut V,
852856
) -> V::Result {
853857
let Item { id, span, ident, vis, .. } = item;
@@ -906,10 +910,10 @@ impl WalkItemKind for AssocItemKind {
906910
}
907911
}
908912

909-
pub fn walk_assoc_item<'a, V: Visitor<'a>>(
913+
pub fn walk_assoc_item<'a, V: Visitor<'a>, K: WalkItemKind>(
910914
visitor: &mut V,
911-
item: &'a Item<impl WalkItemKind>,
912-
ctxt: AssocCtxt,
915+
item: &'a Item<K>,
916+
ctxt: K::Ctxt,
913917
) -> V::Result {
914918
let Item { id: _, span: _, ident, vis, attrs, kind, tokens: _ } = item;
915919
walk_list!(visitor, visit_attribute, attrs);

compiler/rustc_builtin_macros/src/cfg_eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ impl MutVisitor for CfgEval<'_> {
247247
fn flat_map_assoc_item(
248248
&mut self,
249249
item: P<ast::AssocItem>,
250-
_ctxt: AssocCtxt,
250+
ctxt: AssocCtxt,
251251
) -> SmallVec<[P<ast::AssocItem>; 1]> {
252252
let item = configure!(self, item);
253-
mut_visit::walk_flat_map_item(self, item)
253+
mut_visit::walk_flat_map_assoc_item(self, item, ctxt)
254254
}
255255

256256
fn flat_map_foreign_item(

compiler/rustc_builtin_macros/src/test_harness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast as ast;
66
use rustc_ast::entry::EntryPointType;
77
use rustc_ast::mut_visit::*;
88
use rustc_ast::ptr::P;
9-
use rustc_ast::visit::{AssocCtxt, Visitor, walk_item};
9+
use rustc_ast::visit::{Visitor, walk_item};
1010
use rustc_ast::{ModKind, attr};
1111
use rustc_errors::DiagCtxtHandle;
1212
use rustc_expand::base::{ExtCtxt, ResolverExpand};
@@ -150,7 +150,7 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
150150
item.id,
151151
&mut item.ident,
152152
&mut item.vis,
153-
AssocCtxt::Trait, /* ignored */
153+
(),
154154
self,
155155
);
156156
self.add_test_cases(item.id, span, prev_tests);

compiler/rustc_expand/src/expand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag>
13031303
fragment.make_trait_items()
13041304
}
13051305
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1306-
walk_flat_map_item(visitor, self.wrapped)
1306+
walk_flat_map_assoc_item(visitor, self.wrapped, AssocCtxt::Trait)
13071307
}
13081308
fn is_mac_call(&self) -> bool {
13091309
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))
@@ -1344,7 +1344,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag>
13441344
fragment.make_impl_items()
13451345
}
13461346
fn walk_flat_map<V: MutVisitor>(self, visitor: &mut V) -> Self::OutputTy {
1347-
walk_flat_map_item(visitor, self.wrapped)
1347+
walk_flat_map_assoc_item(visitor, self.wrapped, AssocCtxt::Impl)
13481348
}
13491349
fn is_mac_call(&self) -> bool {
13501350
matches!(self.wrapped.kind, AssocItemKind::MacCall(..))

compiler/rustc_expand/src/placeholders.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl MutVisitor for PlaceholderExpander {
286286
AssocCtxt::Impl => it.make_impl_items(),
287287
}
288288
}
289-
_ => walk_flat_map_item(self, item),
289+
_ => walk_flat_map_assoc_item(self, item, ctxt),
290290
}
291291
}
292292

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13241324
// This way they can use `macro_rules` defined later.
13251325
self.visit_vis(&item.vis);
13261326
self.visit_ident(&item.ident);
1327-
item.kind.walk(item, AssocCtxt::Trait, self);
1327+
item.kind.walk(item, (), self);
13281328
visit::walk_list!(self, visit_attribute, &item.attrs);
13291329
}
13301330
_ => visit::walk_item(self, item),

0 commit comments

Comments
 (0)