@@ -21,8 +21,8 @@ use crate::{
21
21
cfg_flag:: CfgFlag ,
22
22
rustc_cfg,
23
23
sysroot:: SysrootCrate ,
24
- utf8_stdout, CargoConfig , CargoWorkspace , ManifestPath , ProjectJson , ProjectManifest , Sysroot ,
25
- TargetKind , WorkspaceBuildScripts ,
24
+ utf8_stdout, CargoConfig , CargoWorkspace , ManifestPath , Package , ProjectJson , ProjectManifest ,
25
+ Sysroot , TargetKind , WorkspaceBuildScripts ,
26
26
} ;
27
27
28
28
/// A set of cfg-overrides per crate.
@@ -315,6 +315,13 @@ impl ProjectWorkspace {
315
315
/// The return type contains the path and whether or not
316
316
/// the root is a member of the current workspace
317
317
pub fn to_roots ( & self ) -> Vec < PackageRoot > {
318
+ let mk_sysroot = |sysroot : Option < & Sysroot > | {
319
+ sysroot. map ( |sysroot| PackageRoot {
320
+ is_local : false ,
321
+ include : vec ! [ sysroot. src_root( ) . to_path_buf( ) ] ,
322
+ exclude : Vec :: new ( ) ,
323
+ } )
324
+ } ;
318
325
match self {
319
326
ProjectWorkspace :: Json { project, sysroot, rustc_cfg : _ } => project
320
327
. crates ( )
@@ -325,13 +332,7 @@ impl ProjectWorkspace {
325
332
} )
326
333
. collect :: < FxHashSet < _ > > ( )
327
334
. into_iter ( )
328
- . chain ( sysroot. as_ref ( ) . into_iter ( ) . flat_map ( |sysroot| {
329
- sysroot. crates ( ) . map ( move |krate| PackageRoot {
330
- is_local : false ,
331
- include : vec ! [ sysroot[ krate] . root. parent( ) . to_path_buf( ) ] ,
332
- exclude : Vec :: new ( ) ,
333
- } )
334
- } ) )
335
+ . chain ( mk_sysroot ( sysroot. as_ref ( ) ) )
335
336
. collect :: < Vec < _ > > ( ) ,
336
337
ProjectWorkspace :: Cargo {
337
338
cargo,
@@ -380,11 +381,7 @@ impl ProjectWorkspace {
380
381
}
381
382
PackageRoot { is_local, include, exclude }
382
383
} )
383
- . chain ( sysroot. iter ( ) . map ( |sysroot| PackageRoot {
384
- is_local : false ,
385
- include : vec ! [ sysroot. src_root( ) . to_path_buf( ) ] ,
386
- exclude : Vec :: new ( ) ,
387
- } ) )
384
+ . chain ( mk_sysroot ( sysroot. as_ref ( ) ) )
388
385
. chain ( rustc. iter ( ) . flat_map ( |rustc| {
389
386
rustc. packages ( ) . map ( move |krate| PackageRoot {
390
387
is_local : false ,
@@ -401,11 +398,7 @@ impl ProjectWorkspace {
401
398
include : vec ! [ detached_file. clone( ) ] ,
402
399
exclude : Vec :: new ( ) ,
403
400
} )
404
- . chain ( sysroot. crates ( ) . map ( |krate| PackageRoot {
405
- is_local : false ,
406
- include : vec ! [ sysroot[ krate] . root. parent( ) . to_path_buf( ) ] ,
407
- exclude : Vec :: new ( ) ,
408
- } ) )
401
+ . chain ( mk_sysroot ( Some ( sysroot) ) )
409
402
. collect ( ) ,
410
403
}
411
404
}
@@ -639,6 +632,8 @@ fn cargo_to_crate_graph(
639
632
lib_tgt = Some ( ( crate_id, cargo[ tgt] . name . clone ( ) ) ) ;
640
633
pkg_to_lib_crate. insert ( pkg, crate_id) ;
641
634
}
635
+ // Even crates that don't set proc-macro = true are allowed to depend on proc_macro
636
+ // (just none of the APIs work when called outside of a proc macro).
642
637
if let Some ( proc_macro) = libproc_macro {
643
638
add_dep_with_prelude (
644
639
& mut crate_graph,
@@ -654,19 +649,19 @@ fn cargo_to_crate_graph(
654
649
}
655
650
656
651
// Set deps to the core, std and to the lib target of the current package
657
- for ( from, kind) in pkg_crates. get ( & pkg) . into_iter ( ) . flatten ( ) {
652
+ for & ( from, kind) in pkg_crates. get ( & pkg) . into_iter ( ) . flatten ( ) {
658
653
// Add sysroot deps first so that a lib target named `core` etc. can overwrite them.
659
- public_deps. add ( * from, & mut crate_graph) ;
654
+ public_deps. add ( from, & mut crate_graph) ;
660
655
661
656
if let Some ( ( to, name) ) = lib_tgt. clone ( ) {
662
- if to != * from && * kind != TargetKind :: BuildScript {
657
+ if to != from && kind != TargetKind :: BuildScript {
663
658
// (build script can not depend on its library target)
664
659
665
660
// For root projects with dashes in their name,
666
661
// cargo metadata does not do any normalization,
667
662
// so we do it ourselves currently
668
663
let name = CrateName :: normalize_dashes ( & name) ;
669
- add_dep ( & mut crate_graph, * from, name, to) ;
664
+ add_dep ( & mut crate_graph, from, name, to) ;
670
665
}
671
666
}
672
667
}
@@ -678,17 +673,17 @@ fn cargo_to_crate_graph(
678
673
for dep in cargo[ pkg] . dependencies . iter ( ) {
679
674
let name = CrateName :: new ( & dep. name ) . unwrap ( ) ;
680
675
if let Some ( & to) = pkg_to_lib_crate. get ( & dep. pkg ) {
681
- for ( from, kind) in pkg_crates. get ( & pkg) . into_iter ( ) . flatten ( ) {
682
- if dep. kind == DepKind :: Build && * kind != TargetKind :: BuildScript {
676
+ for & ( from, kind) in pkg_crates. get ( & pkg) . into_iter ( ) . flatten ( ) {
677
+ if dep. kind == DepKind :: Build && kind != TargetKind :: BuildScript {
683
678
// Only build scripts may depend on build dependencies.
684
679
continue ;
685
680
}
686
- if dep. kind != DepKind :: Build && * kind == TargetKind :: BuildScript {
681
+ if dep. kind != DepKind :: Build && kind == TargetKind :: BuildScript {
687
682
// Build scripts may only depend on build dependencies.
688
683
continue ;
689
684
}
690
685
691
- add_dep ( & mut crate_graph, * from, name. clone ( ) , to)
686
+ add_dep ( & mut crate_graph, from, name. clone ( ) , to)
692
687
}
693
688
}
694
689
}
@@ -699,9 +694,9 @@ fn cargo_to_crate_graph(
699
694
// and create dependencies on them for the crates which opt-in to that
700
695
if let Some ( rustc_workspace) = rustc {
701
696
handle_rustc_crates (
697
+ & mut crate_graph,
702
698
rustc_workspace,
703
699
load,
704
- & mut crate_graph,
705
700
& cfg_options,
706
701
override_cfg,
707
702
load_proc_macro,
@@ -761,16 +756,16 @@ fn detached_files_to_crate_graph(
761
756
}
762
757
763
758
fn handle_rustc_crates (
759
+ crate_graph : & mut CrateGraph ,
764
760
rustc_workspace : & CargoWorkspace ,
765
761
load : & mut dyn FnMut ( & AbsPath ) -> Option < FileId > ,
766
- crate_graph : & mut CrateGraph ,
767
762
cfg_options : & CfgOptions ,
768
763
override_cfg : & CfgOverrides ,
769
764
load_proc_macro : & mut dyn FnMut ( & str , & AbsPath ) -> ProcMacroLoadResult ,
770
- pkg_to_lib_crate : & mut FxHashMap < la_arena :: Idx < crate :: PackageData > , CrateId > ,
765
+ pkg_to_lib_crate : & mut FxHashMap < Package , CrateId > ,
771
766
public_deps : & SysrootPublicDeps ,
772
767
cargo : & CargoWorkspace ,
773
- pkg_crates : & FxHashMap < la_arena :: Idx < crate :: PackageData > , Vec < ( CrateId , TargetKind ) > > ,
768
+ pkg_crates : & FxHashMap < Package , Vec < ( CrateId , TargetKind ) > > ,
774
769
build_scripts : & WorkspaceBuildScripts ,
775
770
) {
776
771
let mut rustc_pkg_crates = FxHashMap :: default ( ) ;
@@ -784,8 +779,8 @@ fn handle_rustc_crates(
784
779
let mut queue = VecDeque :: new ( ) ;
785
780
queue. push_back ( root_pkg) ;
786
781
while let Some ( pkg) = queue. pop_front ( ) {
787
- // Don't duplicate packages if they are dependended on a diamond pattern
788
- // N.B. if this line is omitted, we try to analyse over 4_800_000 crates
782
+ // Don't duplicate packages if they are dependent on a diamond pattern
783
+ // N.B. if this line is omitted, we try to analyze over 4_800_000 crates
789
784
// which is not ideal
790
785
if rustc_pkg_crates. contains_key ( & pkg) {
791
786
continue ;
0 commit comments