Skip to content

Commit

Permalink
Remove rustc workspace hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Mar 1, 2024
1 parent 9efa23c commit a247418
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 64 deletions.
48 changes: 13 additions & 35 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 @@ -983,25 +992,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 @@ -1115,13 +1112,7 @@ fn cargo_to_crate_graph(
&pkg_crates,
&cfg_options,
override_cfg,
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
build_scripts
} else {
rustc_build_scripts
},
rustc_build_scripts,
);
}
}
Expand Down Expand Up @@ -1212,20 +1203,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
19 changes: 5 additions & 14 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ config_data! {
// FIXME(@poliorcetics): move to multiple targets here too, but this will need more work
// than `checkOnSave_target`
cargo_target: Option<String> = "null",
/// Unsets the implicit `#[cfg(test)]` for the specified crates.
cargo_unsetTest: Vec<String> = "[\"core\"]",

/// Run the check command for diagnostics on save.
checkOnSave | checkOnSave_enable: bool = "true",
Expand Down Expand Up @@ -1292,18 +1290,11 @@ impl Config {
.collect(),
vec![],
)
.unwrap(),
selective: self
.data
.cargo_unsetTest
.iter()
.map(|it| {
(
it.clone(),
CfgDiff::new(vec![], vec![CfgAtom::Flag("test".into())]).unwrap(),
)
})
.collect(),
.unwrap_or_else(|| {
tracing::error!("`cargo.cfgs` config contains duplicate keys");
Default::default()
}),
selective: Default::default(),
},
wrap_rustc_in_build_scripts: self.data.cargo_buildScripts_useRustcWrapper,
invocation_strategy: match self.data.cargo_buildScripts_invocationStrategy {
Expand Down
5 changes: 0 additions & 5 deletions docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ This option does not take effect until rust-analyzer is restarted.
--
Compilation target override (target triple).
--
[[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 @@ -671,16 +671,6 @@
"string"
]
},
"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

0 comments on commit a247418

Please sign in to comment.