Skip to content

Commit 2e54c0a

Browse files
committed
Remove rustc core test cfg hacks
1 parent 062e1b9 commit 2e54c0a

File tree

4 files changed

+14
-62
lines changed

4 files changed

+14
-62
lines changed

crates/project-model/src/workspace.rs

+13-28
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ impl CfgOverrides {
4343
pub fn len(&self) -> usize {
4444
self.global.len() + self.selective.values().map(|it| it.len()).sum::<usize>()
4545
}
46+
47+
fn apply(&self, cfg_options: &mut CfgOptions, name: &str) {
48+
if !self.global.is_empty() {
49+
cfg_options.apply_diff(self.global.clone());
50+
};
51+
if let Some(diff) = self.selective.get(name) {
52+
cfg_options.apply_diff(diff.clone());
53+
};
54+
}
4655
}
4756

4857
/// `PackageRoot` describes a package root folder.
@@ -999,25 +1008,13 @@ fn cargo_to_crate_graph(
9991008
let cfg_options = {
10001009
let mut cfg_options = cfg_options.clone();
10011010

1002-
// Add test cfg for local crates
10031011
if cargo[pkg].is_local {
1012+
// Add test cfg for local crates
10041013
cfg_options.insert_atom("test".into());
10051014
cfg_options.insert_atom("rust_analyzer".into());
10061015
}
10071016

1008-
if !override_cfg.global.is_empty() {
1009-
cfg_options.apply_diff(override_cfg.global.clone());
1010-
};
1011-
if let Some(diff) = override_cfg.selective.get(&cargo[pkg].name) {
1012-
// FIXME: this is sort of a hack to deal with #![cfg(not(test))] vanishing such as seen
1013-
// in ed25519_dalek (#7243), and libcore (#9203) (although you only hit that one while
1014-
// working on rust-lang/rust as that's the only time it appears outside sysroot).
1015-
//
1016-
// A more ideal solution might be to reanalyze crates based on where the cursor is and
1017-
// figure out the set of cfgs that would have to apply to make it active.
1018-
1019-
cfg_options.apply_diff(diff.clone());
1020-
};
1017+
override_cfg.apply(&mut cfg_options, &cargo[pkg].name);
10211018
cfg_options
10221019
};
10231020

@@ -1153,6 +1150,7 @@ fn cargo_to_crate_graph(
11531150
&pkg_crates,
11541151
&cfg_options,
11551152
override_cfg,
1153+
// FIXME: Remove this once rustc switched over to rust-project.json
11561154
if rustc_workspace.workspace_root() == cargo.workspace_root() {
11571155
// the rustc workspace does not use the installed toolchain's proc-macro server
11581156
// so we need to make sure we don't use the pre compiled proc-macros there either
@@ -1250,20 +1248,7 @@ fn handle_rustc_crates(
12501248
}
12511249

12521250
let mut cfg_options = cfg_options.clone();
1253-
1254-
if !override_cfg.global.is_empty() {
1255-
cfg_options.apply_diff(override_cfg.global.clone());
1256-
};
1257-
if let Some(diff) = override_cfg.selective.get(&rustc_workspace[pkg].name) {
1258-
// FIXME: this is sort of a hack to deal with #![cfg(not(test))] vanishing such as seen
1259-
// in ed25519_dalek (#7243), and libcore (#9203) (although you only hit that one while
1260-
// working on rust-lang/rust as that's the only time it appears outside sysroot).
1261-
//
1262-
// A more ideal solution might be to reanalyze crates based on where the cursor is and
1263-
// figure out the set of cfgs that would have to apply to make it active.
1264-
1265-
cfg_options.apply_diff(diff.clone());
1266-
};
1251+
override_cfg.apply(&mut cfg_options, &rustc_workspace[pkg].name);
12671252

12681253
for &tgt in rustc_workspace[pkg].targets.iter() {
12691254
let kind @ TargetKind::Lib { is_proc_macro } = rustc_workspace[tgt].kind else {

crates/rust-analyzer/src/config.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ config_data! {
166166
/// Set to `true` to use a subdirectory of the existing target directory or
167167
/// set to a path relative to the workspace to use that path.
168168
cargo_targetDir | rust_analyzerTargetDir: Option<TargetDirectory> = None,
169-
/// Unsets the implicit `#[cfg(test)]` for the specified crates.
170-
cargo_unsetTest: Vec<String> = vec!["core".to_owned()],
171169

172170
/// Run the check command for diagnostics on save.
173171
checkOnSave | checkOnSave_enable: bool = true,
@@ -1604,16 +1602,7 @@ impl Config {
16041602
vec![],
16051603
)
16061604
.unwrap(),
1607-
selective: self
1608-
.cargo_unsetTest()
1609-
.iter()
1610-
.map(|it| {
1611-
(
1612-
it.clone(),
1613-
CfgDiff::new(vec![], vec![CfgAtom::Flag("test".into())]).unwrap(),
1614-
)
1615-
})
1616-
.collect(),
1605+
selective: Default::default(),
16171606
},
16181607
wrap_rustc_in_build_scripts: *self.cargo_buildScripts_useRustcWrapper(),
16191608
invocation_strategy: match self.cargo_buildScripts_invocationStrategy() {

docs/user/generated_config.adoc

-12
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,6 @@ building from locking the `Cargo.lock` at the expense of duplicating build artif
158158

159159
Set to `true` to use a subdirectory of the existing target directory or
160160
set to a path relative to the workspace to use that path.
161-
--
162-
[[rust-analyzer.cargo.unsetTest]]rust-analyzer.cargo.unsetTest::
163-
+
164-
--
165-
Default:
166-
----
167-
[
168-
"core"
169-
]
170-
----
171-
Unsets the implicit `#[cfg(test)]` for the specified crates.
172-
173161
--
174162
[[rust-analyzer.checkOnSave]]rust-analyzer.checkOnSave (default: `true`)::
175163
+

editors/code/package.json

-10
Original file line numberDiff line numberDiff line change
@@ -696,16 +696,6 @@
696696
}
697697
]
698698
},
699-
"rust-analyzer.cargo.unsetTest": {
700-
"markdownDescription": "Unsets the implicit `#[cfg(test)]` for the specified crates.",
701-
"default": [
702-
"core"
703-
],
704-
"type": "array",
705-
"items": {
706-
"type": "string"
707-
}
708-
},
709699
"rust-analyzer.checkOnSave": {
710700
"markdownDescription": "Run the check command for diagnostics on save.",
711701
"default": true,

0 commit comments

Comments
 (0)