Skip to content

Commit 0e6922f

Browse files
Implicitly depend on test
1 parent 0b76b29 commit 0e6922f

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

crates/project_model/src/sysroot.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! but we can't process `.rlib` and need source code instead. The source code
55
//! is typically installed with `rustup component add rust-src` command.
66
7-
use std::{convert::TryFrom, env, fs, ops, path::PathBuf, process::Command};
7+
use std::{convert::TryFrom, env, fs, iter, ops, path::PathBuf, process::Command};
88

99
use anyhow::{format_err, Result};
1010
use la_arena::{Arena, Idx};
@@ -39,10 +39,15 @@ impl Sysroot {
3939
&self.root
4040
}
4141

42-
pub fn public_deps(&self) -> impl Iterator<Item = (&'static str, SysrootCrate)> + '_ {
42+
pub fn public_deps(&self) -> impl Iterator<Item = (&'static str, SysrootCrate, bool)> + '_ {
4343
// core is added as a dependency before std in order to
4444
// mimic rustcs dependency order
45-
["core", "alloc", "std"].iter().filter_map(move |&it| Some((it, self.by_name(it)?)))
45+
["core", "alloc", "std"]
46+
.iter()
47+
.copied()
48+
.zip(iter::repeat(true))
49+
.chain(iter::once(("test", false)))
50+
.filter_map(move |(name, prelude)| Some((name, self.by_name(name)?, prelude)))
4651
}
4752

4853
pub fn proc_macro(&self) -> Option<SysrootCrate> {

crates/project_model/src/tests.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,15 @@ fn rust_project_hello_world_project_model() {
16901690
),
16911691
prelude: true,
16921692
},
1693+
Dependency {
1694+
crate_id: CrateId(
1695+
9,
1696+
),
1697+
name: CrateName(
1698+
"test",
1699+
),
1700+
prelude: false,
1701+
},
16931702
],
16941703
proc_macro: [],
16951704
},

crates/project_model/src/workspace.rs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,7 @@ fn project_json_to_crate_graph(
470470
for (from, krate) in project.crates() {
471471
if let Some(&from) = crates.get(&from) {
472472
if let Some((public_deps, libproc_macro)) = &sysroot_deps {
473-
for (name, to) in public_deps.iter() {
474-
add_dep(&mut crate_graph, from, name.clone(), *to)
475-
}
473+
public_deps.add(from, &mut crate_graph);
476474
if krate.is_proc_macro {
477475
if let Some(proc_macro) = libproc_macro {
478476
add_dep(
@@ -509,7 +507,7 @@ fn cargo_to_crate_graph(
509507
let mut crate_graph = CrateGraph::default();
510508
let (public_deps, libproc_macro) = match sysroot {
511509
Some(sysroot) => sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load),
512-
None => (Vec::new(), None),
510+
None => (SysrootPublicDeps::default(), None),
513511
};
514512

515513
let mut cfg_options = CfgOptions::default();
@@ -590,9 +588,7 @@ fn cargo_to_crate_graph(
590588
add_dep(&mut crate_graph, *from, name, to);
591589
}
592590
}
593-
for (name, krate) in public_deps.iter() {
594-
add_dep(&mut crate_graph, *from, name.clone(), *krate);
595-
}
591+
public_deps.add(*from, &mut crate_graph);
596592
}
597593
}
598594

@@ -674,9 +670,7 @@ fn detached_files_to_crate_graph(
674670
Vec::new(),
675671
);
676672

677-
for (name, krate) in public_deps.iter() {
678-
add_dep(&mut crate_graph, detached_file_crate, name.clone(), *krate);
679-
}
673+
public_deps.add(detached_file_crate, &mut crate_graph);
680674
}
681675
crate_graph
682676
}
@@ -688,7 +682,7 @@ fn handle_rustc_crates(
688682
cfg_options: &CfgOptions,
689683
load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
690684
pkg_to_lib_crate: &mut FxHashMap<la_arena::Idx<crate::PackageData>, CrateId>,
691-
public_deps: &[(CrateName, CrateId)],
685+
public_deps: &SysrootPublicDeps,
692686
cargo: &CargoWorkspace,
693687
pkg_crates: &FxHashMap<la_arena::Idx<crate::PackageData>, Vec<(CrateId, TargetKind)>>,
694688
) {
@@ -728,9 +722,7 @@ fn handle_rustc_crates(
728722
);
729723
pkg_to_lib_crate.insert(pkg, crate_id);
730724
// Add dependencies on core / std / alloc for this crate
731-
for (name, krate) in public_deps.iter() {
732-
add_dep(crate_graph, crate_id, name.clone(), *krate);
733-
}
725+
public_deps.add(crate_id, crate_graph);
734726
rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
735727
}
736728
}
@@ -828,12 +820,26 @@ fn add_target_crate_root(
828820
)
829821
}
830822

823+
#[derive(Default)]
824+
struct SysrootPublicDeps {
825+
deps: Vec<(CrateName, CrateId, bool)>,
826+
}
827+
828+
impl SysrootPublicDeps {
829+
/// Makes `from` depend on the public sysroot crates.
830+
fn add(&self, from: CrateId, crate_graph: &mut CrateGraph) {
831+
for (name, krate, prelude) in &self.deps {
832+
add_dep_with_prelude(crate_graph, from, name.clone(), *krate, *prelude);
833+
}
834+
}
835+
}
836+
831837
fn sysroot_to_crate_graph(
832838
crate_graph: &mut CrateGraph,
833839
sysroot: &Sysroot,
834840
rustc_cfg: Vec<CfgFlag>,
835841
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
836-
) -> (Vec<(CrateName, CrateId)>, Option<CrateId>) {
842+
) -> (SysrootPublicDeps, Option<CrateId>) {
837843
let _p = profile::span("sysroot_to_crate_graph");
838844
let mut cfg_options = CfgOptions::default();
839845
cfg_options.extend(rustc_cfg);
@@ -867,17 +873,35 @@ fn sysroot_to_crate_graph(
867873
}
868874
}
869875

870-
let public_deps = sysroot
871-
.public_deps()
872-
.map(|(name, idx)| (CrateName::new(name).unwrap(), sysroot_crates[&idx]))
873-
.collect::<Vec<_>>();
876+
let public_deps = SysrootPublicDeps {
877+
deps: sysroot
878+
.public_deps()
879+
.map(|(name, idx, prelude)| {
880+
(CrateName::new(name).unwrap(), sysroot_crates[&idx], prelude)
881+
})
882+
.collect::<Vec<_>>(),
883+
};
874884

875885
let libproc_macro = sysroot.proc_macro().and_then(|it| sysroot_crates.get(&it).copied());
876886
(public_deps, libproc_macro)
877887
}
878888

879889
fn add_dep(graph: &mut CrateGraph, from: CrateId, name: CrateName, to: CrateId) {
880-
if let Err(err) = graph.add_dep(from, Dependency::new(name, to)) {
890+
add_dep_inner(graph, from, Dependency::new(name, to))
891+
}
892+
893+
fn add_dep_with_prelude(
894+
graph: &mut CrateGraph,
895+
from: CrateId,
896+
name: CrateName,
897+
to: CrateId,
898+
prelude: bool,
899+
) {
900+
add_dep_inner(graph, from, Dependency::with_prelude(name, to, prelude))
901+
}
902+
903+
fn add_dep_inner(graph: &mut CrateGraph, from: CrateId, dep: Dependency) {
904+
if let Err(err) = graph.add_dep(from, dep) {
881905
tracing::error!("{}", err)
882906
}
883907
}

0 commit comments

Comments
 (0)