Skip to content

Commit 128de40

Browse files
committed
fix bug in DepthFirstSearch where start node was not visited
This could cause us to visit the start node twice.
1 parent fc02f0f commit 128de40

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

compiler/rustc_data_structures/src/graph/iterate/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ where
8484
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
8585
{
8686
pub fn new(graph: &'graph G, start_node: G::Node) -> Self {
87-
Self { graph, stack: vec![start_node], visited: BitSet::new_empty(graph.num_nodes()) }
87+
let mut visited = BitSet::new_empty(graph.num_nodes());
88+
visited.insert(start_node);
89+
Self { graph, stack: vec![start_node], visited }
90+
}
8891
}
8992
}
9093

compiler/rustc_data_structures/src/graph/iterate/tests.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ fn is_cyclic() {
2020
assert!(!is_cyclic(&diamond_acyclic));
2121
assert!(is_cyclic(&diamond_cyclic));
2222
}
23+
24+
#[test]
25+
fn dfs() {
26+
let graph = TestGraph::new(0, &[(0, 1), (0, 2), (1, 3), (2, 3), (3, 0)]);
27+
28+
let result: Vec<usize> = DepthFirstSearch::new(&graph, 0).collect();
29+
assert_eq!(result, vec![0, 2, 3, 1]);
30+
}

0 commit comments

Comments
 (0)