Skip to content

Commit

Permalink
Don't panic on crates depending on themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
flodiebold committed Jun 19, 2020
1 parent 90a5c46 commit b9f04da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
33 changes: 29 additions & 4 deletions crates/ra_db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -403,6 +403,31 @@ mod tests {
assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err());
}

#[test]
fn it_should_panic_because_of_direct_cycle() {
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();
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_hir_def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit b9f04da

Please sign in to comment.