Update crate graph to only use subcrates of rustc_driver

This commit is contained in:
Daniel McNab 2021-03-07 10:18:01 +00:00
parent 7513867aa2
commit b46605cfcd

View File

@ -2,11 +2,7 @@
//! metadata` or `rust-project.json`) into representation stored in the salsa
//! database -- `CrateGraph`.
use std::{
fmt, fs,
path::{Component, Path},
process::Command,
};
use std::{collections::VecDeque, fmt, fs, path::Path, process::Command};
use anyhow::{Context, Result};
use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId, ProcMacro};
@ -446,22 +442,24 @@ fn cargo_to_crate_graph(
let mut rustc_pkg_crates = FxHashMap::default();
// If the user provided a path to rustc sources, we add all the rustc_private crates
// and create dependencies on them for the crates in the current workspace
// and create dependencies on them for the crates which opt-in to that
if let Some(rustc_workspace) = rustc {
for pkg in rustc_workspace.packages() {
// rustc-dev crates start from 'rustc_driver'
// Therefore, we collect all crates which are transitive dependencies of rustc_driver
if let Some(root_pkg) = rustc_workspace
.packages()
.find(|package| rustc_workspace[*package].name == "rustc_driver")
{
let mut queue = VecDeque::new();
queue.push_back(root_pkg);
while let Some(pkg) = queue.pop_front() {
for dep in &rustc_workspace[pkg].dependencies {
queue.push_back(dep.pkg);
}
for &tgt in rustc_workspace[pkg].targets.iter() {
if rustc_workspace[tgt].kind != TargetKind::Lib {
continue;
}
// Exclude alloc / core / std
if rustc_workspace[tgt]
.root
.components()
.any(|c| c == Component::Normal("library".as_ref()))
{
continue;
}
if let Some(file_id) = load(&rustc_workspace[tgt].root) {
let crate_id = add_target_crate_root(
&mut crate_graph,
@ -480,6 +478,7 @@ fn cargo_to_crate_graph(
}
}
}
}
// Now add a dep edge from all targets of upstream to the lib
// target of downstream.
for pkg in rustc_workspace.packages() {
@ -493,7 +492,7 @@ fn cargo_to_crate_graph(
}
}
// Add dependencies for all the crates of the current workspace to rustc_private libraries
// Add dependencies for all crates which opt in to rustc_private libraries
for dep in rustc_workspace.packages() {
let name = CrateName::normalize_dashes(&rustc_workspace[dep].name);
@ -507,13 +506,14 @@ fn cargo_to_crate_graph(
continue;
}
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
// Avoid creating duplicate dependencies
if !crate_graph[from].dependencies.iter().any(|d| d.name == name) {
add_dep(&mut crate_graph, from, name.clone(), to);
} else {
// eprintln!(
// "Skipped {} for {:?}",
// &name, &crate_graph[from].display_name
// );
eprintln!(
"Skipped {} for {:?}",
&name, &crate_graph[from].display_name
);
}
}
}