fix: Correctly handle no_core/no_std for preludes

This commit is contained in:
Lukas Wirth 2024-05-02 09:53:42 +02:00
parent cfce2bb46d
commit a268eaf053
9 changed files with 120 additions and 71 deletions

View file

@ -4,7 +4,7 @@
//! but we can't process `.rlib` and need source code instead. The source code
//! is typically installed with `rustup component add rust-src` command.
use std::{env, fs, iter, ops, process::Command, sync::Arc};
use std::{env, fs, ops, process::Command, sync::Arc};
use anyhow::{format_err, Result};
use base_db::CrateName;
@ -58,13 +58,11 @@ impl Stitched {
pub(crate) fn public_deps(&self) -> impl Iterator<Item = (CrateName, SysrootCrate, bool)> + '_ {
// core is added as a dependency before std in order to
// mimic rustcs dependency order
["core", "alloc", "std"]
.into_iter()
.zip(iter::repeat(true))
.chain(iter::once(("test", false)))
.filter_map(move |(name, prelude)| {
[("core", true), ("alloc", false), ("std", true), ("test", false)].into_iter().filter_map(
move |(name, prelude)| {
Some((CrateName::new(name).unwrap(), self.by_name(name)?, prelude))
})
},
)
}
pub(crate) fn proc_macro(&self) -> Option<SysrootCrate> {

View file

@ -1413,7 +1413,7 @@ impl SysrootPublicDeps {
/// Makes `from` depend on the public sysroot crates.
fn add_to_crate_graph(&self, crate_graph: &mut CrateGraph, from: CrateId) {
for (name, krate, prelude) in &self.deps {
add_dep_with_prelude(crate_graph, from, name.clone(), *krate, *prelude);
add_dep_with_prelude(crate_graph, from, name.clone(), *krate, *prelude, true);
}
}
}
@ -1466,7 +1466,7 @@ fn sysroot_to_crate_graph(
| LangCrateOrigin::Std => pub_deps.push((
CrateName::normalize_dashes(&lang_crate.to_string()),
cid,
!matches!(lang_crate, LangCrateOrigin::Test),
!matches!(lang_crate, LangCrateOrigin::Test | LangCrateOrigin::Alloc),
)),
LangCrateOrigin::ProcMacro => libproc_macro = Some(cid),
LangCrateOrigin::Other => (),
@ -1567,12 +1567,20 @@ fn add_dep_with_prelude(
name: CrateName,
to: CrateId,
prelude: bool,
sysroot: bool,
) {
add_dep_inner(graph, from, Dependency::with_prelude(name, to, prelude))
add_dep_inner(graph, from, Dependency::with_prelude(name, to, prelude, sysroot))
}
fn add_proc_macro_dep(crate_graph: &mut CrateGraph, from: CrateId, to: CrateId, prelude: bool) {
add_dep_with_prelude(crate_graph, from, CrateName::new("proc_macro").unwrap(), to, prelude);
add_dep_with_prelude(
crate_graph,
from,
CrateName::new("proc_macro").unwrap(),
to,
prelude,
true,
);
}
fn add_dep_inner(graph: &mut CrateGraph, from: CrateId, dep: Dependency) {