mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Extract the large nested block into a function
Also add some more detailed comments Extract into function deleted the previous comments
This commit is contained in:
parent
9246df669a
commit
419b5a1bee
@ -376,6 +376,7 @@ fn cargo_to_crate_graph(
|
|||||||
cfg_options.insert_atom("debug_assertions".into());
|
cfg_options.insert_atom("debug_assertions".into());
|
||||||
|
|
||||||
let mut pkg_crates = FxHashMap::default();
|
let mut pkg_crates = FxHashMap::default();
|
||||||
|
// Does any crate signal to rust-analyzer that they need the rustc_private crates?
|
||||||
let mut has_private = false;
|
let mut has_private = false;
|
||||||
// Next, create crates for each package, target pair
|
// Next, create crates for each package, target pair
|
||||||
for pkg in cargo.packages() {
|
for pkg in cargo.packages() {
|
||||||
@ -440,22 +441,53 @@ fn cargo_to_crate_graph(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut rustc_pkg_crates = FxHashMap::default();
|
|
||||||
|
|
||||||
if has_private {
|
if has_private {
|
||||||
// If the user provided a path to rustc sources, we add all the rustc_private crates
|
// If the user provided a path to rustc sources, we add all the rustc_private crates
|
||||||
// and create dependencies on them for the crates which opt-in to that
|
// and create dependencies on them for the crates which opt-in to that
|
||||||
if let Some(rustc_workspace) = rustc {
|
if let Some(rustc_workspace) = rustc {
|
||||||
// rustc-dev crates start from 'rustc_driver'
|
handle_rustc_crates(
|
||||||
// We want to collect all crates which are transitive dependencies of rustc_driver
|
rustc_workspace,
|
||||||
if let Some(root_pkg) = rustc_workspace
|
load,
|
||||||
.packages()
|
&mut crate_graph,
|
||||||
.find(|package| rustc_workspace[*package].name == "rustc_driver")
|
rustc_build_data_map,
|
||||||
{
|
&cfg_options,
|
||||||
|
proc_macro_loader,
|
||||||
|
&mut pkg_to_lib_crate,
|
||||||
|
&public_deps,
|
||||||
|
cargo,
|
||||||
|
&pkg_crates,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
crate_graph
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_rustc_crates(
|
||||||
|
rustc_workspace: &CargoWorkspace,
|
||||||
|
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||||
|
crate_graph: &mut CrateGraph,
|
||||||
|
rustc_build_data_map: Option<&FxHashMap<String, BuildData>>,
|
||||||
|
cfg_options: &CfgOptions,
|
||||||
|
proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
|
||||||
|
pkg_to_lib_crate: &mut FxHashMap<la_arena::Idx<crate::PackageData>, CrateId>,
|
||||||
|
public_deps: &[(CrateName, CrateId)],
|
||||||
|
cargo: &CargoWorkspace,
|
||||||
|
pkg_crates: &FxHashMap<la_arena::Idx<crate::PackageData>, Vec<CrateId>>,
|
||||||
|
) {
|
||||||
|
let mut rustc_pkg_crates = FxHashMap::default();
|
||||||
|
// The root package of the rustc-dev component is rustc_driver, so we match that
|
||||||
|
let root_pkg =
|
||||||
|
rustc_workspace.packages().find(|package| rustc_workspace[*package].name == "rustc_driver");
|
||||||
|
// The rustc workspace might be incomplete (such as if rustc-dev is not installed for the current toolchain)
|
||||||
|
// and `rustcSource` is set to discover.
|
||||||
|
if let Some(root_pkg) = root_pkg {
|
||||||
|
// Iterate through every crate in the dependency subtree of rustc_driver using BFS
|
||||||
let mut queue = VecDeque::new();
|
let mut queue = VecDeque::new();
|
||||||
queue.push_back(root_pkg);
|
queue.push_back(root_pkg);
|
||||||
while let Some(pkg) = queue.pop_front() {
|
while let Some(pkg) = queue.pop_front() {
|
||||||
// Don't duplicate packages
|
// Don't duplicate packages if they are dependended on a diamond pattern
|
||||||
|
// N.B. if this line is ommitted, we try and analyse either 48_000 or 480_000 crates
|
||||||
|
// neither of which makes
|
||||||
if rustc_pkg_crates.contains_key(&pkg) {
|
if rustc_pkg_crates.contains_key(&pkg) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -468,18 +500,17 @@ fn cargo_to_crate_graph(
|
|||||||
}
|
}
|
||||||
if let Some(file_id) = load(&rustc_workspace[tgt].root) {
|
if let Some(file_id) = load(&rustc_workspace[tgt].root) {
|
||||||
let crate_id = add_target_crate_root(
|
let crate_id = add_target_crate_root(
|
||||||
&mut crate_graph,
|
crate_graph,
|
||||||
&rustc_workspace[pkg],
|
&rustc_workspace[pkg],
|
||||||
rustc_build_data_map
|
rustc_build_data_map.and_then(|it| it.get(&rustc_workspace[pkg].id)),
|
||||||
.and_then(|it| it.get(&rustc_workspace[pkg].id)),
|
|
||||||
&cfg_options,
|
&cfg_options,
|
||||||
proc_macro_loader,
|
proc_macro_loader,
|
||||||
file_id,
|
file_id,
|
||||||
);
|
);
|
||||||
pkg_to_lib_crate.insert(pkg, crate_id);
|
pkg_to_lib_crate.insert(pkg, crate_id);
|
||||||
// Add dependencies on the core / std / alloc for rustc
|
// Add dependencies on core / std / alloc for this crate
|
||||||
for (name, krate) in public_deps.iter() {
|
for (name, krate) in public_deps.iter() {
|
||||||
add_dep(&mut crate_graph, crate_id, name.clone(), *krate);
|
add_dep(crate_graph, crate_id, name.clone(), *krate);
|
||||||
}
|
}
|
||||||
rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
|
rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
|
||||||
}
|
}
|
||||||
@ -493,13 +524,13 @@ fn cargo_to_crate_graph(
|
|||||||
let name = CrateName::new(&dep.name).unwrap();
|
let name = CrateName::new(&dep.name).unwrap();
|
||||||
if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
|
if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
|
||||||
for &from in rustc_pkg_crates.get(&pkg).into_iter().flatten() {
|
for &from in rustc_pkg_crates.get(&pkg).into_iter().flatten() {
|
||||||
add_dep(&mut crate_graph, from, name.clone(), to);
|
add_dep(crate_graph, from, name.clone(), to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Add a dependency on the rustc_private crates for all targets of each package
|
||||||
// Add dependencies for all crates which opt in to rustc_private libraries
|
// which opts in
|
||||||
for dep in rustc_workspace.packages() {
|
for dep in rustc_workspace.packages() {
|
||||||
let name = CrateName::normalize_dashes(&rustc_workspace[dep].name);
|
let name = CrateName::normalize_dashes(&rustc_workspace[dep].name);
|
||||||
|
|
||||||
@ -511,21 +542,16 @@ fn cargo_to_crate_graph(
|
|||||||
}
|
}
|
||||||
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
|
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
|
||||||
// Avoid creating duplicate dependencies
|
// Avoid creating duplicate dependencies
|
||||||
|
// This avoids the situation where `from` depends on e.g. `arrayvec`, but
|
||||||
|
// `rust_analyzer` thinks that it should use the one from the `rustcSource`
|
||||||
|
// instead of the one from `crates.io`
|
||||||
if !crate_graph[from].dependencies.iter().any(|d| d.name == name) {
|
if !crate_graph[from].dependencies.iter().any(|d| d.name == name) {
|
||||||
add_dep(&mut crate_graph, from, name.clone(), to);
|
add_dep(crate_graph, from, name.clone(), to);
|
||||||
} else {
|
|
||||||
eprintln!(
|
|
||||||
"Skipped {} for {:?}",
|
|
||||||
&name, &crate_graph[from].display_name
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
crate_graph
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_target_crate_root(
|
fn add_target_crate_root(
|
||||||
|
Loading…
Reference in New Issue
Block a user