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

18 files changed

Lines changed: 89 additions & 193 deletions

File tree

‎compiler/rustc_borrowck/src/constraints/graph.rs‎

Lines changed: 2 additions & 11 deletions
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::DirectedGraphforRegionGraph<'s,'tcx,D>{
218218
typeNode = RegionVid;
219-
}
220219

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

227-
impl<'s,'tcx,D:ConstraintGraphDirection> graph::WithSuccessorsforRegionGraph<'s,'tcx,D>{
228-
fnsuccessors(&self,node:Self::Node) -> <Selfas graph::GraphSuccessors<'_>>::Iter{
225+
impl<'s,'tcx,D:ConstraintGraphDirection> graph::SuccessorsforRegionGraph<'s,'tcx,D>{
226+
fnsuccessors(&self,node:Self::Node) -> implIterator<Item = Self::Node>{
229227
self.outgoing_regions(node)
230228
}
231229
}
232-
233-
impl<'s,'tcx,D:ConstraintGraphDirection> graph::GraphSuccessors<'_>
234-
forRegionGraph<'s,'tcx,D>
235-
{
236-
typeItem = RegionVid;
237-
typeIter = Successors<'s,'tcx,D>;
238-
}

‎compiler/rustc_borrowck/src/dataflow.rs‎

Lines changed: 2 additions & 2 deletions
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 inself.regioncx.region_graph().depth_first_search(issuing_region){
265+
for successor ingraph::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‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
usecrate::constraints::ConstraintSccIndex;
22
usecrate::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
) -> implIterator<Item = RegionVid> + 'a{
2525
letmut 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‎

Lines changed: 4 additions & 2 deletions
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‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
usesuper::{DirectedGraph,WithNumNodes,WithStartNode,WithSuccessors};
1+
usesuper::{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-
pubfnpost_order_from<G:DirectedGraph + WithSuccessors + WithNumNodes>(
9+
pubfnpost_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-
pubfnpost_order_from_to<G:DirectedGraph + WithSuccessors + WithNumNodes>(
16+
pubfnpost_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-
fnpost_order_walk<G:DirectedGraph + WithSuccessors + WithNumNodes>(
30+
fnpost_order_walk<G:DirectedGraph + Successors>(
3131
graph:&G,
3232
node:G::Node,
3333
result:&mutVec<G::Node>,
@@ -60,7 +60,7 @@ fn post_order_walk<G: DirectedGraph + WithSuccessors + WithNumNodes>(
6060
}
6161
}
6262

63-
pubfnreverse_post_order<G:DirectedGraph + WithSuccessors + WithNumNodes>(
63+
pubfnreverse_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
pubstructDepthFirstSearch<'graph,G>
7474
where
75-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
75+
G: ?Sized + DirectedGraph + Successors,
7676
{
7777
graph:&'graphG,
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
pubfnnew(graph:&'graphG) -> Self{
8787
Self{ graph,stack:vec![],visited:BitSet::new_empty(graph.num_nodes())}
@@ -127,7 +127,7 @@ where
127127

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

141141
impl<G>IteratorforDepthFirstSearch<'_,G>
142142
where
143-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
143+
G: ?Sized + DirectedGraph + Successors,
144144
{
145145
typeItem = G::Node;
146146

@@ -201,7 +201,7 @@ struct Event<N> {
201201
/// [CLR]: https://en.wikipedia.org/wiki/Introduction_to_Algorithms
202202
pubstructTriColorDepthFirstSearch<'graph,G>
203203
where
204-
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
204+
G: ?Sized + DirectedGraph + Successors,
205205
{
206206
graph:&'graphG,
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
pubfnnew(graph:&'graphG) -> 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‎

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,43 @@ mod tests;
1212

1313
pubtraitDirectedGraph{
1414
typeNode:Idx;
15-
}
1615

17-
pubtraitWithNumNodes:DirectedGraph{
1816
fnnum_nodes(&self) -> usize;
1917
}
2018

21-
pubtraitWithNumEdges:DirectedGraph{
19+
pubtraitNumEdges:DirectedGraph{
2220
fnnum_edges(&self) -> usize;
2321
}
2422

25-
pubtraitWithSuccessors:DirectedGraph
26-
where
27-
Self:for<'graph>GraphSuccessors<'graph,Item = <SelfasDirectedGraph>::Node>,
28-
{
29-
fnsuccessors(&self,node:Self::Node) -> <SelfasGraphSuccessors<'_>>::Iter;
30-
31-
fndepth_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-
pubtraitGraphSuccessors<'graph>{
41-
typeItem;
42-
typeIter:Iterator<Item = Self::Item>;
43-
}
44-
45-
pubtraitWithPredecessors:DirectedGraph
46-
where
47-
Self:for<'graph>GraphPredecessors<'graph,Item = <SelfasDirectedGraph>::Node>,
48-
{
49-
fnpredecessors(&self,node:Self::Node) -> <SelfasGraphPredecessors<'_>>::Iter;
50-
}
51-
52-
#[allow(unused_lifetimes)]
53-
pubtraitGraphPredecessors<'graph>{
54-
typeItem;
55-
typeIter:Iterator<Item = Self::Item>;
56-
}
57-
58-
pubtraitWithStartNode:DirectedGraph{
23+
pubtraitStartNode:DirectedGraph{
5924
fnstart_node(&self) -> Self::Node;
6025
}
6126

62-
pubtraitControlFlowGraph:
63-
DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
64-
{
65-
// convenient trait
27+
pubtraitSuccessors:DirectedGraph{
28+
fnsuccessors(&self,node:Self::Node) -> implIterator<Item = Self::Node>;
6629
}
6730

68-
impl<T>ControlFlowGraphforTwhere
69-
T:DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
70-
{
31+
pubtraitPredecessors:DirectedGraph{
32+
fnpredecessors(&self,node:Self::Node) -> implIterator<Item = Self::Node>;
7133
}
7234

35+
/// Alias for [`DirectedGraph`] + [`StartNode`] + [`Predecessors`] + [`Successors`].
36+
pubtraitControlFlowGraph:DirectedGraph + StartNode + Predecessors + Successors{}
37+
impl<T>ControlFlowGraphforTwhereT:DirectedGraph + StartNode + Predecessors + Successors{}
38+
7339
/// Returns `true` if the graph has a cycle that is reachable from the start node.
7440
pubfnis_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+
pubfndepth_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‎

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

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

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

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

19-
impl<'graph,G:WithSuccessors>WithSuccessorsfor&'graphG{
20-
fnsuccessors(&self,node:Self::Node) -> <SelfasGraphSuccessors<'_>>::Iter{
17+
impl<'graph,G:Successors>Successorsfor&'graphG{
18+
fnsuccessors(&self,node:Self::Node) -> implIterator<Item = Self::Node>{
2119
(**self).successors(node)
2220
}
2321
}
2422

25-
impl<'graph,G:WithPredecessors>WithPredecessorsfor&'graphG{
26-
fnpredecessors(&self,node:Self::Node) -> <SelfasGraphPredecessors<'_>>::Iter{
23+
impl<'graph,G:Predecessors>Predecessorsfor&'graphG{
24+
fnpredecessors(&self,node:Self::Node) -> implIterator<Item = Self::Node>{
2725
(**self).predecessors(node)
2826
}
2927
}
30-
31-
impl<'iter,'graph,G:WithPredecessors>GraphPredecessors<'iter>for&'graphG{
32-
typeItem = G::Node;
33-
typeIter = <GasGraphPredecessors<'iter>>::Iter;
34-
}
35-
36-
impl<'iter,'graph,G:WithSuccessors>GraphSuccessors<'iter>for&'graphG{
37-
typeItem = G::Node;
38-
typeIter = <GasGraphSuccessors<'iter>>::Iter;
39-
}

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
usecrate::fx::FxHashSet;
99
usecrate::graph::vec_graph::VecGraph;
10-
usecrate::graph::{DirectedGraph,GraphSuccessors,WithNumEdges,WithNumNodes,WithSuccessors};
10+
usecrate::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-
pubfnnew(graph:&(implDirectedGraph<Node = N> + WithNumNodes + WithSuccessors)) -> Self{
42+
pubfnnew(graph:&(implDirectedGraph<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>DirectedGraphforSccs<N,S>{
92+
impl<N:Idx,S:Idx + Ord>DirectedGraphforSccs<N,S>{
9393
typeNode = S;
94-
}
9594

96-
impl<N:Idx,S:Idx + Ord>WithNumNodesforSccs<N,S>{
9795
fnnum_nodes(&self) -> usize{
9896
self.num_sccs()
9997
}
10098
}
10199

102-
impl<N:Idx,S:Idx>WithNumEdgesforSccs<N,S>{
100+
impl<N:Idx,S:Idx + Ord>NumEdgesforSccs<N,S>{
103101
fnnum_edges(&self) -> usize{
104102
self.scc_data.all_successors.len()
105103
}
106104
}
107105

108-
impl<'graph,N:Idx,S:Idx>GraphSuccessors<'graph>forSccs<N,S>{
109-
typeItem = S;
110-
111-
typeIter = std::iter::Cloned<std::slice::Iter<'graph,S>>;
112-
}
113-
114-
impl<N:Idx,S:Idx + Ord>WithSuccessorsforSccs<N,S>{
115-
fnsuccessors(&self,node:S) -> <SelfasGraphSuccessors<'_>>::Iter{
106+
impl<N:Idx,S:Idx + Ord>SuccessorsforSccs<N,S>{
107+
fnsuccessors(&self,node:S) -> implIterator<Item = Self::Node>{
116108
self.successors(node).iter().cloned()
117109
}
118110
}
@@ -158,7 +150,7 @@ impl<S: Idx> SccData<S> {
158150
}
159151
}
160152

161-
structSccsConstruction<'c,G:DirectedGraph + WithNumNodes + WithSuccessors,S:Idx>{
153+
structSccsConstruction<'c,G:DirectedGraph + Successors,S:Idx>{
162154
graph:&'cG,
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)