@@ -750,6 +750,15 @@ fn expand_annotatable(a: Annotatable,
750
750
}
751
751
result. into_iter ( ) . map ( |i| Annotatable :: Item ( i) ) . collect ( )
752
752
} ,
753
+ ast:: ItemKind :: ExternCrate ( _) => {
754
+ // We need to error on `#[macro_use] extern crate` when it isn't at the
755
+ // crate root, because `$crate` won't work properly.
756
+ let allows_macros = fld. cx . syntax_env . is_crate_root ( ) ;
757
+ for def in fld. cx . loader . load_crate ( & it, allows_macros) {
758
+ fld. cx . insert_macro ( def) ;
759
+ }
760
+ SmallVector :: one ( Annotatable :: Item ( it) )
761
+ } ,
753
762
_ => noop_fold_item ( it, fld) . into_iter ( ) . map ( |i| Annotatable :: Item ( i) ) . collect ( ) ,
754
763
} ,
755
764
@@ -1137,8 +1146,6 @@ impl<'feat> ExpansionConfig<'feat> {
1137
1146
}
1138
1147
1139
1148
pub fn expand_crate ( mut cx : ExtCtxt ,
1140
- // these are the macros being imported to this crate:
1141
- imported_macros : Vec < ast:: MacroDef > ,
1142
1149
user_exts : Vec < NamedSyntaxExtension > ,
1143
1150
c : Crate ) -> ( Crate , HashSet < Name > ) {
1144
1151
if std_inject:: no_core ( & c) {
@@ -1151,10 +1158,6 @@ pub fn expand_crate(mut cx: ExtCtxt,
1151
1158
let ret = {
1152
1159
let mut expander = MacroExpander :: new ( & mut cx) ;
1153
1160
1154
- for def in imported_macros {
1155
- expander. cx . insert_macro ( def) ;
1156
- }
1157
-
1158
1161
for ( name, extension) in user_exts {
1159
1162
expander. cx . syntax_env . insert ( name, extension) ;
1160
1163
}
@@ -1220,7 +1223,7 @@ mod tests {
1220
1223
use ast;
1221
1224
use ast:: Name ;
1222
1225
use codemap;
1223
- use ext:: base:: ExtCtxt ;
1226
+ use ext:: base:: { ExtCtxt , DummyMacroLoader } ;
1224
1227
use ext:: mtwt;
1225
1228
use fold:: Folder ;
1226
1229
use parse;
@@ -1291,9 +1294,9 @@ mod tests {
1291
1294
src,
1292
1295
Vec :: new ( ) , & sess) . unwrap ( ) ;
1293
1296
// should fail:
1294
- let mut gated_cfgs = vec ! [ ] ;
1295
- let ecx = ExtCtxt :: new ( & sess, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs) ;
1296
- expand_crate ( ecx, vec ! [ ] , vec ! [ ] , crate_ast) ;
1297
+ let ( mut gated_cfgs, mut loader ) = ( vec ! [ ] , DummyMacroLoader ) ;
1298
+ let ecx = ExtCtxt :: new ( & sess, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs, & mut loader ) ;
1299
+ expand_crate ( ecx, vec ! [ ] , crate_ast) ;
1297
1300
}
1298
1301
1299
1302
// make sure that macros can't escape modules
@@ -1306,9 +1309,9 @@ mod tests {
1306
1309
"<test>" . to_string ( ) ,
1307
1310
src,
1308
1311
Vec :: new ( ) , & sess) . unwrap ( ) ;
1309
- let mut gated_cfgs = vec ! [ ] ;
1310
- let ecx = ExtCtxt :: new ( & sess, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs) ;
1311
- expand_crate ( ecx, vec ! [ ] , vec ! [ ] , crate_ast) ;
1312
+ let ( mut gated_cfgs, mut loader ) = ( vec ! [ ] , DummyMacroLoader ) ;
1313
+ let ecx = ExtCtxt :: new ( & sess, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs, & mut loader ) ;
1314
+ expand_crate ( ecx, vec ! [ ] , crate_ast) ;
1312
1315
}
1313
1316
1314
1317
// macro_use modules should allow macros to escape
@@ -1320,18 +1323,18 @@ mod tests {
1320
1323
"<test>" . to_string ( ) ,
1321
1324
src,
1322
1325
Vec :: new ( ) , & sess) . unwrap ( ) ;
1323
- let mut gated_cfgs = vec ! [ ] ;
1324
- let ecx = ExtCtxt :: new ( & sess, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs) ;
1325
- expand_crate ( ecx, vec ! [ ] , vec ! [ ] , crate_ast) ;
1326
+ let ( mut gated_cfgs, mut loader ) = ( vec ! [ ] , DummyMacroLoader ) ;
1327
+ let ecx = ExtCtxt :: new ( & sess, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs, & mut loader ) ;
1328
+ expand_crate ( ecx, vec ! [ ] , crate_ast) ;
1326
1329
}
1327
1330
1328
1331
fn expand_crate_str ( crate_str : String ) -> ast:: Crate {
1329
1332
let ps = parse:: ParseSess :: new ( ) ;
1330
1333
let crate_ast = panictry ! ( string_to_parser( & ps, crate_str) . parse_crate_mod( ) ) ;
1331
1334
// the cfg argument actually does matter, here...
1332
- let mut gated_cfgs = vec ! [ ] ;
1333
- let ecx = ExtCtxt :: new ( & ps, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs) ;
1334
- expand_crate ( ecx, vec ! [ ] , vec ! [ ] , crate_ast) . 0
1335
+ let ( mut gated_cfgs, mut loader ) = ( vec ! [ ] , DummyMacroLoader ) ;
1336
+ let ecx = ExtCtxt :: new ( & ps, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs, & mut loader ) ;
1337
+ expand_crate ( ecx, vec ! [ ] , crate_ast) . 0
1335
1338
}
1336
1339
1337
1340
// find the pat_ident paths in a crate
0 commit comments