@@ -541,23 +541,25 @@ fn cargo_to_crate_graph(
541
541
542
542
let mut pkg_to_lib_crate = FxHashMap :: default ( ) ;
543
543
544
- // Add test cfg for non-sysroot crates
545
- cfg_options. insert_atom ( "test" . into ( ) ) ;
546
544
cfg_options. insert_atom ( "debug_assertions" . into ( ) ) ;
547
545
548
546
let mut pkg_crates = FxHashMap :: default ( ) ;
549
547
// Does any crate signal to rust-analyzer that they need the rustc_private crates?
550
548
let mut has_private = false ;
551
549
// Next, create crates for each package, target pair
552
550
for pkg in cargo. packages ( ) {
553
- let mut cfg_options = & cfg_options;
554
- let mut replaced_cfg_options;
551
+ let mut cfg_options = cfg_options. clone ( ) ;
555
552
556
553
let overrides = match override_cfg {
557
554
CfgOverrides :: Wildcard ( cfg_diff) => Some ( cfg_diff) ,
558
555
CfgOverrides :: Selective ( cfg_overrides) => cfg_overrides. get ( & cargo[ pkg] . name ) ,
559
556
} ;
560
557
558
+ // Add test cfg for local crates
559
+ if cargo[ pkg] . is_local {
560
+ cfg_options. insert_atom ( "test" . into ( ) ) ;
561
+ }
562
+
561
563
if let Some ( overrides) = overrides {
562
564
// FIXME: this is sort of a hack to deal with #![cfg(not(test))] vanishing such as seen
563
565
// in ed25519_dalek (#7243), and libcore (#9203) (although you only hit that one while
@@ -566,9 +568,7 @@ fn cargo_to_crate_graph(
566
568
// A more ideal solution might be to reanalyze crates based on where the cursor is and
567
569
// figure out the set of cfgs that would have to apply to make it active.
568
570
569
- replaced_cfg_options = cfg_options. clone ( ) ;
570
- replaced_cfg_options. apply_diff ( overrides. clone ( ) ) ;
571
- cfg_options = & replaced_cfg_options;
571
+ cfg_options. apply_diff ( overrides. clone ( ) ) ;
572
572
} ;
573
573
574
574
has_private |= cargo[ pkg] . metadata . rustc_private ;
@@ -588,7 +588,7 @@ fn cargo_to_crate_graph(
588
588
& mut crate_graph,
589
589
& cargo[ pkg] ,
590
590
build_scripts. get_output ( pkg) ,
591
- cfg_options,
591
+ cfg_options. clone ( ) ,
592
592
& mut |path| load_proc_macro ( & cargo[ tgt] . name , path) ,
593
593
file_id,
594
594
& cargo[ tgt] . name ,
@@ -753,8 +753,7 @@ fn handle_rustc_crates(
753
753
queue. push_back ( dep. pkg ) ;
754
754
}
755
755
756
- let mut cfg_options = cfg_options;
757
- let mut replaced_cfg_options;
756
+ let mut cfg_options = cfg_options. clone ( ) ;
758
757
759
758
let overrides = match override_cfg {
760
759
CfgOverrides :: Wildcard ( cfg_diff) => Some ( cfg_diff) ,
@@ -771,9 +770,7 @@ fn handle_rustc_crates(
771
770
// A more ideal solution might be to reanalyze crates based on where the cursor is and
772
771
// figure out the set of cfgs that would have to apply to make it active.
773
772
774
- replaced_cfg_options = cfg_options. clone ( ) ;
775
- replaced_cfg_options. apply_diff ( overrides. clone ( ) ) ;
776
- cfg_options = & replaced_cfg_options;
773
+ cfg_options. apply_diff ( overrides. clone ( ) ) ;
777
774
} ;
778
775
779
776
for & tgt in rustc_workspace[ pkg] . targets . iter ( ) {
@@ -785,7 +782,7 @@ fn handle_rustc_crates(
785
782
crate_graph,
786
783
& rustc_workspace[ pkg] ,
787
784
build_scripts. get_output ( pkg) ,
788
- cfg_options,
785
+ cfg_options. clone ( ) ,
789
786
& mut |path| load_proc_macro ( & rustc_workspace[ tgt] . name , path) ,
790
787
file_id,
791
788
& rustc_workspace[ tgt] . name ,
@@ -840,15 +837,21 @@ fn add_target_crate_root(
840
837
crate_graph : & mut CrateGraph ,
841
838
pkg : & PackageData ,
842
839
build_data : Option < & BuildScriptOutput > ,
843
- cfg_options : & CfgOptions ,
840
+ cfg_options : CfgOptions ,
844
841
load_proc_macro : & mut dyn FnMut ( & AbsPath ) -> ProcMacroLoadResult ,
845
842
file_id : FileId ,
846
843
cargo_name : & str ,
847
844
is_proc_macro : bool ,
848
845
) -> CrateId {
849
846
let edition = pkg. edition ;
847
+ let mut potential_cfg_options = cfg_options. clone ( ) ;
848
+ potential_cfg_options. extend (
849
+ pkg. features
850
+ . iter ( )
851
+ . map ( |feat| CfgFlag :: KeyValue { key : "feature" . into ( ) , value : feat. 0 . into ( ) } ) ,
852
+ ) ;
850
853
let cfg_options = {
851
- let mut opts = cfg_options. clone ( ) ;
854
+ let mut opts = cfg_options;
852
855
for feature in pkg. active_features . iter ( ) {
853
856
opts. insert_key_value ( "feature" . into ( ) , feature. into ( ) ) ;
854
857
}
@@ -873,12 +876,6 @@ fn add_target_crate_root(
873
876
} ;
874
877
875
878
let display_name = CrateDisplayName :: from_canonical_name ( cargo_name. to_string ( ) ) ;
876
- let mut potential_cfg_options = cfg_options. clone ( ) ;
877
- potential_cfg_options. extend (
878
- pkg. features
879
- . iter ( )
880
- . map ( |feat| CfgFlag :: KeyValue { key : "feature" . into ( ) , value : feat. 0 . into ( ) } ) ,
881
- ) ;
882
879
crate_graph. add_crate_root (
883
880
file_id,
884
881
edition,
0 commit comments