@@ -86,6 +86,8 @@ pub enum ProjectWorkspace {
8686 toolchain : Option < Version > ,
8787 /// The target data layout queried for workspace.
8888 target_layout : TargetLayoutLoadResult ,
89+ /// A set of cfg overrides for this workspace.
90+ cfg_overrides : CfgOverrides ,
8991 } ,
9092 // FIXME: The primary limitation of this approach is that the set of detached files needs to be fixed at the beginning.
9193 // That's not the end user experience we should strive for.
@@ -111,6 +113,8 @@ pub enum ProjectWorkspace {
111113 toolchain : Option < Version > ,
112114 /// The target data layout queried for workspace.
113115 target_layout : TargetLayoutLoadResult ,
116+ /// A set of cfg overrides for the files.
117+ cfg_overrides : CfgOverrides ,
114118 } ,
115119}
116120
@@ -149,6 +153,7 @@ impl fmt::Debug for ProjectWorkspace {
149153 rustc_cfg,
150154 toolchain,
151155 target_layout : data_layout,
156+ cfg_overrides,
152157 } => {
153158 let mut debug_struct = f. debug_struct ( "Json" ) ;
154159 debug_struct. field ( "n_crates" , & project. n_crates ( ) ) ;
@@ -158,7 +163,8 @@ impl fmt::Debug for ProjectWorkspace {
158163 debug_struct
159164 . field ( "n_rustc_cfg" , & rustc_cfg. len ( ) )
160165 . field ( "toolchain" , & toolchain)
161- . field ( "data_layout" , & data_layout) ;
166+ . field ( "data_layout" , & data_layout)
167+ . field ( "n_cfg_overrides" , & cfg_overrides. len ( ) ) ;
162168 debug_struct. finish ( )
163169 }
164170 ProjectWorkspace :: DetachedFiles {
@@ -167,13 +173,15 @@ impl fmt::Debug for ProjectWorkspace {
167173 rustc_cfg,
168174 toolchain,
169175 target_layout,
176+ cfg_overrides,
170177 } => f
171178 . debug_struct ( "DetachedFiles" )
172179 . field ( "n_files" , & files. len ( ) )
173180 . field ( "sysroot" , & sysroot. is_ok ( ) )
174181 . field ( "n_rustc_cfg" , & rustc_cfg. len ( ) )
175182 . field ( "toolchain" , & toolchain)
176183 . field ( "data_layout" , & target_layout)
184+ . field ( "n_cfg_overrides" , & cfg_overrides. len ( ) )
177185 . finish ( ) ,
178186 }
179187 }
@@ -227,6 +235,7 @@ impl ProjectWorkspace {
227235 project_json,
228236 config. target . as_deref ( ) ,
229237 & config. extra_env ,
238+ & config. cfg_overrides ,
230239 )
231240 }
232241 ProjectManifest :: CargoToml ( cargo_toml) => {
@@ -368,6 +377,7 @@ impl ProjectWorkspace {
368377 project_json : ProjectJson ,
369378 target : Option < & str > ,
370379 extra_env : & FxHashMap < String , String > ,
380+ cfg_overrides : & CfgOverrides ,
371381 ) -> ProjectWorkspace {
372382 let sysroot = match ( project_json. sysroot . clone ( ) , project_json. sysroot_src . clone ( ) ) {
373383 ( Some ( sysroot) , Some ( sysroot_src) ) => {
@@ -414,6 +424,7 @@ impl ProjectWorkspace {
414424 rustc_cfg,
415425 toolchain,
416426 target_layout : data_layout. map ( Arc :: from) . map_err ( |it| Arc :: from ( it. to_string ( ) ) ) ,
427+ cfg_overrides : cfg_overrides. clone ( ) ,
417428 }
418429 }
419430
@@ -464,6 +475,7 @@ impl ProjectWorkspace {
464475 rustc_cfg,
465476 toolchain,
466477 target_layout : data_layout. map ( Arc :: from) . map_err ( |it| Arc :: from ( it. to_string ( ) ) ) ,
478+ cfg_overrides : config. cfg_overrides . clone ( ) ,
467479 } )
468480 }
469481
@@ -619,6 +631,7 @@ impl ProjectWorkspace {
619631 rustc_cfg : _,
620632 toolchain : _,
621633 target_layout : _,
634+ cfg_overrides : _,
622635 } => project
623636 . crates ( )
624637 . map ( |( _, krate) | PackageRoot {
@@ -734,13 +747,15 @@ impl ProjectWorkspace {
734747 rustc_cfg,
735748 toolchain : _,
736749 target_layout : _,
750+ cfg_overrides,
737751 } => (
738752 project_json_to_crate_graph (
739753 rustc_cfg. clone ( ) ,
740754 load,
741755 project,
742756 sysroot. as_ref ( ) . ok ( ) ,
743757 extra_env,
758+ cfg_overrides,
744759 ) ,
745760 sysroot,
746761 ) ,
@@ -772,12 +787,14 @@ impl ProjectWorkspace {
772787 rustc_cfg,
773788 toolchain : _,
774789 target_layout : _,
790+ cfg_overrides,
775791 } => (
776792 detached_files_to_crate_graph (
777793 rustc_cfg. clone ( ) ,
778794 load,
779795 files,
780796 sysroot. as_ref ( ) . ok ( ) ,
797+ cfg_overrides,
781798 ) ,
782799 sysroot,
783800 ) ,
@@ -828,35 +845,53 @@ impl ProjectWorkspace {
828845 && cargo_config_extra_env == o_cargo_config_extra_env
829846 }
830847 (
831- Self :: Json { project, sysroot, rustc_cfg, toolchain, target_layout : _ } ,
848+ Self :: Json {
849+ project,
850+ sysroot,
851+ rustc_cfg,
852+ toolchain,
853+ target_layout : _,
854+ cfg_overrides,
855+ } ,
832856 Self :: Json {
833857 project : o_project,
834858 sysroot : o_sysroot,
835859 rustc_cfg : o_rustc_cfg,
836860 toolchain : o_toolchain,
837861 target_layout : _,
862+ cfg_overrides : o_cfg_overrides,
838863 } ,
839864 ) => {
840865 project == o_project
841866 && rustc_cfg == o_rustc_cfg
842867 && sysroot == o_sysroot
843868 && toolchain == o_toolchain
869+ && cfg_overrides == o_cfg_overrides
844870 }
845871 (
846- Self :: DetachedFiles { files, sysroot, rustc_cfg, toolchain, target_layout } ,
872+ Self :: DetachedFiles {
873+ files,
874+ sysroot,
875+ rustc_cfg,
876+ toolchain,
877+ target_layout,
878+ cfg_overrides,
879+ } ,
847880 Self :: DetachedFiles {
848881 files : o_files,
849882 sysroot : o_sysroot,
850883 rustc_cfg : o_rustc_cfg,
851884 toolchain : o_toolchain,
852885 target_layout : o_target_layout,
886+ cfg_overrides : o_cfg_overrides,
853887 } ,
854888 ) => {
855889 files == o_files
856890 && sysroot == o_sysroot
857891 && rustc_cfg == o_rustc_cfg
858892 && toolchain == o_toolchain
859893 && target_layout == o_target_layout
894+ && cfg_overrides == o_cfg_overrides
860895 }
861896 _ => false ,
862897 }
@@ -877,6 +912,7 @@ fn project_json_to_crate_graph(
877912 project : & ProjectJson ,
878913 sysroot : Option < & Sysroot > ,
879914 extra_env : & FxHashMap < String , String > ,
915+ override_cfg : & CfgOverrides ,
880916) -> ( CrateGraph , ProcMacroPaths ) {
881917 let mut res = ( CrateGraph :: default ( ) , ProcMacroPaths :: default ( ) ) ;
882918 let ( crate_graph, proc_macros) = & mut res;
@@ -916,19 +952,22 @@ fn project_json_to_crate_graph(
916952 None => & rustc_cfg,
917953 } ;
918954
955+ let mut cfg_options = target_cfgs
956+ . iter ( )
957+ . chain ( cfg. iter ( ) )
958+ . chain ( iter:: once ( & r_a_cfg_flag) )
959+ . cloned ( )
960+ . collect ( ) ;
961+ override_cfg. apply (
962+ & mut cfg_options,
963+ display_name. as_ref ( ) . map ( |it| it. canonical_name ( ) ) . unwrap_or_default ( ) ,
964+ ) ;
919965 let crate_graph_crate_id = crate_graph. add_crate_root (
920966 file_id,
921967 * edition,
922968 display_name. clone ( ) ,
923969 version. clone ( ) ,
924- Arc :: new (
925- target_cfgs
926- . iter ( )
927- . chain ( cfg. iter ( ) )
928- . chain ( iter:: once ( & r_a_cfg_flag) )
929- . cloned ( )
930- . collect ( ) ,
931- ) ,
970+ Arc :: new ( cfg_options) ,
932971 None ,
933972 env,
934973 * is_proc_macro,
@@ -992,7 +1031,7 @@ fn cargo_to_crate_graph(
9921031 None => ( SysrootPublicDeps :: default ( ) , None ) ,
9931032 } ;
9941033
995- let cfg_options = create_cfg_options ( rustc_cfg) ;
1034+ let cfg_options = CfgOptions :: from_iter ( rustc_cfg) ;
9961035
9971036 // Mapping of a package to its library target
9981037 let mut pkg_to_lib_crate = FxHashMap :: default ( ) ;
@@ -1168,6 +1207,7 @@ fn detached_files_to_crate_graph(
11681207 load : FileLoader < ' _ > ,
11691208 detached_files : & [ AbsPathBuf ] ,
11701209 sysroot : Option < & Sysroot > ,
1210+ override_cfg : & CfgOverrides ,
11711211) -> ( CrateGraph , ProcMacroPaths ) {
11721212 let _p = tracing:: span!( tracing:: Level :: INFO , "detached_files_to_crate_graph" ) . entered ( ) ;
11731213 let mut crate_graph = CrateGraph :: default ( ) ;
@@ -1176,8 +1216,10 @@ fn detached_files_to_crate_graph(
11761216 None => ( SysrootPublicDeps :: default ( ) , None ) ,
11771217 } ;
11781218
1179- let mut cfg_options = create_cfg_options ( rustc_cfg) ;
1219+ let mut cfg_options = CfgOptions :: from_iter ( rustc_cfg) ;
1220+ cfg_options. insert_atom ( "test" . into ( ) ) ;
11801221 cfg_options. insert_atom ( "rust_analyzer" . into ( ) ) ;
1222+ override_cfg. apply ( & mut cfg_options, "" ) ;
11811223 let cfg_options = Arc :: new ( cfg_options) ;
11821224
11831225 for detached_file in detached_files {
@@ -1411,7 +1453,11 @@ fn sysroot_to_crate_graph(
14111453 cargo,
14121454 None ,
14131455 rustc_cfg,
1414- & CfgOverrides :: default ( ) ,
1456+ & CfgOverrides {
1457+ global : CfgDiff :: new ( vec ! [ CfgAtom :: Flag ( "debug_assertions" . into( ) ) ] , vec ! [ ] )
1458+ . unwrap ( ) ,
1459+ ..Default :: default ( )
1460+ } ,
14151461 & WorkspaceBuildScripts :: default ( ) ,
14161462 ) ;
14171463
@@ -1469,7 +1515,12 @@ fn sysroot_to_crate_graph(
14691515 ( SysrootPublicDeps { deps : pub_deps } , libproc_macro)
14701516 }
14711517 SysrootMode :: Stitched ( stitched) => {
1472- let cfg_options = Arc :: new ( create_cfg_options ( rustc_cfg) ) ;
1518+ let cfg_options = Arc :: new ( {
1519+ let mut cfg_options = CfgOptions :: default ( ) ;
1520+ cfg_options. extend ( rustc_cfg) ;
1521+ cfg_options. insert_atom ( "debug_assertions" . into ( ) ) ;
1522+ cfg_options
1523+ } ) ;
14731524 let sysroot_crates: FxHashMap < SysrootCrate , CrateId > = stitched
14741525 . crates ( )
14751526 . filter_map ( |krate| {
@@ -1542,10 +1593,3 @@ fn add_dep_inner(graph: &mut CrateGraph, from: CrateId, dep: Dependency) {
15421593 tracing:: error!( "{}" , err)
15431594 }
15441595}
1545-
1546- fn create_cfg_options ( rustc_cfg : Vec < CfgFlag > ) -> CfgOptions {
1547- let mut cfg_options = CfgOptions :: default ( ) ;
1548- cfg_options. extend ( rustc_cfg) ;
1549- cfg_options. insert_atom ( "debug_assertions" . into ( ) ) ;
1550- cfg_options
1551- }
0 commit comments