@@ -726,13 +726,11 @@ fn expand_annotatable(a: Annotatable,
726726 let new_items: SmallVector < Annotatable > = match a {
727727 Annotatable :: Item ( it) => match it. node {
728728 ast:: ItemKind :: Mac ( ..) => {
729- let new_items : SmallVector < P < ast :: Item > > = it. and_then ( |it| match it. node {
729+ it. and_then ( |it| match it. node {
730730 ItemKind :: Mac ( mac) =>
731731 expand_mac_invoc ( mac, Some ( it. ident ) , it. attrs , it. span , fld) ,
732732 _ => unreachable ! ( ) ,
733- } ) ;
734-
735- new_items. into_iter ( ) . map ( |i| Annotatable :: Item ( i) ) . collect ( )
733+ } )
736734 }
737735 ast:: ItemKind :: Mod ( _) | ast:: ItemKind :: ForeignMod ( _) => {
738736 let valid_ident =
@@ -748,10 +746,19 @@ fn expand_annotatable(a: Annotatable,
748746 if valid_ident {
749747 fld. cx . mod_pop ( ) ;
750748 }
751- result. into_iter ( ) . map ( |i| Annotatable :: Item ( i ) ) . collect ( )
749+ result
752750 } ,
753- _ => noop_fold_item ( it, fld) . into_iter ( ) . map ( |i| Annotatable :: Item ( i) ) . collect ( ) ,
754- } ,
751+ ast:: ItemKind :: ExternCrate ( _) => {
752+ // We need to error on `#[macro_use] extern crate` when it isn't at the
753+ // crate root, because `$crate` won't work properly.
754+ let allows_macros = fld. cx . syntax_env . is_crate_root ( ) ;
755+ for def in fld. cx . loader . load_crate ( & it, allows_macros) {
756+ fld. cx . insert_macro ( def) ;
757+ }
758+ SmallVector :: one ( it)
759+ } ,
760+ _ => noop_fold_item ( it, fld) ,
761+ } . into_iter ( ) . map ( |i| Annotatable :: Item ( i) ) . collect ( ) ,
755762
756763 Annotatable :: TraitItem ( it) => match it. node {
757764 ast:: TraitItemKind :: Method ( _, Some ( _) ) => {
@@ -1137,8 +1144,6 @@ impl<'feat> ExpansionConfig<'feat> {
11371144}
11381145
11391146pub fn expand_crate ( mut cx : ExtCtxt ,
1140- // these are the macros being imported to this crate:
1141- imported_macros : Vec < ast:: MacroDef > ,
11421147 user_exts : Vec < NamedSyntaxExtension > ,
11431148 c : Crate ) -> ( Crate , HashSet < Name > ) {
11441149 if std_inject:: no_core ( & c) {
@@ -1151,10 +1156,6 @@ pub fn expand_crate(mut cx: ExtCtxt,
11511156 let ret = {
11521157 let mut expander = MacroExpander :: new ( & mut cx) ;
11531158
1154- for def in imported_macros {
1155- expander. cx . insert_macro ( def) ;
1156- }
1157-
11581159 for ( name, extension) in user_exts {
11591160 expander. cx . syntax_env . insert ( name, extension) ;
11601161 }
@@ -1220,7 +1221,7 @@ mod tests {
12201221 use ast;
12211222 use ast:: Name ;
12221223 use codemap;
1223- use ext:: base:: ExtCtxt ;
1224+ use ext:: base:: { ExtCtxt , DummyMacroLoader } ;
12241225 use ext:: mtwt;
12251226 use fold:: Folder ;
12261227 use parse;
@@ -1291,9 +1292,9 @@ mod tests {
12911292 src,
12921293 Vec :: new ( ) , & sess) . unwrap ( ) ;
12931294 // 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) ;
1295+ let ( mut gated_cfgs, mut loader ) = ( vec ! [ ] , DummyMacroLoader ) ;
1296+ let ecx = ExtCtxt :: new ( & sess, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs, & mut loader ) ;
1297+ expand_crate ( ecx, vec ! [ ] , crate_ast) ;
12971298 }
12981299
12991300 // make sure that macros can't escape modules
@@ -1306,9 +1307,9 @@ mod tests {
13061307 "<test>" . to_string ( ) ,
13071308 src,
13081309 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) ;
1310+ let ( mut gated_cfgs, mut loader ) = ( vec ! [ ] , DummyMacroLoader ) ;
1311+ let ecx = ExtCtxt :: new ( & sess, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs, & mut loader ) ;
1312+ expand_crate ( ecx, vec ! [ ] , crate_ast) ;
13121313 }
13131314
13141315 // macro_use modules should allow macros to escape
@@ -1320,18 +1321,18 @@ mod tests {
13201321 "<test>" . to_string ( ) ,
13211322 src,
13221323 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) ;
1324+ let ( mut gated_cfgs, mut loader ) = ( vec ! [ ] , DummyMacroLoader ) ;
1325+ let ecx = ExtCtxt :: new ( & sess, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs, & mut loader ) ;
1326+ expand_crate ( ecx, vec ! [ ] , crate_ast) ;
13261327 }
13271328
13281329 fn expand_crate_str ( crate_str : String ) -> ast:: Crate {
13291330 let ps = parse:: ParseSess :: new ( ) ;
13301331 let crate_ast = panictry ! ( string_to_parser( & ps, crate_str) . parse_crate_mod( ) ) ;
13311332 // 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
1333+ let ( mut gated_cfgs, mut loader ) = ( vec ! [ ] , DummyMacroLoader ) ;
1334+ let ecx = ExtCtxt :: new ( & ps, vec ! [ ] , test_ecfg ( ) , & mut gated_cfgs, & mut loader ) ;
1335+ expand_crate ( ecx, vec ! [ ] , crate_ast) . 0
13351336 }
13361337
13371338 // find the pat_ident paths in a crate
0 commit comments