4951: Don't panic on crates depending on themselves r=matklad a=flodiebold

Fixes #3883.

Co-authored-by: Florian Diebold <florian.diebold@freiheit.com>
This commit is contained in:
bors[bot] 2020-06-19 21:28:32 +00:00 committed by GitHub
commit af0dcc5481
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 6 deletions

View file

@ -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();

View file

@ -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(),