diff options
-rw-r--r-- | crates/project_model/src/lib.rs | 36 | ||||
-rw-r--r-- | crates/project_model/src/project_json.rs | 8 |
2 files changed, 20 insertions, 24 deletions
diff --git a/crates/project_model/src/lib.rs b/crates/project_model/src/lib.rs index cbd4c5009..724c586db 100644 --- a/crates/project_model/src/lib.rs +++ b/crates/project_model/src/lib.rs | |||
@@ -43,7 +43,7 @@ impl fmt::Debug for ProjectWorkspace { | |||
43 | f.debug_struct("Cargo").field("n_packages", &cargo.packages().len()).finish() | 43 | f.debug_struct("Cargo").field("n_packages", &cargo.packages().len()).finish() |
44 | } | 44 | } |
45 | ProjectWorkspace::Json { project } => { | 45 | ProjectWorkspace::Json { project } => { |
46 | f.debug_struct("Json").field("n_crates", &project.crates.len()).finish() | 46 | f.debug_struct("Json").field("n_crates", &project.n_crates()).finish() |
47 | } | 47 | } |
48 | } | 48 | } |
49 | } | 49 | } |
@@ -202,9 +202,8 @@ impl ProjectWorkspace { | |||
202 | pub fn to_roots(&self) -> Vec<PackageRoot> { | 202 | pub fn to_roots(&self) -> Vec<PackageRoot> { |
203 | match self { | 203 | match self { |
204 | ProjectWorkspace::Json { project } => project | 204 | ProjectWorkspace::Json { project } => project |
205 | .crates | 205 | .crates() |
206 | .iter() | 206 | .map(|(_, krate)| PackageRoot { |
207 | .map(|krate| PackageRoot { | ||
208 | is_member: krate.is_workspace_member, | 207 | is_member: krate.is_workspace_member, |
209 | include: krate.include.clone(), | 208 | include: krate.include.clone(), |
210 | exclude: krate.exclude.clone(), | 209 | exclude: krate.exclude.clone(), |
@@ -243,9 +242,8 @@ impl ProjectWorkspace { | |||
243 | pub fn proc_macro_dylib_paths(&self) -> Vec<AbsPathBuf> { | 242 | pub fn proc_macro_dylib_paths(&self) -> Vec<AbsPathBuf> { |
244 | match self { | 243 | match self { |
245 | ProjectWorkspace::Json { project } => project | 244 | ProjectWorkspace::Json { project } => project |
246 | .crates | 245 | .crates() |
247 | .iter() | 246 | .filter_map(|(_, krate)| krate.proc_macro_dylib_path.as_ref()) |
248 | .filter_map(|krate| krate.proc_macro_dylib_path.as_ref()) | ||
249 | .cloned() | 247 | .cloned() |
250 | .collect(), | 248 | .collect(), |
251 | ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => cargo | 249 | ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => cargo |
@@ -258,7 +256,7 @@ impl ProjectWorkspace { | |||
258 | 256 | ||
259 | pub fn n_packages(&self) -> usize { | 257 | pub fn n_packages(&self) -> usize { |
260 | match self { | 258 | match self { |
261 | ProjectWorkspace::Json { project, .. } => project.crates.len(), | 259 | ProjectWorkspace::Json { project, .. } => project.n_crates(), |
262 | ProjectWorkspace::Cargo { cargo, sysroot } => { | 260 | ProjectWorkspace::Cargo { cargo, sysroot } => { |
263 | cargo.packages().len() + sysroot.crates().len() | 261 | cargo.packages().len() + sysroot.crates().len() |
264 | } | 262 | } |
@@ -276,10 +274,8 @@ impl ProjectWorkspace { | |||
276 | ProjectWorkspace::Json { project } => { | 274 | ProjectWorkspace::Json { project } => { |
277 | let mut cfg_cache: FxHashMap<Option<&str>, Vec<CfgFlag>> = FxHashMap::default(); | 275 | let mut cfg_cache: FxHashMap<Option<&str>, Vec<CfgFlag>> = FxHashMap::default(); |
278 | let crates: FxHashMap<_, _> = project | 276 | let crates: FxHashMap<_, _> = project |
279 | .crates | 277 | .crates() |
280 | .iter() | 278 | .filter_map(|(crate_id, krate)| { |
281 | .enumerate() | ||
282 | .filter_map(|(seq_index, krate)| { | ||
283 | let file_path = &krate.root_module; | 279 | let file_path = &krate.root_module; |
284 | let file_id = load(&file_path)?; | 280 | let file_id = load(&file_path)?; |
285 | 281 | ||
@@ -297,9 +293,8 @@ impl ProjectWorkspace { | |||
297 | let mut cfg_options = CfgOptions::default(); | 293 | let mut cfg_options = CfgOptions::default(); |
298 | cfg_options.extend(target_cfgs.iter().chain(krate.cfg.iter()).cloned()); | 294 | cfg_options.extend(target_cfgs.iter().chain(krate.cfg.iter()).cloned()); |
299 | 295 | ||
300 | // FIXME: No crate name in json definition such that we cannot add OUT_DIR to env | ||
301 | Some(( | 296 | Some(( |
302 | CrateId(seq_index as u32), | 297 | crate_id, |
303 | crate_graph.add_crate_root( | 298 | crate_graph.add_crate_root( |
304 | file_id, | 299 | file_id, |
305 | krate.edition, | 300 | krate.edition, |
@@ -313,19 +308,14 @@ impl ProjectWorkspace { | |||
313 | }) | 308 | }) |
314 | .collect(); | 309 | .collect(); |
315 | 310 | ||
316 | for (id, krate) in project.crates.iter().enumerate() { | 311 | for (from, krate) in project.crates() { |
317 | for dep in &krate.deps { | 312 | for dep in &krate.deps { |
318 | let from_crate_id = CrateId(id as u32); | ||
319 | let to_crate_id = dep.crate_id; | 313 | let to_crate_id = dep.crate_id; |
320 | if let (Some(&from), Some(&to)) = | 314 | if let (Some(&from), Some(&to)) = |
321 | (crates.get(&from_crate_id), crates.get(&to_crate_id)) | 315 | (crates.get(&from), crates.get(&to_crate_id)) |
322 | { | 316 | { |
323 | if crate_graph.add_dep(from, dep.name.clone(), to).is_err() { | 317 | if let Err(_) = crate_graph.add_dep(from, dep.name.clone(), to) { |
324 | log::error!( | 318 | log::error!("cyclic dependency {:?} -> {:?}", from, to_crate_id); |
325 | "cyclic dependency {:?} -> {:?}", | ||
326 | from_crate_id, | ||
327 | to_crate_id | ||
328 | ); | ||
329 | } | 319 | } |
330 | } | 320 | } |
331 | } | 321 | } |
diff --git a/crates/project_model/src/project_json.rs b/crates/project_model/src/project_json.rs index 060ea5b7d..ae14e5126 100644 --- a/crates/project_model/src/project_json.rs +++ b/crates/project_model/src/project_json.rs | |||
@@ -12,7 +12,7 @@ use crate::cfg_flag::CfgFlag; | |||
12 | /// Roots and crates that compose this Rust project. | 12 | /// Roots and crates that compose this Rust project. |
13 | #[derive(Clone, Debug, Eq, PartialEq)] | 13 | #[derive(Clone, Debug, Eq, PartialEq)] |
14 | pub struct ProjectJson { | 14 | pub struct ProjectJson { |
15 | pub(crate) crates: Vec<Crate>, | 15 | crates: Vec<Crate>, |
16 | } | 16 | } |
17 | 17 | ||
18 | /// A crate points to the root module of a crate and lists the dependencies of the crate. This is | 18 | /// A crate points to the root module of a crate and lists the dependencies of the crate. This is |
@@ -79,6 +79,12 @@ impl ProjectJson { | |||
79 | .collect::<Vec<_>>(), | 79 | .collect::<Vec<_>>(), |
80 | } | 80 | } |
81 | } | 81 | } |
82 | pub fn n_crates(&self) -> usize { | ||
83 | self.crates.len() | ||
84 | } | ||
85 | pub fn crates(&self) -> impl Iterator<Item = (CrateId, &Crate)> + '_ { | ||
86 | self.crates.iter().enumerate().map(|(idx, krate)| (CrateId(idx as u32), krate)) | ||
87 | } | ||
82 | } | 88 | } |
83 | 89 | ||
84 | #[derive(Deserialize)] | 90 | #[derive(Deserialize)] |