Remove patch sysroot cfg-if hack

This commit is contained in:
Lukas Wirth 2024-12-09 11:42:51 +01:00
parent 1c3043bc8c
commit 7085328185
2 changed files with 18 additions and 53 deletions

View file

@ -547,29 +547,6 @@ impl CrateGraph {
None None
} }
// Work around for https://github.com/rust-lang/rust-analyzer/issues/6038.
// As hacky as it gets.
pub fn patch_cfg_if(&mut self) -> bool {
// we stupidly max by version in an attempt to have all duplicated std's depend on the same cfg_if so that deduplication still works
let cfg_if =
self.hacky_find_crate("cfg_if").max_by_key(|&it| self.arena[it].version.clone());
let std = self.hacky_find_crate("std").next();
match (cfg_if, std) {
(Some(cfg_if), Some(std)) => {
self.arena[cfg_if].dependencies.clear();
self.arena[std]
.dependencies
.push(Dependency::new(CrateName::new("cfg_if").unwrap(), cfg_if));
true
}
_ => false,
}
}
fn hacky_find_crate<'a>(&'a self, display_name: &'a str) -> impl Iterator<Item = CrateId> + 'a {
self.iter().filter(move |it| self[*it].display_name.as_deref() == Some(display_name))
}
/// Removes all crates from this crate graph except for the ones in `to_keep` and fixes up the dependencies. /// Removes all crates from this crate graph except for the ones in `to_keep` and fixes up the dependencies.
/// Returns a mapping from old crate ids to new crate ids. /// Returns a mapping from old crate ids to new crate ids.
pub fn remove_crates_except(&mut self, to_keep: &[CrateId]) -> Vec<Option<CrateId>> { pub fn remove_crates_except(&mut self, to_keep: &[CrateId]) -> Vec<Option<CrateId>> {

View file

@ -747,9 +747,8 @@ impl ProjectWorkspace {
let _p = tracing::info_span!("ProjectWorkspace::to_crate_graph").entered(); let _p = tracing::info_span!("ProjectWorkspace::to_crate_graph").entered();
let Self { kind, sysroot, cfg_overrides, rustc_cfg, .. } = self; let Self { kind, sysroot, cfg_overrides, rustc_cfg, .. } = self;
let ((mut crate_graph, proc_macros), sysroot) = match kind { let (crate_graph, proc_macros) = match kind {
ProjectWorkspaceKind::Json(project) => ( ProjectWorkspaceKind::Json(project) => project_json_to_crate_graph(
project_json_to_crate_graph(
rustc_cfg.clone(), rustc_cfg.clone(),
load, load,
project, project,
@ -757,8 +756,6 @@ impl ProjectWorkspace {
extra_env, extra_env,
cfg_overrides, cfg_overrides,
), ),
sysroot,
),
ProjectWorkspaceKind::Cargo { ProjectWorkspaceKind::Cargo {
cargo, cargo,
rustc, rustc,
@ -766,8 +763,7 @@ impl ProjectWorkspace {
cargo_config_extra_env: _, cargo_config_extra_env: _,
error: _, error: _,
set_test, set_test,
} => ( } => cargo_to_crate_graph(
cargo_to_crate_graph(
load, load,
rustc.as_ref().map(|a| a.as_ref()).ok(), rustc.as_ref().map(|a| a.as_ref()).ok(),
cargo, cargo,
@ -777,9 +773,7 @@ impl ProjectWorkspace {
build_scripts, build_scripts,
*set_test, *set_test,
), ),
sysroot, ProjectWorkspaceKind::DetachedFile { file, cargo: cargo_script, set_test, .. } => {
),
ProjectWorkspaceKind::DetachedFile { file, cargo: cargo_script, set_test, .. } => (
if let Some((cargo, build_scripts, _)) = cargo_script { if let Some((cargo, build_scripts, _)) = cargo_script {
cargo_to_crate_graph( cargo_to_crate_graph(
&mut |path| load(path), &mut |path| load(path),
@ -800,16 +794,10 @@ impl ProjectWorkspace {
cfg_overrides, cfg_overrides,
*set_test, *set_test,
) )
}, }
sysroot, }
),
}; };
if matches!(sysroot.mode(), SysrootMode::Stitched(_)) && crate_graph.patch_cfg_if() {
debug!("Patched std to depend on cfg-if")
} else {
debug!("Did not patch std to depend on cfg-if")
}
(crate_graph, proc_macros) (crate_graph, proc_macros)
} }