Skip to content

Commit e8d2221

Browse files
committed
Make depth_first_search into a standalone function
Does not necessarily change much, but we never overwrite it, so I see no reason for it to be in the `Successors` trait. (+we already have a similar `is_cyclic`)
1 parent 3124fa9 commit e8d2221

File tree

6 files changed

+21
-17
lines changed

6 files changed

+21
-17
lines changed

compiler/rustc_borrowck/src/dataflow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_data_structures::fx::FxIndexMap;
2-
use rustc_data_structures::graph::Successors;
2+
use rustc_data_structures::graph;
33
use rustc_index::bit_set::BitSet;
44
use rustc_middle::mir::{
55
self, BasicBlock, Body, CallReturnPlaces, Location, Place, TerminatorEdges,
@@ -262,7 +262,7 @@ impl<'tcx> PoloniusOutOfScopePrecomputer<'_, 'tcx> {
262262

263263
// We first handle the cases where the loan doesn't go out of scope, depending on the issuing
264264
// region's successors.
265-
for successor in self.regioncx.region_graph().depth_first_search(issuing_region) {
265+
for successor in graph::depth_first_search(&self.regioncx.region_graph(), issuing_region) {
266266
// 1. Via applied member constraints
267267
//
268268
// The issuing region can flow into the choice regions, and they are either:

compiler/rustc_borrowck/src/region_infer/reverse_sccs.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::constraints::ConstraintSccIndex;
22
use crate::RegionInferenceContext;
33
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
4+
use rustc_data_structures::graph;
45
use rustc_data_structures::graph::vec_graph::VecGraph;
5-
use rustc_data_structures::graph::Successors;
66
use rustc_middle::ty::RegionVid;
77
use std::ops::Range;
88

@@ -23,8 +23,7 @@ impl ReverseSccGraph {
2323
scc0: ConstraintSccIndex,
2424
) -> impl Iterator<Item = RegionVid> + 'a {
2525
let mut duplicates = FxIndexSet::default();
26-
self.graph
27-
.depth_first_search(scc0)
26+
graph::depth_first_search(&self.graph, scc0)
2827
.flat_map(move |scc1| {
2928
self.scc_regions
3029
.get(&scc1)

compiler/rustc_borrowck/src/type_check/liveness/trace.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2-
use rustc_data_structures::graph::Successors;
32
use rustc_index::bit_set::BitSet;
43
use rustc_index::interval::IntervalSet;
54
use rustc_infer::infer::canonical::QueryRegionConstraints;
@@ -64,7 +63,10 @@ pub(super) fn trace<'mir, 'tcx>(
6463
// Traverse each issuing region's constraints, and record the loan as flowing into the
6564
// outlived region.
6665
for (loan, issuing_region_data) in borrow_set.iter_enumerated() {
67-
for succ in region_graph.depth_first_search(issuing_region_data.region) {
66+
for succ in rustc_data_structures::graph::depth_first_search(
67+
&region_graph,
68+
issuing_region_data.region,
69+
) {
6870
// We don't need to mention that a loan flows into its issuing region.
6971
if succ == issuing_region_data.region {
7072
continue;

compiler/rustc_data_structures/src/graph/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ pub trait Successors: DirectedGraph {
3030
Self: 'g;
3131

3232
fn successors(&self, node: Self::Node) -> Self::Successors<'_>;
33-
34-
fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self> {
35-
iterate::DepthFirstSearch::new(self).with_start_node(from)
36-
}
3733
}
3834

3935
pub trait Predecessors: DirectedGraph {
@@ -57,3 +53,10 @@ where
5753
.run_from_start(&mut iterate::CycleDetector)
5854
.is_some()
5955
}
56+
57+
pub fn depth_first_search<G>(graph: &G, from: G::Node) -> iterate::DepthFirstSearch<'_, G>
58+
where
59+
G: ?Sized + Successors,
60+
{
61+
iterate::DepthFirstSearch::new(graph).with_start_node(from)
62+
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::graph;
2+
13
use super::*;
24

35
fn create_graph() -> VecGraph<usize> {
@@ -37,6 +39,6 @@ fn successors() {
3739
#[test]
3840
fn dfs() {
3941
let graph = create_graph();
40-
let dfs: Vec<_> = graph.depth_first_search(0).collect();
42+
let dfs: Vec<_> = graph::depth_first_search(&graph, 0).collect();
4143
assert_eq!(dfs, vec![0, 1, 3, 4, 2]);
4244
}

compiler/rustc_hir_typeck/src/fallback.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::FnCtxt;
22
use rustc_data_structures::{
3-
graph::Successors,
4-
graph::{iterate::DepthFirstSearch, vec_graph::VecGraph},
3+
graph::{self, iterate::DepthFirstSearch, vec_graph::VecGraph},
54
unord::{UnordBag, UnordMap, UnordSet},
65
};
76
use rustc_infer::infer::{DefineOpaqueTypes, InferOk};
@@ -300,7 +299,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
300299
debug!(
301300
"calculate_diverging_fallback: root_vid={:?} reaches {:?}",
302301
root_vid,
303-
coercion_graph.depth_first_search(root_vid).collect::<Vec<_>>()
302+
graph::depth_first_search(&coercion_graph, root_vid).collect::<Vec<_>>()
304303
);
305304

306305
// drain the iterator to visit all nodes reachable from this node
@@ -342,8 +341,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
342341
for &diverging_vid in &diverging_vids {
343342
let diverging_ty = Ty::new_var(self.tcx, diverging_vid);
344343
let root_vid = self.root_var(diverging_vid);
345-
let can_reach_non_diverging = coercion_graph
346-
.depth_first_search(root_vid)
344+
let can_reach_non_diverging = graph::depth_first_search(&coercion_graph, root_vid)
347345
.any(|n| roots_reachable_from_non_diverging.visited(n));
348346

349347
let infer_var_infos: UnordBag<_> = self

0 commit comments

Comments
 (0)