aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model
diff options
context:
space:
mode:
Diffstat (limited to 'crates/project_model')
-rw-r--r--crates/project_model/src/workspace.rs88
1 files changed, 24 insertions, 64 deletions
diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs
index 90ab5e7a9..dbf1dc5bf 100644
--- a/crates/project_model/src/workspace.rs
+++ b/crates/project_model/src/workspace.rs
@@ -12,8 +12,8 @@ use proc_macro_api::ProcMacroClient;
12use rustc_hash::{FxHashMap, FxHashSet}; 12use rustc_hash::{FxHashMap, FxHashSet};
13 13
14use crate::{ 14use crate::{
15 cargo_workspace, cfg_flag::CfgFlag, utf8_stdout, CargoConfig, CargoWorkspace, ProjectJson, 15 cargo_workspace, cfg_flag::CfgFlag, sysroot::SysrootCrate, utf8_stdout, CargoConfig,
16 ProjectManifest, Sysroot, TargetKind, 16 CargoWorkspace, ProjectJson, ProjectManifest, Sysroot, TargetKind,
17}; 17};
18 18
19/// `PackageRoot` describes a package root folder. 19/// `PackageRoot` describes a package root folder.
@@ -249,18 +249,14 @@ impl ProjectWorkspace {
249 if let Some(&from) = crates.get(&from) { 249 if let Some(&from) = crates.get(&from) {
250 if let Some((public_deps, _proc_macro)) = &sysroot_dps { 250 if let Some((public_deps, _proc_macro)) = &sysroot_dps {
251 for (name, to) in public_deps.iter() { 251 for (name, to) in public_deps.iter() {
252 if let Err(_) = crate_graph.add_dep(from, name.clone(), *to) { 252 add_dep(&mut crate_graph, from, name.clone(), *to)
253 log::error!("cyclic dependency on {} for {:?}", name, from)
254 }
255 } 253 }
256 } 254 }
257 255
258 for dep in &krate.deps { 256 for dep in &krate.deps {
259 let to_crate_id = dep.crate_id; 257 let to_crate_id = dep.crate_id;
260 if let Some(&to) = crates.get(&to_crate_id) { 258 if let Some(&to) = crates.get(&to_crate_id) {
261 if let Err(_) = crate_graph.add_dep(from, dep.name.clone(), to) { 259 add_dep(&mut crate_graph, from, dep.name.clone(), to)
262 log::error!("cyclic dependency {:?} -> {:?}", from, to);
263 }
264 } 260 }
265 } 261 }
266 } 262 }
@@ -299,16 +295,12 @@ impl ProjectWorkspace {
299 } 295 }
300 if cargo[tgt].is_proc_macro { 296 if cargo[tgt].is_proc_macro {
301 if let Some(proc_macro) = libproc_macro { 297 if let Some(proc_macro) = libproc_macro {
302 if let Err(_) = crate_graph.add_dep( 298 add_dep(
299 &mut crate_graph,
303 crate_id, 300 crate_id,
304 CrateName::new("proc_macro").unwrap(), 301 CrateName::new("proc_macro").unwrap(),
305 proc_macro, 302 proc_macro,
306 ) { 303 );
307 log::error!(
308 "cyclic dependency on proc_macro for {}",
309 &cargo[pkg].name
310 )
311 }
312 } 304 }
313 } 305 }
314 306
@@ -323,21 +315,12 @@ impl ProjectWorkspace {
323 // cargo metadata does not do any normalization, 315 // cargo metadata does not do any normalization,
324 // so we do it ourselves currently 316 // so we do it ourselves currently
325 let name = CrateName::normalize_dashes(&name); 317 let name = CrateName::normalize_dashes(&name);
326 if to != from && crate_graph.add_dep(from, name, to).is_err() { 318 if to != from {
327 log::error!( 319 add_dep(&mut crate_graph, from, name, to);
328 "cyclic dependency between targets of {}",
329 &cargo[pkg].name
330 )
331 } 320 }
332 } 321 }
333 for (name, krate) in public_deps.iter() { 322 for (name, krate) in public_deps.iter() {
334 if let Err(_) = crate_graph.add_dep(from, name.clone(), *krate) { 323 add_dep(&mut crate_graph, from, name.clone(), *krate);
335 log::error!(
336 "cyclic dependency on {} for {}",
337 name,
338 &cargo[pkg].name
339 )
340 }
341 } 324 }
342 } 325 }
343 } 326 }
@@ -349,13 +332,7 @@ impl ProjectWorkspace {
349 let name = CrateName::new(&dep.name).unwrap(); 332 let name = CrateName::new(&dep.name).unwrap();
350 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) { 333 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
351 for &from in pkg_crates.get(&pkg).into_iter().flatten() { 334 for &from in pkg_crates.get(&pkg).into_iter().flatten() {
352 if let Err(_) = crate_graph.add_dep(from, name.clone(), to) { 335 add_dep(&mut crate_graph, from, name.clone(), to)
353 log::error!(
354 "cyclic dependency {} -> {}",
355 &cargo[pkg].name,
356 &cargo[dep.pkg].name
357 )
358 }
359 } 336 }
360 } 337 }
361 } 338 }
@@ -391,15 +368,7 @@ impl ProjectWorkspace {
391 pkg_to_lib_crate.insert(pkg, crate_id); 368 pkg_to_lib_crate.insert(pkg, crate_id);
392 // Add dependencies on the core / std / alloc for rustc 369 // Add dependencies on the core / std / alloc for rustc
393 for (name, krate) in public_deps.iter() { 370 for (name, krate) in public_deps.iter() {
394 if let Err(_) = 371 add_dep(&mut crate_graph, crate_id, name.clone(), *krate);
395 crate_graph.add_dep(crate_id, name.clone(), *krate)
396 {
397 log::error!(
398 "cyclic dependency on {} for {}",
399 name,
400 &cargo[pkg].name
401 )
402 }
403 } 372 }
404 rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id); 373 rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
405 } 374 }
@@ -412,13 +381,7 @@ impl ProjectWorkspace {
412 let name = CrateName::new(&dep.name).unwrap(); 381 let name = CrateName::new(&dep.name).unwrap();
413 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) { 382 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
414 for &from in rustc_pkg_crates.get(&pkg).into_iter().flatten() { 383 for &from in rustc_pkg_crates.get(&pkg).into_iter().flatten() {
415 if let Err(_) = crate_graph.add_dep(from, name.clone(), to) { 384 add_dep(&mut crate_graph, from, name.clone(), to);
416 log::error!(
417 "cyclic dependency {} -> {}",
418 &rustc_workspace[pkg].name,
419 &rustc_workspace[dep.pkg].name
420 )
421 }
422 } 385 }
423 } 386 }
424 } 387 }
@@ -434,13 +397,7 @@ impl ProjectWorkspace {
434 continue; 397 continue;
435 } 398 }
436 for &from in pkg_crates.get(&pkg).into_iter().flatten() { 399 for &from in pkg_crates.get(&pkg).into_iter().flatten() {
437 if let Err(_) = crate_graph.add_dep(from, name.clone(), to) { 400 add_dep(&mut crate_graph, from, name.clone(), to);
438 log::error!(
439 "cyclic dependency {} -> {}",
440 &cargo[pkg].name,
441 &rustc_workspace[dep].name
442 )
443 }
444 } 401 }
445 } 402 }
446 } 403 }
@@ -511,19 +468,18 @@ fn sysroot_to_crate_graph(
511) -> (Vec<(CrateName, CrateId)>, Option<CrateId>) { 468) -> (Vec<(CrateName, CrateId)>, Option<CrateId>) {
512 let mut cfg_options = CfgOptions::default(); 469 let mut cfg_options = CfgOptions::default();
513 cfg_options.extend(get_rustc_cfg_options(target)); 470 cfg_options.extend(get_rustc_cfg_options(target));
514 let sysroot_crates: FxHashMap<_, _> = sysroot 471 let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = sysroot
515 .crates() 472 .crates()
516 .filter_map(|krate| { 473 .filter_map(|krate| {
517 let file_id = load(&sysroot[krate].root)?; 474 let file_id = load(&sysroot[krate].root)?;
518 475
519 let env = Env::default(); 476 let env = Env::default();
520 let proc_macro = vec![]; 477 let proc_macro = vec![];
521 let name = CrateName::new(&sysroot[krate].name) 478 let display_name = CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
522 .expect("Sysroot crates' names do not contain dashes");
523 let crate_id = crate_graph.add_crate_root( 479 let crate_id = crate_graph.add_crate_root(
524 file_id, 480 file_id,
525 Edition::Edition2018, 481 Edition::Edition2018,
526 Some(name.into()), 482 Some(display_name),
527 cfg_options.clone(), 483 cfg_options.clone(),
528 env, 484 env,
529 proc_macro, 485 proc_macro,
@@ -536,9 +492,7 @@ fn sysroot_to_crate_graph(
536 for &to in sysroot[from].deps.iter() { 492 for &to in sysroot[from].deps.iter() {
537 let name = CrateName::new(&sysroot[to].name).unwrap(); 493 let name = CrateName::new(&sysroot[to].name).unwrap();
538 if let (Some(&from), Some(&to)) = (sysroot_crates.get(&from), sysroot_crates.get(&to)) { 494 if let (Some(&from), Some(&to)) = (sysroot_crates.get(&from), sysroot_crates.get(&to)) {
539 if let Err(_) = crate_graph.add_dep(from, name, to) { 495 add_dep(crate_graph, from, name, to);
540 log::error!("cyclic dependency between sysroot crates")
541 }
542 } 496 }
543 } 497 }
544 } 498 }
@@ -579,3 +533,9 @@ fn get_rustc_cfg_options(target: Option<&str>) -> Vec<CfgFlag> {
579 533
580 res 534 res
581} 535}
536
537fn add_dep(graph: &mut CrateGraph, from: CrateId, name: CrateName, to: CrateId) {
538 if let Err(err) = graph.add_dep(from, name, to) {
539 log::error!("{}", err)
540 }
541}