@@ -249,6 +249,8 @@ impl<'a> Resolver<'a> {
249
249
// n.b. we don't need to look at the path option here, because cstore already did
250
250
let crate_id = self . session . cstore . extern_mod_stmt_cnum ( item. id ) . unwrap ( ) ;
251
251
let module = self . get_extern_crate_root ( crate_id) ;
252
+ self . populate_module_if_necessary ( module) ;
253
+ let used = self . process_legacy_macro_imports ( item, module, expansion) ;
252
254
let binding =
253
255
( module, ty:: Visibility :: Public , sp, expansion) . to_name_binding ( self . arenas ) ;
254
256
let directive = self . arenas . alloc_import_directive ( ImportDirective {
@@ -260,11 +262,11 @@ impl<'a> Resolver<'a> {
260
262
module_path : Vec :: new ( ) ,
261
263
vis : Cell :: new ( vis) ,
262
264
expansion : expansion,
265
+ used : Cell :: new ( used) ,
263
266
} ) ;
267
+ self . potentially_unused_imports . push ( directive) ;
264
268
let imported_binding = self . import ( binding, directive) ;
265
269
self . define ( parent, ident, TypeNS , imported_binding) ;
266
- self . populate_module_if_necessary ( module) ;
267
- self . process_legacy_macro_imports ( item, module, expansion) ;
268
270
}
269
271
270
272
ItemKind :: Mod ( ..) if item. ident == keywords:: Invalid . ident ( ) => { } // Crate root
@@ -522,7 +524,6 @@ impl<'a> Resolver<'a> {
522
524
binding : & ' a NameBinding < ' a > ,
523
525
span : Span ,
524
526
allow_shadowing : bool ) {
525
- self . used_crates . insert ( binding. def ( ) . def_id ( ) . krate ) ;
526
527
self . macro_names . insert ( name) ;
527
528
if self . builtin_macros . insert ( name, binding) . is_some ( ) && !allow_shadowing {
528
529
let msg = format ! ( "`{}` is already in scope" , name) ;
@@ -532,22 +533,23 @@ impl<'a> Resolver<'a> {
532
533
}
533
534
}
534
535
535
- fn process_legacy_macro_imports ( & mut self , item : & Item , module : Module < ' a > , expansion : Mark ) {
536
+ // This returns true if we should consider the underlying `extern crate` to be used.
537
+ fn process_legacy_macro_imports ( & mut self , item : & Item , module : Module < ' a > , expansion : Mark )
538
+ -> bool {
536
539
let allow_shadowing = expansion == Mark :: root ( ) ;
537
540
let legacy_imports = self . legacy_macro_imports ( & item. attrs ) ;
538
- let cnum = module . def_id ( ) . unwrap ( ) . krate ;
541
+ let mut used = legacy_imports != LegacyMacroImports :: default ( ) ;
539
542
540
543
// `#[macro_use]` and `#[macro_reexport]` are only allowed at the crate root.
541
- if self . current_module . parent . is_some ( ) && legacy_imports != LegacyMacroImports :: default ( ) {
544
+ if self . current_module . parent . is_some ( ) && used {
542
545
span_err ! ( self . session, item. span, E0468 ,
543
546
"an `extern crate` loading macros must be at the crate root" ) ;
544
- } else if !self . use_extern_macros &&
545
- self . session . cstore . dep_kind ( cnum) . macros_only ( ) &&
546
- legacy_imports == LegacyMacroImports :: default ( ) {
547
+ } else if !self . use_extern_macros && !used &&
548
+ self . session . cstore . dep_kind ( module. def_id ( ) . unwrap ( ) . krate ) . macros_only ( ) {
547
549
let msg = "custom derive crates and `#[no_link]` crates have no effect without \
548
550
`#[macro_use]`";
549
551
self . session . span_warn ( item. span , msg) ;
550
- self . used_crates . insert ( cnum ) ; // Avoid the normal unused extern crate warning
552
+ used = true ; // Avoid the normal unused extern crate warning
551
553
}
552
554
553
555
if let Some ( span) = legacy_imports. import_all {
@@ -566,9 +568,7 @@ impl<'a> Resolver<'a> {
566
568
}
567
569
}
568
570
for ( name, span) in legacy_imports. reexports {
569
- let krate = module. def_id ( ) . unwrap ( ) . krate ;
570
- self . used_crates . insert ( krate) ;
571
- self . session . cstore . export_macros ( krate) ;
571
+ self . session . cstore . export_macros ( module. def_id ( ) . unwrap ( ) . krate ) ;
572
572
let ident = Ident :: with_empty_ctxt ( name) ;
573
573
let result = self . resolve_ident_in_module ( module, ident, MacroNS , false , None ) ;
574
574
if let Ok ( binding) = result {
@@ -577,6 +577,7 @@ impl<'a> Resolver<'a> {
577
577
span_err ! ( self . session, span, E0470 , "reexported macro not found" ) ;
578
578
}
579
579
}
580
+ used
580
581
}
581
582
582
583
// does this attribute list contain "macro_use"?
0 commit comments