From 584bddef0cc9412b5ab25bb998713d59b5c9f7ef Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 19 Jun 2020 18:02:54 +0200 Subject: Don't panic on crates depending on themselves Fixes #3883. --- crates/ra_db/src/input.rs | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index bf26048f2..e6af99035 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(); -- cgit v1.2.3