@@ -436,16 +436,23 @@ impl<'a> CrateLoader<'a> {
436
436
fn resolve_crate < ' b > (
437
437
& ' b mut self ,
438
438
name : Symbol ,
439
+ orig_name : Option < Symbol > ,
440
+ transitive : bool ,
439
441
span : Span ,
440
442
dep_kind : DepKind ,
441
443
dep : Option < ( & ' b CratePaths , & ' b CrateDep ) > ,
442
444
) -> CrateNum {
443
- self . maybe_resolve_crate ( name, span, dep_kind, dep) . unwrap_or_else ( |err| err. report ( ) )
445
+ self . maybe_resolve_crate ( name, orig_name, transitive, span, dep_kind, dep)
446
+ . unwrap_or_else ( |err| err. report ( ) )
444
447
}
445
448
446
449
fn maybe_resolve_crate < ' b > (
447
450
& ' b mut self ,
448
451
name : Symbol ,
452
+ orig_name : Option < Symbol > ,
453
+ // Whether this is a direct dependency of the current compilation session,
454
+ // or a transitive dependency
455
+ transitive : bool ,
449
456
span : Span ,
450
457
mut dep_kind : DepKind ,
451
458
dep : Option < ( & ' b CratePaths , & ' b CrateDep ) > ,
@@ -479,15 +486,33 @@ impl<'a> CrateLoader<'a> {
479
486
Some ( false ) , // is_proc_macro
480
487
) ;
481
488
482
- self . load ( & mut locator)
489
+ let res = self
490
+ . load ( & mut locator)
483
491
. map ( |r| ( r, None ) )
484
492
. or_else ( || {
485
493
dep_kind = DepKind :: MacrosOnly ;
486
494
self . load_proc_macro ( & mut locator, path_kind)
487
495
} )
488
- . ok_or_else ( move || LoadError :: LocatorError ( locator) ) ?
496
+ . ok_or_else ( move || LoadError :: LocatorError ( locator) ) ?;
497
+
498
+ res
489
499
} ;
490
500
501
+ // Loading a transitive dependency doesn't mark that dependency as used
502
+ // Note that we need to perform this check even when the crate was already loaded
503
+ // (e.g. `existing_match` finds the crate), since the crate may have been
504
+ // initially loaded as a transitive dependency
505
+ if !transitive {
506
+ self . loaded_crates . as_mut ( ) . unwrap ( ) . insert ( name) ;
507
+ // If this crate was renamed via `extern crate foo as bar`,
508
+ // mark both names as loaded. An `extern crate` always forces
509
+ // the crate to be loaded, so we don't want the UNUSED_EXTERN_OPTION lint
510
+ // will never fire (though the UNUSED_EXTERN_CRATES lint might still fire)
511
+ if let Some ( orig_name) = orig_name {
512
+ self . loaded_crates . as_mut ( ) . unwrap ( ) . insert ( orig_name) ;
513
+ }
514
+ }
515
+
491
516
match result {
492
517
( LoadResult :: Previous ( cnum) , None ) => {
493
518
let data = self . cstore . get_crate_data ( cnum) ;
@@ -498,7 +523,6 @@ impl<'a> CrateLoader<'a> {
498
523
Ok ( cnum)
499
524
}
500
525
( LoadResult :: Loaded ( library) , host_library) => {
501
- self . loaded_crates . as_mut ( ) . unwrap ( ) . insert ( name) ;
502
526
Ok ( self . register_crate ( host_library, root, span, library, dep_kind, name) )
503
527
}
504
528
_ => panic ! ( ) ,
@@ -571,7 +595,7 @@ impl<'a> CrateLoader<'a> {
571
595
DepKind :: MacrosOnly => DepKind :: MacrosOnly ,
572
596
_ => dep. kind ,
573
597
} ;
574
- self . resolve_crate ( dep. name , span, dep_kind, Some ( ( root, & dep) ) )
598
+ self . resolve_crate ( dep. name , None , true , span, dep_kind, Some ( ( root, & dep) ) )
575
599
} ) )
576
600
. collect ( )
577
601
}
@@ -666,7 +690,7 @@ impl<'a> CrateLoader<'a> {
666
690
} ;
667
691
info ! ( "panic runtime not found -- loading {}" , name) ;
668
692
669
- let cnum = self . resolve_crate ( name, DUMMY_SP , DepKind :: Implicit , None ) ;
693
+ let cnum = self . resolve_crate ( name, None , false , DUMMY_SP , DepKind :: Implicit , None ) ;
670
694
let data = self . cstore . get_crate_data ( cnum) ;
671
695
672
696
// Sanity check the loaded crate to ensure it is indeed a panic runtime
@@ -692,7 +716,7 @@ impl<'a> CrateLoader<'a> {
692
716
info ! ( "loading profiler" ) ;
693
717
694
718
let name = Symbol :: intern ( "profiler_builtins" ) ;
695
- let cnum = self . resolve_crate ( name, DUMMY_SP , DepKind :: Implicit , None ) ;
719
+ let cnum = self . resolve_crate ( name, None , false , DUMMY_SP , DepKind :: Implicit , None ) ;
696
720
let data = self . cstore . get_crate_data ( cnum) ;
697
721
698
722
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
@@ -834,15 +858,18 @@ impl<'a> CrateLoader<'a> {
834
858
} ) ;
835
859
}
836
860
837
- pub fn postprocess ( & mut self , krate : & ast:: Crate ) -> FxHashSet < Symbol > {
861
+ pub fn take_loaded_crates ( & mut self ) -> FxHashSet < Symbol > {
862
+ self . loaded_crates . take ( ) . unwrap ( )
863
+ }
864
+
865
+ pub fn postprocess ( & mut self , krate : & ast:: Crate ) {
838
866
self . inject_profiler_runtime ( ) ;
839
867
self . inject_allocator_crate ( krate) ;
840
868
self . inject_panic_runtime ( krate) ;
841
869
842
870
if log_enabled ! ( log:: Level :: Info ) {
843
871
dump_crates ( & self . cstore ) ;
844
872
}
845
- self . loaded_crates . take ( ) . unwrap ( )
846
873
}
847
874
848
875
pub fn process_extern_crate (
@@ -873,7 +900,7 @@ impl<'a> CrateLoader<'a> {
873
900
DepKind :: Explicit
874
901
} ;
875
902
876
- let cnum = self . resolve_crate ( name, item. span , dep_kind, None ) ;
903
+ let cnum = self . resolve_crate ( name, orig_name , false , item. span , dep_kind, None ) ;
877
904
878
905
let def_id = definitions. opt_local_def_id ( item. id ) . unwrap ( ) ;
879
906
let path_len = definitions. def_path ( def_id. index ) . data . len ( ) ;
@@ -893,7 +920,7 @@ impl<'a> CrateLoader<'a> {
893
920
}
894
921
895
922
pub fn process_path_extern ( & mut self , name : Symbol , span : Span ) -> CrateNum {
896
- let cnum = self . resolve_crate ( name, span, DepKind :: Explicit , None ) ;
923
+ let cnum = self . resolve_crate ( name, None , false , span, DepKind :: Explicit , None ) ;
897
924
898
925
self . update_extern_crate (
899
926
cnum,
@@ -910,6 +937,6 @@ impl<'a> CrateLoader<'a> {
910
937
}
911
938
912
939
pub fn maybe_process_path_extern ( & mut self , name : Symbol , span : Span ) -> Option < CrateNum > {
913
- self . maybe_resolve_crate ( name, span, DepKind :: Explicit , None ) . ok ( )
940
+ self . maybe_resolve_crate ( name, None , false , span, DepKind :: Explicit , None ) . ok ( )
914
941
}
915
942
}
0 commit comments