@@ -750,6 +750,15 @@ fn expand_annotatable(a: Annotatable,
750750 }
751751 result. into_iter ( ) . map ( |i| Annotatable :: Item ( i) ) . collect ( )
752752 } ,
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+ } ,
753762 _ => noop_fold_item ( it, fld) . into_iter ( ) . map ( |i| Annotatable :: Item ( i) ) . collect ( ) ,
754763 } ,
755764
@@ -1137,8 +1146,6 @@ impl<'feat> ExpansionConfig<'feat> {
11371146}
11381147
11391148pub fn expand_crate ( mut cx : ExtCtxt ,
1140- // these are the macros being imported to this crate:
1141- imported_macros : Vec < ast:: MacroDef > ,
11421149 user_exts : Vec < NamedSyntaxExtension > ,
11431150 c : Crate ) -> ( Crate , HashSet < Name > ) {
11441151 if std_inject:: no_core ( & c) {
@@ -1151,10 +1158,6 @@ pub fn expand_crate(mut cx: ExtCtxt,
11511158 let ret = {
11521159 let mut expander = MacroExpander :: new ( & mut cx) ;
11531160
1154- for def in imported_macros {
1155- expander. cx . insert_macro ( def) ;
1156- }
1157-
11581161 for ( name, extension) in user_exts {
11591162 expander. cx . syntax_env . insert ( name, extension) ;
11601163 }
@@ -1220,7 +1223,7 @@ mod tests {
12201223 use ast;
12211224 use ast:: Name ;
12221225 use codemap;
1223- use ext:: base:: ExtCtxt ;
1226+ use ext:: base:: { ExtCtxt , DummyMacroLoader } ;
12241227 use ext:: mtwt;
12251228 use fold:: Folder ;
12261229 use parse;
@@ -1291,9 +1294,9 @@ mod tests {
12911294 src,
12921295 Vec :: new ( ) , & sess) . unwrap ( ) ;
12931296 // 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) ;
12971300 }
12981301
12991302 // make sure that macros can't escape modules
@@ -1306,9 +1309,9 @@ mod tests {
13061309 "<test>" . to_string ( ) ,
13071310 src,
13081311 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) ;
13121315 }
13131316
13141317 // macro_use modules should allow macros to escape
@@ -1320,18 +1323,18 @@ mod tests {
13201323 "<test>" . to_string ( ) ,
13211324 src,
13221325 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) ;
13261329 }
13271330
13281331 fn expand_crate_str ( crate_str : String ) -> ast:: Crate {
13291332 let ps = parse:: ParseSess :: new ( ) ;
13301333 let crate_ast = panictry ! ( string_to_parser( & ps, crate_str) . parse_crate_mod( ) ) ;
13311334 // 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
13351338 }
13361339
13371340 // find the pat_ident paths in a crate
0 commit comments