Skip to content

Commit 0d5fc9b

Browse files
committed
Merge {With,Graph}{Successors,Predecessors} into {Successors,Predecessors}
Now with GAT!
1 parent 398da59 commit 0d5fc9b

File tree

15 files changed

+78
-133
lines changed

15 files changed

+78
-133
lines changed

compiler/rustc_borrowck/src/constraints/graph.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,10 @@ impl<'s, 'tcx, D: ConstraintGraphDirection> graph::DirectedGraph for RegionGraph
222222
}
223223
}
224224

225-
impl<'s, 'tcx, D: ConstraintGraphDirection> graph::WithSuccessors for RegionGraph<'s, 'tcx, D> {
226-
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+
type Successors<'g> = Successors<'s, 'tcx, D> where Self: 'g;
227+
228+
fn successors(&self, node: Self::Node) -> Self::Successors<'_> {
227229
self.outgoing_regions(node)
228230
}
229231
}
230-
231-
impl<'s, 'tcx, D: ConstraintGraphDirection> graph::GraphSuccessors<'_>
232-
for RegionGraph<'s, 'tcx, D>
233-
{
234-
type Item = RegionVid;
235-
type Iter = Successors<'s, 'tcx, D>;
236-
}

compiler/rustc_borrowck/src/dataflow.rs

+1-1
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::Successors;
33
use rustc_index::bit_set::BitSet;
44
use rustc_middle::mir::{
55
self, BasicBlock, Body, CallReturnPlaces, Location, Place, TerminatorEdges,

compiler/rustc_borrowck/src/region_infer/reverse_sccs.rs

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2-
use rustc_data_structures::graph::WithSuccessors;
2+
use rustc_data_structures::graph::Successors;
33
use rustc_index::bit_set::BitSet;
44
use rustc_index::interval::IntervalSet;
55
use rustc_infer::infer::canonical::QueryRegionConstraints;

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, WithStartNode, WithSuccessors};
1+
use super::{DirectedGraph, Successors, WithStartNode};
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>(
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>(
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>(
2727
result
2828
}
2929

30-
fn post_order_walk<G: DirectedGraph + WithSuccessors>(
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>(
6060
}
6161
}
6262

63-
pub fn reverse_post_order<G: DirectedGraph + WithSuccessors>(
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>(
7272
/// A "depth-first search" iterator for a directed graph.
7373
pub struct DepthFirstSearch<'graph, G>
7474
where
75-
G: ?Sized + DirectedGraph + 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 + 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 + 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 + 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 + 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 + 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 + WithSuccessors + WithStartNode,
281+
G: ?Sized + DirectedGraph + Successors + WithStartNode,
282282
{
283283
/// Performs a depth-first search, starting from `G::start_node()`.
284284
///

compiler/rustc_data_structures/src/graph/mod.rs

+14-29
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,40 @@ pub trait WithNumEdges: DirectedGraph {
2020
fn num_edges(&self) -> usize;
2121
}
2222

23-
pub trait WithSuccessors: DirectedGraph
24-
where
25-
Self: for<'graph> GraphSuccessors<'graph, Item = <Self as DirectedGraph>::Node>,
26-
{
27-
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter;
23+
pub trait Successors: DirectedGraph {
24+
type Successors<'g>: Iterator<Item = Self::Node>
25+
where
26+
Self: 'g;
27+
28+
fn successors(&self, node: Self::Node) -> Self::Successors<'_>;
2829

2930
fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self> {
3031
iterate::DepthFirstSearch::new(self).with_start_node(from)
3132
}
3233
}
3334

34-
#[allow(unused_lifetimes)]
35-
pub trait GraphSuccessors<'graph> {
36-
type Item;
37-
type Iter: Iterator<Item = Self::Item>;
38-
}
35+
pub trait Predecessors: DirectedGraph {
36+
type Predecessors<'g>: Iterator<Item = Self::Node>
37+
where
38+
Self: 'g;
3939

40-
pub trait WithPredecessors: DirectedGraph
41-
where
42-
Self: for<'graph> GraphPredecessors<'graph, Item = <Self as DirectedGraph>::Node>,
43-
{
44-
fn predecessors(&self, node: Self::Node) -> <Self as GraphPredecessors<'_>>::Iter;
45-
}
46-
47-
#[allow(unused_lifetimes)]
48-
pub trait GraphPredecessors<'graph> {
49-
type Item;
50-
type Iter: Iterator<Item = Self::Item>;
40+
fn predecessors(&self, node: Self::Node) -> Self::Predecessors<'_>;
5141
}
5242

5343
pub trait WithStartNode: DirectedGraph {
5444
fn start_node(&self) -> Self::Node;
5545
}
5646

57-
pub trait ControlFlowGraph:
58-
DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors
59-
{
47+
pub trait ControlFlowGraph: DirectedGraph + WithStartNode + Predecessors + Successors {
6048
// convenient trait
6149
}
6250

63-
impl<T> ControlFlowGraph for T where
64-
T: DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors
65-
{
66-
}
51+
impl<T> ControlFlowGraph for T where T: DirectedGraph + WithStartNode + Predecessors + Successors {}
6752

6853
/// Returns `true` if the graph has a cycle that is reachable from the start node.
6954
pub fn is_cyclic<G>(graph: &G) -> bool
7055
where
71-
G: ?Sized + DirectedGraph + WithStartNode + WithSuccessors,
56+
G: ?Sized + DirectedGraph + WithStartNode + Successors,
7257
{
7358
iterate::TriColorDepthFirstSearch::new(graph)
7459
.run_from_start(&mut iterate::CycleDetector)

compiler/rustc_data_structures/src/graph/reference.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,18 @@ impl<'graph, G: WithStartNode> WithStartNode for &'graph G {
1414
}
1515
}
1616

17-
impl<'graph, G: WithSuccessors> WithSuccessors for &'graph G {
18-
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter {
17+
impl<'graph, G: Successors> Successors for &'graph G {
18+
type Successors<'g> = G::Successors<'g> where 'graph: 'g;
19+
20+
fn successors(&self, node: Self::Node) -> Self::Successors<'_> {
1921
(**self).successors(node)
2022
}
2123
}
2224

23-
impl<'graph, G: WithPredecessors> WithPredecessors for &'graph G {
24-
fn predecessors(&self, node: Self::Node) -> <Self as GraphPredecessors<'_>>::Iter {
25+
impl<'graph, G: Predecessors> Predecessors for &'graph G {
26+
type Predecessors<'g> = G::Predecessors<'g> where 'graph: 'g;
27+
28+
fn predecessors(&self, node: Self::Node) -> Self::Predecessors<'_> {
2529
(**self).predecessors(node)
2630
}
2731
}
28-
29-
impl<'iter, 'graph, G: WithPredecessors> GraphPredecessors<'iter> for &'graph G {
30-
type Item = G::Node;
31-
type Iter = <G as GraphPredecessors<'iter>>::Iter;
32-
}
33-
34-
impl<'iter, 'graph, G: WithSuccessors> GraphSuccessors<'iter> for &'graph G {
35-
type Item = G::Node;
36-
type Iter = <G as GraphSuccessors<'iter>>::Iter;
37-
}

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

+7-11
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, WithSuccessors};
10+
use crate::graph::{DirectedGraph, Successors, WithNumEdges};
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> + WithSuccessors)) -> Self {
42+
pub fn new(graph: &(impl DirectedGraph<Node = N> + Successors)) -> Self {
4343
SccsConstruction::construct(graph)
4444
}
4545

@@ -103,14 +103,10 @@ impl<N: Idx, S: Idx + Ord> WithNumEdges for Sccs<N, S> {
103103
}
104104
}
105105

106-
impl<'graph, N: Idx, S: Idx> GraphSuccessors<'graph> for Sccs<N, S> {
107-
type Item = S;
106+
impl<N: Idx, S: Idx + Ord> Successors for Sccs<N, S> {
107+
type Successors<'g> = std::iter::Cloned<std::slice::Iter<'g, S>>;
108108

109-
type Iter = std::iter::Cloned<std::slice::Iter<'graph, S>>;
110-
}
111-
112-
impl<N: Idx, S: Idx + Ord> WithSuccessors for Sccs<N, S> {
113-
fn successors(&self, node: S) -> <Self as GraphSuccessors<'_>>::Iter {
109+
fn successors(&self, node: S) -> Self::Successors<'_> {
114110
self.successors(node).iter().cloned()
115111
}
116112
}
@@ -156,7 +152,7 @@ impl<S: Idx> SccData<S> {
156152
}
157153
}
158154

159-
struct SccsConstruction<'c, G: DirectedGraph + WithSuccessors, S: Idx> {
155+
struct SccsConstruction<'c, G: DirectedGraph + Successors, S: Idx> {
160156
graph: &'c G,
161157

162158
/// The state of each node; used during walk to record the stack
@@ -216,7 +212,7 @@ enum WalkReturn<S> {
216212

217213
impl<'c, G, S> SccsConstruction<'c, G, S>
218214
where
219-
G: DirectedGraph + WithSuccessors,
215+
G: DirectedGraph + Successors,
220216
S: Idx,
221217
{
222218
/// Identifies SCCs in the graph `G` and computes the resulting

compiler/rustc_data_structures/src/graph/tests.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,18 @@ impl WithStartNode for TestGraph {
4848
}
4949
}
5050

51-
impl WithPredecessors for TestGraph {
52-
fn predecessors(&self, node: usize) -> <Self as GraphPredecessors<'_>>::Iter {
51+
impl Predecessors for TestGraph {
52+
type Predecessors<'g> = iter::Cloned<slice::Iter<'g, usize>>;
53+
54+
fn predecessors(&self, node: usize) -> Self::Predecessors<'_> {
5355
self.predecessors[&node].iter().cloned()
5456
}
5557
}
5658

57-
impl WithSuccessors for TestGraph {
58-
fn successors(&self, node: usize) -> <Self as GraphSuccessors<'_>>::Iter {
59+
impl Successors for TestGraph {
60+
type Successors<'g> = iter::Cloned<slice::Iter<'g, usize>>;
61+
62+
fn successors(&self, node: usize) -> Self::Successors<'_> {
5963
self.successors[&node].iter().cloned()
6064
}
6165
}
62-
63-
impl<'graph> GraphPredecessors<'graph> for TestGraph {
64-
type Item = usize;
65-
type Iter = iter::Cloned<slice::Iter<'graph, usize>>;
66-
}
67-
68-
impl<'graph> GraphSuccessors<'graph> for TestGraph {
69-
type Item = usize;
70-
type Iter = iter::Cloned<slice::Iter<'graph, usize>>;
71-
}

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::graph::{DirectedGraph, GraphSuccessors, WithNumEdges, WithSuccessors};
1+
use crate::graph::{DirectedGraph, Successors, WithNumEdges};
22
use rustc_index::{Idx, IndexVec};
33

44
#[cfg(test)]
@@ -92,14 +92,10 @@ impl<N: Idx> WithNumEdges for VecGraph<N> {
9292
}
9393
}
9494

95-
impl<'graph, N: Idx> GraphSuccessors<'graph> for VecGraph<N> {
96-
type Item = N;
95+
impl<N: Idx + Ord> Successors for VecGraph<N> {
96+
type Successors<'g> = std::iter::Cloned<std::slice::Iter<'g, N>>;
9797

98-
type Iter = std::iter::Cloned<std::slice::Iter<'graph, N>>;
99-
}
100-
101-
impl<N: Idx + Ord> WithSuccessors for VecGraph<N> {
102-
fn successors(&self, node: N) -> <Self as GraphSuccessors<'_>>::Iter {
98+
fn successors(&self, node: N) -> Self::Successors<'_> {
10399
self.successors(node).iter().cloned()
104100
}
105101
}

compiler/rustc_hir_typeck/src/fallback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::FnCtxt;
22
use rustc_data_structures::{
3-
graph::WithSuccessors,
3+
graph::Successors,
44
graph::{iterate::DepthFirstSearch, vec_graph::VecGraph},
55
unord::{UnordBag, UnordMap, UnordSet},
66
};

0 commit comments

Comments
 (0)