Skip to content

internal: Remove rustc core test cfg hacks #16726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 13 additions & 28 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ impl CfgOverrides {
pub fn len(&self) -> usize {
self.global.len() + self.selective.values().map(|it| it.len()).sum::<usize>()
}

fn apply(&self, cfg_options: &mut CfgOptions, name: &str) {
if !self.global.is_empty() {
cfg_options.apply_diff(self.global.clone());
};
if let Some(diff) = self.selective.get(name) {
cfg_options.apply_diff(diff.clone());
};
}
}

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

// Add test cfg for local crates
if cargo[pkg].is_local {
// Add test cfg for local crates
cfg_options.insert_atom("test".into());
cfg_options.insert_atom("rust_analyzer".into());
}

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

cfg_options.apply_diff(diff.clone());
};
override_cfg.apply(&mut cfg_options, &cargo[pkg].name);
cfg_options
};

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

let mut cfg_options = cfg_options.clone();

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

cfg_options.apply_diff(diff.clone());
};
override_cfg.apply(&mut cfg_options, &rustc_workspace[pkg].name);

for &tgt in rustc_workspace[pkg].targets.iter() {
let kind @ TargetKind::Lib { is_proc_macro } = rustc_workspace[tgt].kind else {
Expand Down
13 changes: 1 addition & 12 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ config_data! {
/// Set to `true` to use a subdirectory of the existing target directory or
/// set to a path relative to the workspace to use that path.
cargo_targetDir | rust_analyzerTargetDir: Option<TargetDirectory> = None,
/// Unsets the implicit `#[cfg(test)]` for the specified crates.
cargo_unsetTest: Vec<String> = vec!["core".to_owned()],

/// Run the check command for diagnostics on save.
checkOnSave | checkOnSave_enable: bool = true,
Expand Down Expand Up @@ -1604,16 +1602,7 @@ impl Config {
vec![],
)
.unwrap(),
selective: self
.cargo_unsetTest()
.iter()
.map(|it| {
(
it.clone(),
CfgDiff::new(vec![], vec![CfgAtom::Flag("test".into())]).unwrap(),
)
})
.collect(),
selective: Default::default(),
},
wrap_rustc_in_build_scripts: *self.cargo_buildScripts_useRustcWrapper(),
invocation_strategy: match self.cargo_buildScripts_invocationStrategy() {
Expand Down
12 changes: 0 additions & 12 deletions docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,6 @@ building from locking the `Cargo.lock` at the expense of duplicating build artif

Set to `true` to use a subdirectory of the existing target directory or
set to a path relative to the workspace to use that path.
--
[[rust-analyzer.cargo.unsetTest]]rust-analyzer.cargo.unsetTest::
+
--
Default:
----
[
"core"
]
----
Unsets the implicit `#[cfg(test)]` for the specified crates.

--
[[rust-analyzer.checkOnSave]]rust-analyzer.checkOnSave (default: `true`)::
+
Expand Down
10 changes: 0 additions & 10 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -696,16 +696,6 @@
}
]
},
"rust-analyzer.cargo.unsetTest": {
"markdownDescription": "Unsets the implicit `#[cfg(test)]` for the specified crates.",
"default": [
"core"
],
"type": "array",
"items": {
"type": "string"
}
},
"rust-analyzer.checkOnSave": {
"markdownDescription": "Run the check command for diagnostics on save.",
"default": true,
Expand Down