From 584bddef0cc9412b5ab25bb998713d59b5c9f7ef Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 19 Jun 2020 18:02:54 +0200 Subject: [PATCH] Don't panic on crates depending on themselves Fixes #3883. --- crates/ra_db/src/input.rs | 35 ++++++++++++++++++---- crates/ra_hir_def/src/nameres/collector.rs | 2 +- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index bf26048f2a..e6af990355 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -254,12 +254,12 @@ impl CrateGraph { return false; } + if target == from { + return true; + } + for dep in &self[from].dependencies { let crate_id = dep.crate_id; - if crate_id == target { - return true; - } - if self.dfs_find(target, crate_id, visited) { return true; } @@ -369,7 +369,7 @@ mod tests { use super::{CfgOptions, CrateGraph, CrateName, Dependency, Edition::Edition2018, Env, FileId}; #[test] - fn it_should_panic_because_of_cycle_dependencies() { + fn detect_cyclic_dependency_indirect() { let mut graph = CrateGraph::default(); let crate1 = graph.add_crate_root( FileId(1u32), @@ -403,6 +403,31 @@ mod tests { assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err()); } + #[test] + fn detect_cyclic_dependency_direct() { + let mut graph = CrateGraph::default(); + let crate1 = graph.add_crate_root( + FileId(1u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + Default::default(), + Default::default(), + ); + let crate2 = graph.add_crate_root( + FileId(2u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + Default::default(), + Default::default(), + ); + assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); + assert!(graph.add_dep(crate2, CrateName::new("crate2").unwrap(), crate2).is_err()); + } + #[test] fn it_works() { let mut graph = CrateGraph::default(); diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 77baa4c696..b8f6aac8f2 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -36,8 +36,8 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> Cr // populate external prelude for dep in &crate_graph[def_map.krate].dependencies { - let dep_def_map = db.crate_def_map(dep.crate_id); log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); + let dep_def_map = db.crate_def_map(dep.crate_id); def_map.extern_prelude.insert( dep.as_name(), ModuleId { krate: dep.crate_id, local_id: dep_def_map.root }.into(),