Skip to content

Commit 5580ae9

Browse files
authored
Rollup merge of #123934 - WaffleLapkin:graph-mini-refactor, r=fmease
`rustc_data_structures::graph` mini refactor Who doesn't love to breathe dust from the ancient times?
2 parents b79d0b0 + 435db9b commit 5580ae9

File tree

18 files changed

+89
-193
lines changed

18 files changed

+89
-193
lines changed

compiler/rustc_borrowck/src/constraints/graph.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,14 @@ impl<'s, 'tcx, D: ConstraintGraphDirection> Iterator for Successors<'s, 'tcx, D>
216216

217217
impl<'s, 'tcx, D: ConstraintGraphDirection> graph::DirectedGraph for RegionGraph<'s, 'tcx, D> {
218218
type Node = RegionVid;
219-
}
220219

221-
impl<'s, 'tcx, D: ConstraintGraphDirection> graph::WithNumNodes for RegionGraph<'s, 'tcx, D> {
222220
fn num_nodes(&self) -> usize {
223221
self.constraint_graph.first_constraints.len()
224222
}
225223
}
226224

227-
impl<'s, 'tcx, D: ConstraintGraphDirection> graph::WithSuccessors for RegionGraph<'s, 'tcx, D> {
228-
fn successors(&self, node: Self::Node) -> <Self as graph::GraphSuccessors<'_>>::Iter {
225+
impl<'s, 'tcx, D: ConstraintGraphDirection> graph::Successors for RegionGraph<'s, 'tcx, D> {
226+
fn successors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
229227
self.outgoing_regions(node)
230228
}
231229
}
232-
233-
impl<'s, 'tcx, D: ConstraintGraphDirection> graph::GraphSuccessors<'_>
234-
for RegionGraph<'s, 'tcx, D>
235-
{
236-
type Item = RegionVid;
237-
type Iter = Successors<'s, 'tcx, D>;
238-
}

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::WithSuccessors;
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::WithSuccessors;
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::WithSuccessors;
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/iterate/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
use super::{DirectedGraph, WithNumNodes, WithStartNode, WithSuccessors};
1+
use super::{DirectedGraph, StartNode, Successors};
22
use rustc_index::bit_set::BitSet;
33
use rustc_index::{IndexSlice, IndexVec};
44
use std::ops::ControlFlow;
55

66
#[cfg(test)]
77
mod tests;
88

9-
pub fn post_order_from<G: DirectedGraph + WithSuccessors + WithNumNodes>(
9+
pub fn post_order_from<G: DirectedGraph + Successors>(
1010
graph: &G,
1111
start_node: G::Node,
1212
) -> Vec<G::Node> {
1313
post_order_from_to(graph, start_node, None)
1414
}
1515

16-
pub fn post_order_from_to<G: DirectedGraph + WithSuccessors + WithNumNodes>(
16+
pub fn post_order_from_to<G: DirectedGraph + Successors>(
1717
graph: &G,
1818
start_node: G::Node,
1919
end_node: Option<G::Node>,
@@ -27,7 +27,7 @@ pub fn post_order_from_to<G: DirectedGraph + WithSuccessors + WithNumNodes>(
2727
result
2828
}
2929

30-
fn post_order_walk<G: DirectedGraph + WithSuccessors + WithNumNodes>(
30+
fn post_order_walk<G: DirectedGraph + Successors>(
3131
graph: &G,
3232
node: G::Node,
3333
result: &mut Vec<G::Node>,
@@ -60,7 +60,7 @@ fn post_order_walk<G: DirectedGraph + WithSuccessors + WithNumNodes>(
6060
}
6161
}
6262

63-
pub fn reverse_post_order<G: DirectedGraph + WithSuccessors + WithNumNodes>(
63+
pub fn reverse_post_order<G: DirectedGraph + Successors>(
6464
graph: &G,
6565
start_node: G::Node,
6666
) -> Vec<G::Node> {
@@ -72,7 +72,7 @@ pub fn reverse_post_order<G: DirectedGraph + WithSuccessors + WithNumNodes>(
7272
/// A "depth-first search" iterator for a directed graph.
7373
pub struct DepthFirstSearch<'graph, G>
7474
where
75-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
75+
G: ?Sized + DirectedGraph + Successors,
7676
{
7777
graph: &'graph G,
7878
stack: Vec<G::Node>,
@@ -81,7 +81,7 @@ where
8181

8282
impl<'graph, G> DepthFirstSearch<'graph, G>
8383
where
84-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
84+
G: ?Sized + DirectedGraph + Successors,
8585
{
8686
pub fn new(graph: &'graph G) -> Self {
8787
Self { graph, stack: vec![], visited: BitSet::new_empty(graph.num_nodes()) }
@@ -127,7 +127,7 @@ where
127127

128128
impl<G> std::fmt::Debug for DepthFirstSearch<'_, G>
129129
where
130-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
130+
G: ?Sized + DirectedGraph + Successors,
131131
{
132132
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133133
let mut f = fmt.debug_set();
@@ -140,7 +140,7 @@ where
140140

141141
impl<G> Iterator for DepthFirstSearch<'_, G>
142142
where
143-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
143+
G: ?Sized + DirectedGraph + Successors,
144144
{
145145
type Item = G::Node;
146146

@@ -201,7 +201,7 @@ struct Event<N> {
201201
/// [CLR]: https://en.wikipedia.org/wiki/Introduction_to_Algorithms
202202
pub struct TriColorDepthFirstSearch<'graph, G>
203203
where
204-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
204+
G: ?Sized + DirectedGraph + Successors,
205205
{
206206
graph: &'graph G,
207207
stack: Vec<Event<G::Node>>,
@@ -211,7 +211,7 @@ where
211211

212212
impl<'graph, G> TriColorDepthFirstSearch<'graph, G>
213213
where
214-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
214+
G: ?Sized + DirectedGraph + Successors,
215215
{
216216
pub fn new(graph: &'graph G) -> Self {
217217
TriColorDepthFirstSearch {
@@ -278,7 +278,7 @@ where
278278

279279
impl<G> TriColorDepthFirstSearch<'_, G>
280280
where
281-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors + WithStartNode,
281+
G: ?Sized + DirectedGraph + Successors + StartNode,
282282
{
283283
/// Performs a depth-first search, starting from `G::start_node()`.
284284
///

compiler/rustc_data_structures/src/graph/mod.rs

+18-45
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,43 @@ mod tests;
1212

1313
pub trait DirectedGraph {
1414
type Node: Idx;
15-
}
1615

17-
pub trait WithNumNodes: DirectedGraph {
1816
fn num_nodes(&self) -> usize;
1917
}
2018

21-
pub trait WithNumEdges: DirectedGraph {
19+
pub trait NumEdges: DirectedGraph {
2220
fn num_edges(&self) -> usize;
2321
}
2422

25-
pub trait WithSuccessors: DirectedGraph
26-
where
27-
Self: for<'graph> GraphSuccessors<'graph, Item = <Self as DirectedGraph>::Node>,
28-
{
29-
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter;
30-
31-
fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self>
32-
where
33-
Self: WithNumNodes,
34-
{
35-
iterate::DepthFirstSearch::new(self).with_start_node(from)
36-
}
37-
}
38-
39-
#[allow(unused_lifetimes)]
40-
pub trait GraphSuccessors<'graph> {
41-
type Item;
42-
type Iter: Iterator<Item = Self::Item>;
43-
}
44-
45-
pub trait WithPredecessors: DirectedGraph
46-
where
47-
Self: for<'graph> GraphPredecessors<'graph, Item = <Self as DirectedGraph>::Node>,
48-
{
49-
fn predecessors(&self, node: Self::Node) -> <Self as GraphPredecessors<'_>>::Iter;
50-
}
51-
52-
#[allow(unused_lifetimes)]
53-
pub trait GraphPredecessors<'graph> {
54-
type Item;
55-
type Iter: Iterator<Item = Self::Item>;
56-
}
57-
58-
pub trait WithStartNode: DirectedGraph {
23+
pub trait StartNode: DirectedGraph {
5924
fn start_node(&self) -> Self::Node;
6025
}
6126

62-
pub trait ControlFlowGraph:
63-
DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
64-
{
65-
// convenient trait
27+
pub trait Successors: DirectedGraph {
28+
fn successors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node>;
6629
}
6730

68-
impl<T> ControlFlowGraph for T where
69-
T: DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
70-
{
31+
pub trait Predecessors: DirectedGraph {
32+
fn predecessors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node>;
7133
}
7234

35+
/// Alias for [`DirectedGraph`] + [`StartNode`] + [`Predecessors`] + [`Successors`].
36+
pub trait ControlFlowGraph: DirectedGraph + StartNode + Predecessors + Successors {}
37+
impl<T> ControlFlowGraph for T where T: DirectedGraph + StartNode + Predecessors + Successors {}
38+
7339
/// Returns `true` if the graph has a cycle that is reachable from the start node.
7440
pub fn is_cyclic<G>(graph: &G) -> bool
7541
where
76-
G: ?Sized + DirectedGraph + WithStartNode + WithSuccessors + WithNumNodes,
42+
G: ?Sized + DirectedGraph + StartNode + Successors,
7743
{
7844
iterate::TriColorDepthFirstSearch::new(graph)
7945
.run_from_start(&mut iterate::CycleDetector)
8046
.is_some()
8147
}
48+
49+
pub fn depth_first_search<G>(graph: &G, from: G::Node) -> iterate::DepthFirstSearch<'_, G>
50+
where
51+
G: ?Sized + Successors,
52+
{
53+
iterate::DepthFirstSearch::new(graph).with_start_node(from)
54+
}

compiler/rustc_data_structures/src/graph/reference.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,26 @@ use super::*;
22

33
impl<'graph, G: DirectedGraph> DirectedGraph for &'graph G {
44
type Node = G::Node;
5-
}
65

7-
impl<'graph, G: WithNumNodes> WithNumNodes for &'graph G {
86
fn num_nodes(&self) -> usize {
97
(**self).num_nodes()
108
}
119
}
1210

13-
impl<'graph, G: WithStartNode> WithStartNode for &'graph G {
11+
impl<'graph, G: StartNode> StartNode for &'graph G {
1412
fn start_node(&self) -> Self::Node {
1513
(**self).start_node()
1614
}
1715
}
1816

19-
impl<'graph, G: WithSuccessors> WithSuccessors for &'graph G {
20-
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter {
17+
impl<'graph, G: Successors> Successors for &'graph G {
18+
fn successors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
2119
(**self).successors(node)
2220
}
2321
}
2422

25-
impl<'graph, G: WithPredecessors> WithPredecessors for &'graph G {
26-
fn predecessors(&self, node: Self::Node) -> <Self as GraphPredecessors<'_>>::Iter {
23+
impl<'graph, G: Predecessors> Predecessors for &'graph G {
24+
fn predecessors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
2725
(**self).predecessors(node)
2826
}
2927
}
30-
31-
impl<'iter, 'graph, G: WithPredecessors> GraphPredecessors<'iter> for &'graph G {
32-
type Item = G::Node;
33-
type Iter = <G as GraphPredecessors<'iter>>::Iter;
34-
}
35-
36-
impl<'iter, 'graph, G: WithSuccessors> GraphSuccessors<'iter> for &'graph G {
37-
type Item = G::Node;
38-
type Iter = <G as GraphSuccessors<'iter>>::Iter;
39-
}

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

+8-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use crate::fx::FxHashSet;
99
use crate::graph::vec_graph::VecGraph;
10-
use crate::graph::{DirectedGraph, GraphSuccessors, WithNumEdges, WithNumNodes, WithSuccessors};
10+
use crate::graph::{DirectedGraph, NumEdges, Successors};
1111
use rustc_index::{Idx, IndexSlice, IndexVec};
1212
use std::ops::Range;
1313

@@ -39,7 +39,7 @@ pub struct SccData<S: Idx> {
3939
}
4040

4141
impl<N: Idx, S: Idx + Ord> Sccs<N, S> {
42-
pub fn new(graph: &(impl DirectedGraph<Node = N> + WithNumNodes + WithSuccessors)) -> Self {
42+
pub fn new(graph: &(impl DirectedGraph<Node = N> + Successors)) -> Self {
4343
SccsConstruction::construct(graph)
4444
}
4545

@@ -89,30 +89,22 @@ impl<N: Idx, S: Idx + Ord> Sccs<N, S> {
8989
}
9090
}
9191

92-
impl<N: Idx, S: Idx> DirectedGraph for Sccs<N, S> {
92+
impl<N: Idx, S: Idx + Ord> DirectedGraph for Sccs<N, S> {
9393
type Node = S;
94-
}
9594

96-
impl<N: Idx, S: Idx + Ord> WithNumNodes for Sccs<N, S> {
9795
fn num_nodes(&self) -> usize {
9896
self.num_sccs()
9997
}
10098
}
10199

102-
impl<N: Idx, S: Idx> WithNumEdges for Sccs<N, S> {
100+
impl<N: Idx, S: Idx + Ord> NumEdges for Sccs<N, S> {
103101
fn num_edges(&self) -> usize {
104102
self.scc_data.all_successors.len()
105103
}
106104
}
107105

108-
impl<'graph, N: Idx, S: Idx> GraphSuccessors<'graph> for Sccs<N, S> {
109-
type Item = S;
110-
111-
type Iter = std::iter::Cloned<std::slice::Iter<'graph, S>>;
112-
}
113-
114-
impl<N: Idx, S: Idx + Ord> WithSuccessors for Sccs<N, S> {
115-
fn successors(&self, node: S) -> <Self as GraphSuccessors<'_>>::Iter {
106+
impl<N: Idx, S: Idx + Ord> Successors for Sccs<N, S> {
107+
fn successors(&self, node: S) -> impl Iterator<Item = Self::Node> {
116108
self.successors(node).iter().cloned()
117109
}
118110
}
@@ -158,7 +150,7 @@ impl<S: Idx> SccData<S> {
158150
}
159151
}
160152

161-
struct SccsConstruction<'c, G: DirectedGraph + WithNumNodes + WithSuccessors, S: Idx> {
153+
struct SccsConstruction<'c, G: DirectedGraph + Successors, S: Idx> {
162154
graph: &'c G,
163155

164156
/// The state of each node; used during walk to record the stack
@@ -218,7 +210,7 @@ enum WalkReturn<S> {
218210

219211
impl<'c, G, S> SccsConstruction<'c, G, S>
220212
where
221-
G: DirectedGraph + WithNumNodes + WithSuccessors,
213+
G: DirectedGraph + Successors,
222214
S: Idx,
223215
{
224216
/// Identifies SCCs in the graph `G` and computes the resulting

0 commit comments

Comments
 (0)