@@ -475,6 +475,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
475
475
cx : self . cx ,
476
476
invocations : Vec :: new ( ) ,
477
477
monotonic : self . monotonic ,
478
+ tests_nameable : true ,
478
479
} ;
479
480
( fragment. fold_with ( & mut collector) , collector. invocations )
480
481
} ;
@@ -1050,6 +1051,11 @@ struct InvocationCollector<'a, 'b: 'a> {
1050
1051
cfg : StripUnconfigured < ' a > ,
1051
1052
invocations : Vec < Invocation > ,
1052
1053
monotonic : bool ,
1054
+
1055
+ /// Test functions need to be nameable. Tests inside functions or in other
1056
+ /// unnameable locations need to be ignored. `tests_nameable` tracks whether
1057
+ /// any test functions found in the current context would be nameable.
1058
+ tests_nameable : bool ,
1053
1059
}
1054
1060
1055
1061
impl < ' a , ' b > InvocationCollector < ' a , ' b > {
@@ -1067,6 +1073,20 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
1067
1073
placeholder ( fragment_kind, NodeId :: placeholder_from_mark ( mark) )
1068
1074
}
1069
1075
1076
+ /// Folds the item allowing tests to be expanded because they are still nameable.
1077
+ /// This should probably only be called with module items
1078
+ fn fold_nameable ( & mut self , item : P < ast:: Item > ) -> SmallVector < P < ast:: Item > > {
1079
+ fold:: noop_fold_item ( item, self )
1080
+ }
1081
+
1082
+ /// Folds the item but doesn't allow tests to occur within it
1083
+ fn fold_unnameable ( & mut self , item : P < ast:: Item > ) -> SmallVector < P < ast:: Item > > {
1084
+ let was_nameable = mem:: replace ( & mut self . tests_nameable , false ) ;
1085
+ let items = fold:: noop_fold_item ( item, self ) ;
1086
+ self . tests_nameable = was_nameable;
1087
+ items
1088
+ }
1089
+
1070
1090
fn collect_bang ( & mut self , mac : ast:: Mac , span : Span , kind : AstFragmentKind ) -> AstFragment {
1071
1091
self . collect ( kind, InvocationKind :: Bang { mac : mac, ident : None , span : span } )
1072
1092
}
@@ -1307,7 +1327,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
1307
1327
}
1308
1328
ast:: ItemKind :: Mod ( ast:: Mod { inner, .. } ) => {
1309
1329
if item. ident == keywords:: Invalid . ident ( ) {
1310
- return noop_fold_item ( item, self ) ;
1330
+ return self . fold_nameable ( item) ;
1311
1331
}
1312
1332
1313
1333
let orig_directory_ownership = self . cx . current_expansion . directory_ownership ;
@@ -1347,14 +1367,14 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
1347
1367
1348
1368
let orig_module =
1349
1369
mem:: replace ( & mut self . cx . current_expansion . module , Rc :: new ( module) ) ;
1350
- let result = noop_fold_item ( item, self ) ;
1370
+ let result = self . fold_nameable ( item) ;
1351
1371
self . cx . current_expansion . module = orig_module;
1352
1372
self . cx . current_expansion . directory_ownership = orig_directory_ownership;
1353
1373
result
1354
1374
}
1355
1375
// Ensure that test functions are accessible from the test harness.
1356
1376
ast:: ItemKind :: Fn ( ..) if self . cx . ecfg . should_test => {
1357
- if item. attrs . iter ( ) . any ( |attr| is_test_or_bench ( attr) ) {
1377
+ if self . tests_nameable && item. attrs . iter ( ) . any ( |attr| is_test_or_bench ( attr) ) {
1358
1378
let orig_vis = item. vis . clone ( ) ;
1359
1379
1360
1380
// Publicize the item under gensymed name to avoid pollution
@@ -1370,16 +1390,16 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
1370
1390
item. ident . span ,
1371
1391
orig_vis,
1372
1392
Some ( Ident :: from_interned_str ( item. ident . as_interned_str ( ) ) ) ,
1373
- self . cx . path ( item. ident . span , vec ! [ item. ident] ) ) ;
1393
+ self . cx . path ( item. ident . span , vec ! [ Ident :: from_str ( "self" ) , item. ident] ) ) ;
1374
1394
1375
1395
SmallVector :: many (
1376
- noop_fold_item ( item, self ) . into_iter ( )
1377
- . chain ( noop_fold_item ( use_item, self ) ) )
1396
+ self . fold_unnameable ( item) . into_iter ( )
1397
+ . chain ( self . fold_unnameable ( use_item) ) )
1378
1398
} else {
1379
- noop_fold_item ( item, self )
1399
+ self . fold_unnameable ( item)
1380
1400
}
1381
1401
}
1382
- _ => noop_fold_item ( item, self ) ,
1402
+ _ => self . fold_unnameable ( item) ,
1383
1403
}
1384
1404
}
1385
1405
0 commit comments