Compress code

This commit is contained in:
Aleksey Kladov 2020-11-17 11:50:54 +01:00
parent a6960fb3b8
commit e4927d52e2
2 changed files with 42 additions and 66 deletions

View file

@ -225,7 +225,10 @@ impl CrateGraph {
to: CrateId, to: CrateId,
) -> Result<(), CyclicDependenciesError> { ) -> Result<(), CyclicDependenciesError> {
if self.dfs_find(from, to, &mut FxHashSet::default()) { if self.dfs_find(from, to, &mut FxHashSet::default()) {
return Err(CyclicDependenciesError); return Err(CyclicDependenciesError {
from: (from, self[from].display_name.clone()),
to: (to, self[to].display_name.clone()),
});
} }
self.arena.get_mut(&from).unwrap().add_dep(name, to); self.arena.get_mut(&from).unwrap().add_dep(name, to);
Ok(()) Ok(())
@ -421,7 +424,20 @@ impl fmt::Display for ParseEditionError {
impl std::error::Error for ParseEditionError {} impl std::error::Error for ParseEditionError {}
#[derive(Debug)] #[derive(Debug)]
pub struct CyclicDependenciesError; pub struct CyclicDependenciesError {
from: (CrateId, Option<CrateDisplayName>),
to: (CrateId, Option<CrateDisplayName>),
}
impl fmt::Display for CyclicDependenciesError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let render = |(id, name): &(CrateId, Option<CrateDisplayName>)| match name {
Some(it) => format!("{}({:?})", it, id),
None => format!("{:?}", id),
};
write!(f, "cyclic deps: {} -> {}", render(&self.from), render(&self.to))
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

@ -12,8 +12,8 @@ use proc_macro_api::ProcMacroClient;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use crate::{ use crate::{
cargo_workspace, cfg_flag::CfgFlag, utf8_stdout, CargoConfig, CargoWorkspace, ProjectJson, cargo_workspace, cfg_flag::CfgFlag, sysroot::SysrootCrate, utf8_stdout, CargoConfig,
ProjectManifest, Sysroot, TargetKind, CargoWorkspace, ProjectJson, ProjectManifest, Sysroot, TargetKind,
}; };
/// `PackageRoot` describes a package root folder. /// `PackageRoot` describes a package root folder.
@ -249,18 +249,14 @@ impl ProjectWorkspace {
if let Some(&from) = crates.get(&from) { if let Some(&from) = crates.get(&from) {
if let Some((public_deps, _proc_macro)) = &sysroot_dps { if let Some((public_deps, _proc_macro)) = &sysroot_dps {
for (name, to) in public_deps.iter() { for (name, to) in public_deps.iter() {
if let Err(_) = crate_graph.add_dep(from, name.clone(), *to) { add_dep(&mut crate_graph, from, name.clone(), *to)
log::error!("cyclic dependency on {} for {:?}", name, from)
}
} }
} }
for dep in &krate.deps { for dep in &krate.deps {
let to_crate_id = dep.crate_id; let to_crate_id = dep.crate_id;
if let Some(&to) = crates.get(&to_crate_id) { if let Some(&to) = crates.get(&to_crate_id) {
if let Err(_) = crate_graph.add_dep(from, dep.name.clone(), to) { add_dep(&mut crate_graph, from, dep.name.clone(), to)
log::error!("cyclic dependency {:?} -> {:?}", from, to);
}
} }
} }
} }
@ -299,16 +295,12 @@ impl ProjectWorkspace {
} }
if cargo[tgt].is_proc_macro { if cargo[tgt].is_proc_macro {
if let Some(proc_macro) = libproc_macro { if let Some(proc_macro) = libproc_macro {
if let Err(_) = crate_graph.add_dep( add_dep(
&mut crate_graph,
crate_id, crate_id,
CrateName::new("proc_macro").unwrap(), CrateName::new("proc_macro").unwrap(),
proc_macro, proc_macro,
) { );
log::error!(
"cyclic dependency on proc_macro for {}",
&cargo[pkg].name
)
}
} }
} }
@ -323,21 +315,12 @@ impl ProjectWorkspace {
// cargo metadata does not do any normalization, // cargo metadata does not do any normalization,
// so we do it ourselves currently // so we do it ourselves currently
let name = CrateName::normalize_dashes(&name); let name = CrateName::normalize_dashes(&name);
if to != from && crate_graph.add_dep(from, name, to).is_err() { if to != from {
log::error!( add_dep(&mut crate_graph, from, name, to);
"cyclic dependency between targets of {}",
&cargo[pkg].name
)
} }
} }
for (name, krate) in public_deps.iter() { for (name, krate) in public_deps.iter() {
if let Err(_) = crate_graph.add_dep(from, name.clone(), *krate) { add_dep(&mut crate_graph, from, name.clone(), *krate);
log::error!(
"cyclic dependency on {} for {}",
name,
&cargo[pkg].name
)
}
} }
} }
} }
@ -349,13 +332,7 @@ impl ProjectWorkspace {
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 pkg_crates.get(&pkg).into_iter().flatten() { for &from in pkg_crates.get(&pkg).into_iter().flatten() {
if let Err(_) = crate_graph.add_dep(from, name.clone(), to) { add_dep(&mut crate_graph, from, name.clone(), to)
log::error!(
"cyclic dependency {} -> {}",
&cargo[pkg].name,
&cargo[dep.pkg].name
)
}
} }
} }
} }
@ -391,15 +368,7 @@ impl ProjectWorkspace {
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 the core / std / alloc for rustc
for (name, krate) in public_deps.iter() { for (name, krate) in public_deps.iter() {
if let Err(_) = add_dep(&mut crate_graph, crate_id, name.clone(), *krate);
crate_graph.add_dep(crate_id, name.clone(), *krate)
{
log::error!(
"cyclic dependency on {} for {}",
name,
&cargo[pkg].name
)
}
} }
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);
} }
@ -412,13 +381,7 @@ impl ProjectWorkspace {
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() {
if let Err(_) = crate_graph.add_dep(from, name.clone(), to) { add_dep(&mut crate_graph, from, name.clone(), to);
log::error!(
"cyclic dependency {} -> {}",
&rustc_workspace[pkg].name,
&rustc_workspace[dep.pkg].name
)
}
} }
} }
} }
@ -434,13 +397,7 @@ impl ProjectWorkspace {
continue; continue;
} }
for &from in pkg_crates.get(&pkg).into_iter().flatten() { for &from in pkg_crates.get(&pkg).into_iter().flatten() {
if let Err(_) = crate_graph.add_dep(from, name.clone(), to) { add_dep(&mut crate_graph, from, name.clone(), to);
log::error!(
"cyclic dependency {} -> {}",
&cargo[pkg].name,
&rustc_workspace[dep].name
)
}
} }
} }
} }
@ -511,19 +468,18 @@ fn sysroot_to_crate_graph(
) -> (Vec<(CrateName, CrateId)>, Option<CrateId>) { ) -> (Vec<(CrateName, CrateId)>, Option<CrateId>) {
let mut cfg_options = CfgOptions::default(); let mut cfg_options = CfgOptions::default();
cfg_options.extend(get_rustc_cfg_options(target)); cfg_options.extend(get_rustc_cfg_options(target));
let sysroot_crates: FxHashMap<_, _> = sysroot let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = sysroot
.crates() .crates()
.filter_map(|krate| { .filter_map(|krate| {
let file_id = load(&sysroot[krate].root)?; let file_id = load(&sysroot[krate].root)?;
let env = Env::default(); let env = Env::default();
let proc_macro = vec![]; let proc_macro = vec![];
let name = CrateName::new(&sysroot[krate].name) let display_name = CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
.expect("Sysroot crates' names do not contain dashes");
let crate_id = crate_graph.add_crate_root( let crate_id = crate_graph.add_crate_root(
file_id, file_id,
Edition::Edition2018, Edition::Edition2018,
Some(name.into()), Some(display_name),
cfg_options.clone(), cfg_options.clone(),
env, env,
proc_macro, proc_macro,
@ -536,9 +492,7 @@ fn sysroot_to_crate_graph(
for &to in sysroot[from].deps.iter() { for &to in sysroot[from].deps.iter() {
let name = CrateName::new(&sysroot[to].name).unwrap(); let name = CrateName::new(&sysroot[to].name).unwrap();
if let (Some(&from), Some(&to)) = (sysroot_crates.get(&from), sysroot_crates.get(&to)) { if let (Some(&from), Some(&to)) = (sysroot_crates.get(&from), sysroot_crates.get(&to)) {
if let Err(_) = crate_graph.add_dep(from, name, to) { add_dep(crate_graph, from, name, to);
log::error!("cyclic dependency between sysroot crates")
}
} }
} }
} }
@ -579,3 +533,9 @@ fn get_rustc_cfg_options(target: Option<&str>) -> Vec<CfgFlag> {
res res
} }
fn add_dep(graph: &mut CrateGraph, from: CrateId, name: CrateName, to: CrateId) {
if let Err(err) = graph.add_dep(from, name, to) {
log::error!("{}", err)
}
}