@@ -470,9 +470,7 @@ fn project_json_to_crate_graph(
470
470
for ( from, krate) in project. crates ( ) {
471
471
if let Some ( & from) = crates. get ( & from) {
472
472
if let Some ( ( public_deps, libproc_macro) ) = & sysroot_deps {
473
- for ( name, to) in public_deps. iter ( ) {
474
- add_dep ( & mut crate_graph, from, name. clone ( ) , * to)
475
- }
473
+ public_deps. add ( from, & mut crate_graph) ;
476
474
if krate. is_proc_macro {
477
475
if let Some ( proc_macro) = libproc_macro {
478
476
add_dep (
@@ -509,7 +507,7 @@ fn cargo_to_crate_graph(
509
507
let mut crate_graph = CrateGraph :: default ( ) ;
510
508
let ( public_deps, libproc_macro) = match sysroot {
511
509
Some ( sysroot) => sysroot_to_crate_graph ( & mut crate_graph, sysroot, rustc_cfg. clone ( ) , load) ,
512
- None => ( Vec :: new ( ) , None ) ,
510
+ None => ( SysrootPublicDeps :: default ( ) , None ) ,
513
511
} ;
514
512
515
513
let mut cfg_options = CfgOptions :: default ( ) ;
@@ -590,9 +588,7 @@ fn cargo_to_crate_graph(
590
588
add_dep ( & mut crate_graph, * from, name, to) ;
591
589
}
592
590
}
593
- for ( name, krate) in public_deps. iter ( ) {
594
- add_dep ( & mut crate_graph, * from, name. clone ( ) , * krate) ;
595
- }
591
+ public_deps. add ( * from, & mut crate_graph) ;
596
592
}
597
593
}
598
594
@@ -674,9 +670,7 @@ fn detached_files_to_crate_graph(
674
670
Vec :: new ( ) ,
675
671
) ;
676
672
677
- for ( name, krate) in public_deps. iter ( ) {
678
- add_dep ( & mut crate_graph, detached_file_crate, name. clone ( ) , * krate) ;
679
- }
673
+ public_deps. add ( detached_file_crate, & mut crate_graph) ;
680
674
}
681
675
crate_graph
682
676
}
@@ -688,7 +682,7 @@ fn handle_rustc_crates(
688
682
cfg_options : & CfgOptions ,
689
683
load_proc_macro : & mut dyn FnMut ( & AbsPath ) -> Vec < ProcMacro > ,
690
684
pkg_to_lib_crate : & mut FxHashMap < la_arena:: Idx < crate :: PackageData > , CrateId > ,
691
- public_deps : & [ ( CrateName , CrateId ) ] ,
685
+ public_deps : & SysrootPublicDeps ,
692
686
cargo : & CargoWorkspace ,
693
687
pkg_crates : & FxHashMap < la_arena:: Idx < crate :: PackageData > , Vec < ( CrateId , TargetKind ) > > ,
694
688
) {
@@ -728,9 +722,7 @@ fn handle_rustc_crates(
728
722
) ;
729
723
pkg_to_lib_crate. insert ( pkg, crate_id) ;
730
724
// Add dependencies on core / std / alloc for this crate
731
- for ( name, krate) in public_deps. iter ( ) {
732
- add_dep ( crate_graph, crate_id, name. clone ( ) , * krate) ;
733
- }
725
+ public_deps. add ( crate_id, crate_graph) ;
734
726
rustc_pkg_crates. entry ( pkg) . or_insert_with ( Vec :: new) . push ( crate_id) ;
735
727
}
736
728
}
@@ -828,12 +820,26 @@ fn add_target_crate_root(
828
820
)
829
821
}
830
822
823
+ #[ derive( Default ) ]
824
+ struct SysrootPublicDeps {
825
+ deps : Vec < ( CrateName , CrateId , bool ) > ,
826
+ }
827
+
828
+ impl SysrootPublicDeps {
829
+ /// Makes `from` depend on the public sysroot crates.
830
+ fn add ( & self , from : CrateId , crate_graph : & mut CrateGraph ) {
831
+ for ( name, krate, prelude) in & self . deps {
832
+ add_dep_with_prelude ( crate_graph, from, name. clone ( ) , * krate, * prelude) ;
833
+ }
834
+ }
835
+ }
836
+
831
837
fn sysroot_to_crate_graph (
832
838
crate_graph : & mut CrateGraph ,
833
839
sysroot : & Sysroot ,
834
840
rustc_cfg : Vec < CfgFlag > ,
835
841
load : & mut dyn FnMut ( & AbsPath ) -> Option < FileId > ,
836
- ) -> ( Vec < ( CrateName , CrateId ) > , Option < CrateId > ) {
842
+ ) -> ( SysrootPublicDeps , Option < CrateId > ) {
837
843
let _p = profile:: span ( "sysroot_to_crate_graph" ) ;
838
844
let mut cfg_options = CfgOptions :: default ( ) ;
839
845
cfg_options. extend ( rustc_cfg) ;
@@ -867,17 +873,35 @@ fn sysroot_to_crate_graph(
867
873
}
868
874
}
869
875
870
- let public_deps = sysroot
871
- . public_deps ( )
872
- . map ( |( name, idx) | ( CrateName :: new ( name) . unwrap ( ) , sysroot_crates[ & idx] ) )
873
- . collect :: < Vec < _ > > ( ) ;
876
+ let public_deps = SysrootPublicDeps {
877
+ deps : sysroot
878
+ . public_deps ( )
879
+ . map ( |( name, idx, prelude) | {
880
+ ( CrateName :: new ( name) . unwrap ( ) , sysroot_crates[ & idx] , prelude)
881
+ } )
882
+ . collect :: < Vec < _ > > ( ) ,
883
+ } ;
874
884
875
885
let libproc_macro = sysroot. proc_macro ( ) . and_then ( |it| sysroot_crates. get ( & it) . copied ( ) ) ;
876
886
( public_deps, libproc_macro)
877
887
}
878
888
879
889
fn add_dep ( graph : & mut CrateGraph , from : CrateId , name : CrateName , to : CrateId ) {
880
- if let Err ( err) = graph. add_dep ( from, Dependency :: new ( name, to) ) {
890
+ add_dep_inner ( graph, from, Dependency :: new ( name, to) )
891
+ }
892
+
893
+ fn add_dep_with_prelude (
894
+ graph : & mut CrateGraph ,
895
+ from : CrateId ,
896
+ name : CrateName ,
897
+ to : CrateId ,
898
+ prelude : bool ,
899
+ ) {
900
+ add_dep_inner ( graph, from, Dependency :: with_prelude ( name, to, prelude) )
901
+ }
902
+
903
+ fn add_dep_inner ( graph : & mut CrateGraph , from : CrateId , dep : Dependency ) {
904
+ if let Err ( err) = graph. add_dep ( from, dep) {
881
905
tracing:: error!( "{}" , err)
882
906
}
883
907
}
0 commit comments