@@ -541,23 +541,25 @@ fn cargo_to_crate_graph(
541541
542542 let mut pkg_to_lib_crate = FxHashMap :: default ( ) ;
543543
544- // Add test cfg for non-sysroot crates
545- cfg_options. insert_atom ( "test" . into ( ) ) ;
546544 cfg_options. insert_atom ( "debug_assertions" . into ( ) ) ;
547545
548546 let mut pkg_crates = FxHashMap :: default ( ) ;
549547 // Does any crate signal to rust-analyzer that they need the rustc_private crates?
550548 let mut has_private = false ;
551549 // Next, create crates for each package, target pair
552550 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 ( ) ;
555552
556553 let overrides = match override_cfg {
557554 CfgOverrides :: Wildcard ( cfg_diff) => Some ( cfg_diff) ,
558555 CfgOverrides :: Selective ( cfg_overrides) => cfg_overrides. get ( & cargo[ pkg] . name ) ,
559556 } ;
560557
558+ // Add test cfg for local crates
559+ if cargo[ pkg] . is_local {
560+ cfg_options. insert_atom ( "test" . into ( ) ) ;
561+ }
562+
561563 if let Some ( overrides) = overrides {
562564 // FIXME: this is sort of a hack to deal with #![cfg(not(test))] vanishing such as seen
563565 // in ed25519_dalek (#7243), and libcore (#9203) (although you only hit that one while
@@ -566,9 +568,7 @@ fn cargo_to_crate_graph(
566568 // A more ideal solution might be to reanalyze crates based on where the cursor is and
567569 // figure out the set of cfgs that would have to apply to make it active.
568570
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 ( ) ) ;
572572 } ;
573573
574574 has_private |= cargo[ pkg] . metadata . rustc_private ;
@@ -588,7 +588,7 @@ fn cargo_to_crate_graph(
588588 & mut crate_graph,
589589 & cargo[ pkg] ,
590590 build_scripts. get_output ( pkg) ,
591- cfg_options,
591+ cfg_options. clone ( ) ,
592592 & mut |path| load_proc_macro ( & cargo[ tgt] . name , path) ,
593593 file_id,
594594 & cargo[ tgt] . name ,
@@ -753,8 +753,7 @@ fn handle_rustc_crates(
753753 queue. push_back ( dep. pkg ) ;
754754 }
755755
756- let mut cfg_options = cfg_options;
757- let mut replaced_cfg_options;
756+ let mut cfg_options = cfg_options. clone ( ) ;
758757
759758 let overrides = match override_cfg {
760759 CfgOverrides :: Wildcard ( cfg_diff) => Some ( cfg_diff) ,
@@ -771,9 +770,7 @@ fn handle_rustc_crates(
771770 // A more ideal solution might be to reanalyze crates based on where the cursor is and
772771 // figure out the set of cfgs that would have to apply to make it active.
773772
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 ( ) ) ;
777774 } ;
778775
779776 for & tgt in rustc_workspace[ pkg] . targets . iter ( ) {
@@ -785,7 +782,7 @@ fn handle_rustc_crates(
785782 crate_graph,
786783 & rustc_workspace[ pkg] ,
787784 build_scripts. get_output ( pkg) ,
788- cfg_options,
785+ cfg_options. clone ( ) ,
789786 & mut |path| load_proc_macro ( & rustc_workspace[ tgt] . name , path) ,
790787 file_id,
791788 & rustc_workspace[ tgt] . name ,
@@ -840,15 +837,21 @@ fn add_target_crate_root(
840837 crate_graph : & mut CrateGraph ,
841838 pkg : & PackageData ,
842839 build_data : Option < & BuildScriptOutput > ,
843- cfg_options : & CfgOptions ,
840+ cfg_options : CfgOptions ,
844841 load_proc_macro : & mut dyn FnMut ( & AbsPath ) -> ProcMacroLoadResult ,
845842 file_id : FileId ,
846843 cargo_name : & str ,
847844 is_proc_macro : bool ,
848845) -> CrateId {
849846 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+ ) ;
850853 let cfg_options = {
851- let mut opts = cfg_options. clone ( ) ;
854+ let mut opts = cfg_options;
852855 for feature in pkg. active_features . iter ( ) {
853856 opts. insert_key_value ( "feature" . into ( ) , feature. into ( ) ) ;
854857 }
@@ -873,12 +876,6 @@ fn add_target_crate_root(
873876 } ;
874877
875878 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- ) ;
882879 crate_graph. add_crate_root (
883880 file_id,
884881 edition,
0 commit comments