Skip to content

Commit ddbf43b

Browse files
committed
Move crate graph generation to ra_project_model
1 parent fcd615e commit ddbf43b

File tree

5 files changed

+116
-83
lines changed

5 files changed

+116
-83
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_db/src/input.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl CrateGraph {
9191
assert!(prev.is_none());
9292
crate_id
9393
}
94+
9495
pub fn add_dep(
9596
&mut self,
9697
from: CrateId,
@@ -102,22 +103,40 @@ impl CrateGraph {
102103
}
103104
Ok(self.arena.get_mut(&from).unwrap().add_dep(name, to))
104105
}
106+
105107
pub fn is_empty(&self) -> bool {
106108
self.arena.is_empty()
107109
}
110+
108111
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
109112
self.arena[&crate_id].file_id
110113
}
114+
111115
pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
112116
let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?;
113117
Some(crate_id)
114118
}
119+
115120
pub fn dependencies<'a>(
116121
&'a self,
117122
crate_id: CrateId,
118123
) -> impl Iterator<Item = &'a Dependency> + 'a {
119124
self.arena[&crate_id].dependencies.iter()
120125
}
126+
127+
/// Extends this crate graph by adding a complete disjoint second crate
128+
/// graph.
129+
pub fn extend(&mut self, other: CrateGraph) {
130+
let start = self.arena.len() as u32;
131+
self.arena.extend(other.arena.into_iter().map(|(id, mut data)| {
132+
let new_id = CrateId(id.0 + start);
133+
for dep in &mut data.dependencies {
134+
dep.crate_id = CrateId(dep.crate_id.0 + start);
135+
}
136+
(new_id, data)
137+
}));
138+
}
139+
121140
fn dfs_find(&self, target: CrateId, from: CrateId, visited: &mut FxHashSet<CrateId>) -> bool {
122141
if !visited.insert(from) {
123142
return false;

crates/ra_lsp_server/src/server_world.rs

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ use ra_ide_api::{
99
SourceRootId
1010
};
1111
use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot};
12-
use rustc_hash::FxHashMap;
1312
use relative_path::RelativePathBuf;
1413
use parking_lot::RwLock;
1514
use failure::format_err;
1615

1716
use crate::{
18-
project_model::{ProjectWorkspace, TargetKind},
17+
project_model::ProjectWorkspace,
1918
Result,
2019
};
2120

@@ -57,88 +56,10 @@ impl ServerWorldState {
5756
change.add_root(SourceRootId(r.0.into()), is_local);
5857
}
5958

59+
// Create crate graph from all the workspaces
6060
let mut crate_graph = CrateGraph::default();
6161
for ws in workspaces.iter() {
62-
// First, load std
63-
let mut sysroot_crates = FxHashMap::default();
64-
for krate in ws.sysroot.crates() {
65-
if let Some(file_id) = vfs.load(krate.root(&ws.sysroot)) {
66-
let file_id = FileId(file_id.0.into());
67-
sysroot_crates.insert(krate, crate_graph.add_crate_root(file_id));
68-
}
69-
}
70-
for from in ws.sysroot.crates() {
71-
for to in from.deps(&ws.sysroot) {
72-
let name = to.name(&ws.sysroot);
73-
if let (Some(&from), Some(&to)) =
74-
(sysroot_crates.get(&from), sysroot_crates.get(&to))
75-
{
76-
if let Err(_) = crate_graph.add_dep(from, name.clone(), to) {
77-
log::error!("cyclic dependency between sysroot crates")
78-
}
79-
}
80-
}
81-
}
82-
83-
let libstd = ws.sysroot.std().and_then(|it| sysroot_crates.get(&it).map(|&it| it));
84-
85-
let mut pkg_to_lib_crate = FxHashMap::default();
86-
let mut pkg_crates = FxHashMap::default();
87-
// Next, create crates for each package, target pair
88-
for pkg in ws.cargo.packages() {
89-
let mut lib_tgt = None;
90-
for tgt in pkg.targets(&ws.cargo) {
91-
let root = tgt.root(&ws.cargo);
92-
if let Some(file_id) = vfs.load(root) {
93-
let file_id = FileId(file_id.0.into());
94-
let crate_id = crate_graph.add_crate_root(file_id);
95-
if tgt.kind(&ws.cargo) == TargetKind::Lib {
96-
lib_tgt = Some(crate_id);
97-
pkg_to_lib_crate.insert(pkg, crate_id);
98-
}
99-
pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
100-
}
101-
}
102-
103-
// Set deps to the std and to the lib target of the current package
104-
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
105-
if let Some(to) = lib_tgt {
106-
if to != from {
107-
if let Err(_) =
108-
crate_graph.add_dep(from, pkg.name(&ws.cargo).into(), to)
109-
{
110-
log::error!(
111-
"cyclic dependency between targets of {}",
112-
pkg.name(&ws.cargo)
113-
)
114-
}
115-
}
116-
}
117-
if let Some(std) = libstd {
118-
if let Err(_) = crate_graph.add_dep(from, "std".into(), std) {
119-
log::error!("cyclic dependency on std for {}", pkg.name(&ws.cargo))
120-
}
121-
}
122-
}
123-
}
124-
125-
// Now add a dep ednge from all targets of upstream to the lib
126-
// target of downstream.
127-
for pkg in ws.cargo.packages() {
128-
for dep in pkg.dependencies(&ws.cargo) {
129-
if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
130-
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
131-
if let Err(_) = crate_graph.add_dep(from, dep.name.clone(), to) {
132-
log::error!(
133-
"cyclic dependency {} -> {}",
134-
pkg.name(&ws.cargo),
135-
dep.pkg.name(&ws.cargo)
136-
)
137-
}
138-
}
139-
}
140-
}
141-
}
62+
crate_graph.extend(ws.to_crate_graph(&mut vfs));
14263
}
14364
change.set_crate_graph(crate_graph);
14465

crates/ra_project_model/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = ["Aleksey Kladov <[email protected]>"]
77
[dependencies]
88
# itertools = "0.8.0"
99
# join_to_string = "0.1.3"
10-
# log = "0.4.5"
10+
log = "0.4.5"
1111
# relative-path = "0.4.0"
1212
# rayon = "1.0.2"
1313
# fst = "0.3.1"
@@ -25,6 +25,8 @@ walkdir = "2.2.7"
2525
cargo_metadata = "0.7.0"
2626

2727
ra_arena = { path = "../ra_arena" }
28+
ra_db = { path = "../ra_db" }
29+
ra_vfs = { path = "../ra_vfs" }
2830

2931
[dev-dependencies]
3032
test_utils = { path = "../test_utils" }

crates/ra_project_model/src/lib.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ mod sysroot;
44
use std::path::{Path, PathBuf};
55

66
use failure::bail;
7+
use rustc_hash::FxHashMap;
8+
9+
use ra_db::{CrateGraph, FileId};
10+
use ra_vfs::Vfs;
711

812
pub use crate::{
913
cargo_workspace::{CargoWorkspace, Package, Target, TargetKind},
@@ -27,6 +31,90 @@ impl ProjectWorkspace {
2731
let res = ProjectWorkspace { cargo, sysroot };
2832
Ok(res)
2933
}
34+
35+
pub fn to_crate_graph(&self, vfs: &mut Vfs) -> CrateGraph {
36+
let mut crate_graph = CrateGraph::default();
37+
let mut sysroot_crates = FxHashMap::default();
38+
for krate in self.sysroot.crates() {
39+
if let Some(file_id) = vfs.load(krate.root(&self.sysroot)) {
40+
let file_id = FileId(file_id.0.into());
41+
sysroot_crates.insert(krate, crate_graph.add_crate_root(file_id));
42+
}
43+
}
44+
for from in self.sysroot.crates() {
45+
for to in from.deps(&self.sysroot) {
46+
let name = to.name(&self.sysroot);
47+
if let (Some(&from), Some(&to)) =
48+
(sysroot_crates.get(&from), sysroot_crates.get(&to))
49+
{
50+
if let Err(_) = crate_graph.add_dep(from, name.clone(), to) {
51+
log::error!("cyclic dependency between sysroot crates")
52+
}
53+
}
54+
}
55+
}
56+
57+
let libstd = self.sysroot.std().and_then(|it| sysroot_crates.get(&it).map(|&it| it));
58+
59+
let mut pkg_to_lib_crate = FxHashMap::default();
60+
let mut pkg_crates = FxHashMap::default();
61+
// Next, create crates for each package, target pair
62+
for pkg in self.cargo.packages() {
63+
let mut lib_tgt = None;
64+
for tgt in pkg.targets(&self.cargo) {
65+
let root = tgt.root(&self.cargo);
66+
if let Some(file_id) = vfs.load(root) {
67+
let file_id = FileId(file_id.0.into());
68+
let crate_id = crate_graph.add_crate_root(file_id);
69+
if tgt.kind(&self.cargo) == TargetKind::Lib {
70+
lib_tgt = Some(crate_id);
71+
pkg_to_lib_crate.insert(pkg, crate_id);
72+
}
73+
pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
74+
}
75+
}
76+
77+
// Set deps to the std and to the lib target of the current package
78+
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
79+
if let Some(to) = lib_tgt {
80+
if to != from {
81+
if let Err(_) = crate_graph.add_dep(from, pkg.name(&self.cargo).into(), to)
82+
{
83+
log::error!(
84+
"cyclic dependency between targets of {}",
85+
pkg.name(&self.cargo)
86+
)
87+
}
88+
}
89+
}
90+
if let Some(std) = libstd {
91+
if let Err(_) = crate_graph.add_dep(from, "std".into(), std) {
92+
log::error!("cyclic dependency on std for {}", pkg.name(&self.cargo))
93+
}
94+
}
95+
}
96+
}
97+
98+
// Now add a dep ednge from all targets of upstream to the lib
99+
// target of downstream.
100+
for pkg in self.cargo.packages() {
101+
for dep in pkg.dependencies(&self.cargo) {
102+
if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
103+
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
104+
if let Err(_) = crate_graph.add_dep(from, dep.name.clone(), to) {
105+
log::error!(
106+
"cyclic dependency {} -> {}",
107+
pkg.name(&self.cargo),
108+
dep.pkg.name(&self.cargo)
109+
)
110+
}
111+
}
112+
}
113+
}
114+
}
115+
116+
crate_graph
117+
}
30118
}
31119

32120
fn find_cargo_toml(path: &Path) -> Result<PathBuf> {

0 commit comments

Comments
 (0)