@@ -23,7 +23,7 @@ use crate::ast::*;
23
23
use crate :: ptr:: P ;
24
24
use crate :: token:: { self , Token } ;
25
25
use crate :: tokenstream:: * ;
26
- use crate :: visit:: { AssocCtxt , BoundKind } ;
26
+ use crate :: visit:: { AssocCtxt , BoundKind , FnCtxt } ;
27
27
28
28
pub trait ExpectOne < A : Array > {
29
29
fn expect_one ( self , err : & ' static str ) -> A :: Item ;
@@ -37,7 +37,16 @@ impl<A: Array> ExpectOne<A> for SmallVec<A> {
37
37
}
38
38
39
39
pub trait WalkItemKind {
40
- fn walk ( & mut self , span : Span , id : NodeId , visitor : & mut impl MutVisitor ) ;
40
+ type Ctxt ;
41
+ fn walk (
42
+ & mut self ,
43
+ span : Span ,
44
+ id : NodeId ,
45
+ ident : & mut Ident ,
46
+ visibility : & mut Visibility ,
47
+ ctxt : Self :: Ctxt ,
48
+ visitor : & mut impl MutVisitor ,
49
+ ) ;
41
50
}
42
51
43
52
pub trait MutVisitor : Sized {
@@ -114,9 +123,9 @@ pub trait MutVisitor: Sized {
114
123
fn flat_map_assoc_item (
115
124
& mut self ,
116
125
i : P < AssocItem > ,
117
- _ctxt : AssocCtxt ,
126
+ ctxt : AssocCtxt ,
118
127
) -> SmallVec < [ P < AssocItem > ; 1 ] > {
119
- walk_flat_map_item ( self , i)
128
+ walk_flat_map_assoc_item ( self , i, ctxt )
120
129
}
121
130
122
131
fn visit_fn_decl ( & mut self , d : & mut P < FnDecl > ) {
@@ -880,7 +889,7 @@ fn walk_coroutine_kind<T: MutVisitor>(vis: &mut T, coroutine_kind: &mut Coroutin
880
889
881
890
fn walk_fn < T : MutVisitor > ( vis : & mut T , kind : FnKind < ' _ > ) {
882
891
match kind {
883
- FnKind :: Fn ( FnSig { header, decl, span } , generics, body) => {
892
+ FnKind :: Fn ( _ctxt , _ident , FnSig { header, decl, span } , _visibility , generics, body) => {
884
893
// Identifier and visibility are visited as a part of the item.
885
894
vis. visit_fn_header ( header) ;
886
895
vis. visit_generics ( generics) ;
@@ -890,8 +899,9 @@ fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
890
899
}
891
900
vis. visit_span ( span) ;
892
901
}
893
- FnKind :: Closure ( binder, decl, body) => {
902
+ FnKind :: Closure ( binder, coroutine_kind , decl, body) => {
894
903
vis. visit_closure_binder ( binder) ;
904
+ coroutine_kind. as_mut ( ) . map ( |coroutine_kind| vis. visit_coroutine_kind ( coroutine_kind) ) ;
895
905
vis. visit_fn_decl ( decl) ;
896
906
vis. visit_expr ( body) ;
897
907
}
@@ -1079,17 +1089,29 @@ pub fn walk_block<T: MutVisitor>(vis: &mut T, block: &mut P<Block>) {
1079
1089
vis. visit_span ( span) ;
1080
1090
}
1081
1091
1082
- pub fn walk_item_kind (
1083
- kind : & mut impl WalkItemKind ,
1092
+ pub fn walk_item_kind < K : WalkItemKind > (
1093
+ kind : & mut K ,
1084
1094
span : Span ,
1085
1095
id : NodeId ,
1096
+ ident : & mut Ident ,
1097
+ visibility : & mut Visibility ,
1098
+ ctxt : K :: Ctxt ,
1086
1099
vis : & mut impl MutVisitor ,
1087
1100
) {
1088
- kind. walk ( span, id, vis)
1101
+ kind. walk ( span, id, ident , visibility , ctxt , vis)
1089
1102
}
1090
1103
1091
1104
impl WalkItemKind for ItemKind {
1092
- fn walk ( & mut self , span : Span , id : NodeId , vis : & mut impl MutVisitor ) {
1105
+ type Ctxt = ( ) ;
1106
+ fn walk (
1107
+ & mut self ,
1108
+ span : Span ,
1109
+ id : NodeId ,
1110
+ ident : & mut Ident ,
1111
+ visibility : & mut Visibility ,
1112
+ _ctxt : Self :: Ctxt ,
1113
+ vis : & mut impl MutVisitor ,
1114
+ ) {
1093
1115
match self {
1094
1116
ItemKind :: ExternCrate ( _orig_name) => { }
1095
1117
ItemKind :: Use ( use_tree) => vis. visit_use_tree ( use_tree) ,
@@ -1102,7 +1124,11 @@ impl WalkItemKind for ItemKind {
1102
1124
}
1103
1125
ItemKind :: Fn ( box Fn { defaultness, generics, sig, body } ) => {
1104
1126
visit_defaultness ( vis, defaultness) ;
1105
- vis. visit_fn ( FnKind :: Fn ( sig, generics, body) , span, id) ;
1127
+ vis. visit_fn (
1128
+ FnKind :: Fn ( FnCtxt :: Free , ident, sig, visibility, generics, body) ,
1129
+ span,
1130
+ id,
1131
+ ) ;
1106
1132
}
1107
1133
ItemKind :: Mod ( safety, mod_kind) => {
1108
1134
visit_safety ( vis, safety) ;
@@ -1201,14 +1227,27 @@ impl WalkItemKind for ItemKind {
1201
1227
}
1202
1228
1203
1229
impl WalkItemKind for AssocItemKind {
1204
- fn walk ( & mut self , span : Span , id : NodeId , visitor : & mut impl MutVisitor ) {
1230
+ type Ctxt = AssocCtxt ;
1231
+ fn walk (
1232
+ & mut self ,
1233
+ span : Span ,
1234
+ id : NodeId ,
1235
+ ident : & mut Ident ,
1236
+ visibility : & mut Visibility ,
1237
+ ctxt : Self :: Ctxt ,
1238
+ visitor : & mut impl MutVisitor ,
1239
+ ) {
1205
1240
match self {
1206
1241
AssocItemKind :: Const ( item) => {
1207
1242
visit_const_item ( item, visitor) ;
1208
1243
}
1209
1244
AssocItemKind :: Fn ( box Fn { defaultness, generics, sig, body } ) => {
1210
1245
visit_defaultness ( visitor, defaultness) ;
1211
- visitor. visit_fn ( FnKind :: Fn ( sig, generics, body) , span, id) ;
1246
+ visitor. visit_fn (
1247
+ FnKind :: Fn ( FnCtxt :: Assoc ( ctxt) , ident, sig, visibility, generics, body) ,
1248
+ span,
1249
+ id,
1250
+ ) ;
1212
1251
}
1213
1252
AssocItemKind :: Type ( box TyAlias {
1214
1253
defaultness,
@@ -1288,32 +1327,52 @@ pub fn walk_crate<T: MutVisitor>(vis: &mut T, krate: &mut Crate) {
1288
1327
vis. visit_span ( inject_use_span) ;
1289
1328
}
1290
1329
1291
- /// Mutates one item, returning the item again.
1292
- pub fn walk_flat_map_item < K : WalkItemKind > (
1330
+ pub fn walk_flat_map_item < K : WalkItemKind < Ctxt = ( ) > > (
1331
+ visitor : & mut impl MutVisitor ,
1332
+ item : P < Item < K > > ,
1333
+ ) -> SmallVec < [ P < Item < K > > ; 1 ] > {
1334
+ walk_flat_map_assoc_item ( visitor, item, ( ) )
1335
+ }
1336
+
1337
+ pub fn walk_flat_map_assoc_item < K : WalkItemKind > (
1293
1338
visitor : & mut impl MutVisitor ,
1294
1339
mut item : P < Item < K > > ,
1340
+ ctxt : K :: Ctxt ,
1295
1341
) -> SmallVec < [ P < Item < K > > ; 1 ] > {
1296
1342
let Item { ident, attrs, id, kind, vis, span, tokens } = item. deref_mut ( ) ;
1297
1343
visitor. visit_id ( id) ;
1298
1344
visit_attrs ( visitor, attrs) ;
1299
1345
visitor. visit_vis ( vis) ;
1300
1346
visitor. visit_ident ( ident) ;
1301
- kind. walk ( * span, * id, visitor) ;
1347
+ kind. walk ( * span, * id, ident , vis , ctxt , visitor) ;
1302
1348
visit_lazy_tts ( visitor, tokens) ;
1303
1349
visitor. visit_span ( span) ;
1304
1350
smallvec ! [ item]
1305
1351
}
1306
1352
1307
1353
impl WalkItemKind for ForeignItemKind {
1308
- fn walk ( & mut self , span : Span , id : NodeId , visitor : & mut impl MutVisitor ) {
1354
+ type Ctxt = ( ) ;
1355
+ fn walk (
1356
+ & mut self ,
1357
+ span : Span ,
1358
+ id : NodeId ,
1359
+ ident : & mut Ident ,
1360
+ visibility : & mut Visibility ,
1361
+ _ctxt : Self :: Ctxt ,
1362
+ visitor : & mut impl MutVisitor ,
1363
+ ) {
1309
1364
match self {
1310
1365
ForeignItemKind :: Static ( box StaticItem { ty, mutability : _, expr, safety : _ } ) => {
1311
1366
visitor. visit_ty ( ty) ;
1312
1367
visit_opt ( expr, |expr| visitor. visit_expr ( expr) ) ;
1313
1368
}
1314
1369
ForeignItemKind :: Fn ( box Fn { defaultness, generics, sig, body } ) => {
1315
1370
visit_defaultness ( visitor, defaultness) ;
1316
- visitor. visit_fn ( FnKind :: Fn ( sig, generics, body) , span, id) ;
1371
+ visitor. visit_fn (
1372
+ FnKind :: Fn ( FnCtxt :: Foreign , ident, sig, visibility, generics, body) ,
1373
+ span,
1374
+ id,
1375
+ ) ;
1317
1376
}
1318
1377
ForeignItemKind :: TyAlias ( box TyAlias {
1319
1378
defaultness,
@@ -1522,9 +1581,8 @@ pub fn walk_expr<T: MutVisitor>(vis: &mut T, Expr { kind, id, span, attrs, token
1522
1581
fn_arg_span,
1523
1582
} ) => {
1524
1583
visit_constness ( vis, constness) ;
1525
- coroutine_kind. as_mut ( ) . map ( |coroutine_kind| vis. visit_coroutine_kind ( coroutine_kind) ) ;
1526
1584
vis. visit_capture_by ( capture_clause) ;
1527
- vis. visit_fn ( FnKind :: Closure ( binder, fn_decl, body) , * span, * id) ;
1585
+ vis. visit_fn ( FnKind :: Closure ( binder, coroutine_kind , fn_decl, body) , * span, * id) ;
1528
1586
vis. visit_span ( fn_decl_span) ;
1529
1587
vis. visit_span ( fn_arg_span) ;
1530
1588
}
@@ -1785,8 +1843,20 @@ impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNo
1785
1843
#[ derive( Debug ) ]
1786
1844
pub enum FnKind < ' a > {
1787
1845
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
1788
- Fn ( & ' a mut FnSig , & ' a mut Generics , & ' a mut Option < P < Block > > ) ,
1846
+ Fn (
1847
+ FnCtxt ,
1848
+ & ' a mut Ident ,
1849
+ & ' a mut FnSig ,
1850
+ & ' a mut Visibility ,
1851
+ & ' a mut Generics ,
1852
+ & ' a mut Option < P < Block > > ,
1853
+ ) ,
1789
1854
1790
1855
/// E.g., `|x, y| body`.
1791
- Closure ( & ' a mut ClosureBinder , & ' a mut P < FnDecl > , & ' a mut P < Expr > ) ,
1856
+ Closure (
1857
+ & ' a mut ClosureBinder ,
1858
+ & ' a mut Option < CoroutineKind > ,
1859
+ & ' a mut P < FnDecl > ,
1860
+ & ' a mut P < Expr > ,
1861
+ ) ,
1792
1862
}
0 commit comments