@@ -254,12 +254,12 @@ impl CrateGraph {
254254 return false ;
255255 }
256256
257+ if target == from {
258+ return true ;
259+ }
260+
257261 for dep in & self [ from] . dependencies {
258262 let crate_id = dep. crate_id ;
259- if crate_id == target {
260- return true ;
261- }
262-
263263 if self . dfs_find ( target, crate_id, visited) {
264264 return true ;
265265 }
@@ -369,7 +369,7 @@ mod tests {
369369 use super :: { CfgOptions , CrateGraph , CrateName , Dependency , Edition :: Edition2018 , Env , FileId } ;
370370
371371 #[ test]
372- fn it_should_panic_because_of_cycle_dependencies ( ) {
372+ fn detect_cyclic_dependency_indirect ( ) {
373373 let mut graph = CrateGraph :: default ( ) ;
374374 let crate1 = graph. add_crate_root (
375375 FileId ( 1u32 ) ,
@@ -403,6 +403,31 @@ mod tests {
403403 assert ! ( graph. add_dep( crate3, CrateName :: new( "crate1" ) . unwrap( ) , crate1) . is_err( ) ) ;
404404 }
405405
406+ #[ test]
407+ fn detect_cyclic_dependency_direct ( ) {
408+ let mut graph = CrateGraph :: default ( ) ;
409+ let crate1 = graph. add_crate_root (
410+ FileId ( 1u32 ) ,
411+ Edition2018 ,
412+ None ,
413+ CfgOptions :: default ( ) ,
414+ Env :: default ( ) ,
415+ Default :: default ( ) ,
416+ Default :: default ( ) ,
417+ ) ;
418+ let crate2 = graph. add_crate_root (
419+ FileId ( 2u32 ) ,
420+ Edition2018 ,
421+ None ,
422+ CfgOptions :: default ( ) ,
423+ Env :: default ( ) ,
424+ Default :: default ( ) ,
425+ Default :: default ( ) ,
426+ ) ;
427+ assert ! ( graph. add_dep( crate1, CrateName :: new( "crate2" ) . unwrap( ) , crate2) . is_ok( ) ) ;
428+ assert ! ( graph. add_dep( crate2, CrateName :: new( "crate2" ) . unwrap( ) , crate2) . is_err( ) ) ;
429+ }
430+
406431 #[ test]
407432 fn it_works ( ) {
408433 let mut graph = CrateGraph :: default ( ) ;
0 commit comments