Skip to content

Commit 398da59

Browse files
committed
Merge WithNumNodes into DirectedGraph
1 parent 029cb1b commit 398da59

File tree

13 files changed

+33
-52
lines changed

13 files changed

+33
-52
lines changed

compiler/rustc_borrowck/src/constraints/graph.rs

-2
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ 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
}

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, WithStartNode, WithSuccessors};
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 + WithSuccessors>(
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 + WithSuccessors>(
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 + WithSuccessors>(
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 + WithSuccessors>(
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 + WithSuccessors,
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 + WithSuccessors,
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 + WithSuccessors,
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 + WithSuccessors,
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 + WithSuccessors,
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 + WithSuccessors,
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 + WithSuccessors + WithStartNode,
282282
{
283283
/// Performs a depth-first search, starting from `G::start_node()`.
284284
///

compiler/rustc_data_structures/src/graph/mod.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ 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

@@ -28,10 +26,7 @@ where
2826
{
2927
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter;
3028

31-
fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self>
32-
where
33-
Self: WithNumNodes,
34-
{
29+
fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self> {
3530
iterate::DepthFirstSearch::new(self).with_start_node(from)
3631
}
3732
}
@@ -60,20 +55,20 @@ pub trait WithStartNode: DirectedGraph {
6055
}
6156

6257
pub trait ControlFlowGraph:
63-
DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
58+
DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors
6459
{
6560
// convenient trait
6661
}
6762

6863
impl<T> ControlFlowGraph for T where
69-
T: DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
64+
T: DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors
7065
{
7166
}
7267

7368
/// Returns `true` if the graph has a cycle that is reachable from the start node.
7469
pub fn is_cyclic<G>(graph: &G) -> bool
7570
where
76-
G: ?Sized + DirectedGraph + WithStartNode + WithSuccessors + WithNumNodes,
71+
G: ?Sized + DirectedGraph + WithStartNode + WithSuccessors,
7772
{
7873
iterate::TriColorDepthFirstSearch::new(graph)
7974
.run_from_start(&mut iterate::CycleDetector)

compiler/rustc_data_structures/src/graph/reference.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ 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
}

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

+6-8
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, GraphSuccessors, WithNumEdges, WithSuccessors};
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> + WithSuccessors)) -> Self {
4343
SccsConstruction::construct(graph)
4444
}
4545

@@ -89,17 +89,15 @@ 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> WithNumEdges for Sccs<N, S> {
103101
fn num_edges(&self) -> usize {
104102
self.scc_data.all_successors.len()
105103
}
@@ -158,7 +156,7 @@ impl<S: Idx> SccData<S> {
158156
}
159157
}
160158

161-
struct SccsConstruction<'c, G: DirectedGraph + WithNumNodes + WithSuccessors, S: Idx> {
159+
struct SccsConstruction<'c, G: DirectedGraph + WithSuccessors, S: Idx> {
162160
graph: &'c G,
163161

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

219217
impl<'c, G, S> SccsConstruction<'c, G, S>
220218
where
221-
G: DirectedGraph + WithNumNodes + WithSuccessors,
219+
G: DirectedGraph + WithSuccessors,
222220
S: Idx,
223221
{
224222
/// Identifies SCCs in the graph `G` and computes the resulting

compiler/rustc_data_structures/src/graph/tests.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ impl TestGraph {
3636

3737
impl DirectedGraph for TestGraph {
3838
type Node = usize;
39+
40+
fn num_nodes(&self) -> usize {
41+
self.num_nodes
42+
}
3943
}
4044

4145
impl WithStartNode for TestGraph {
@@ -44,12 +48,6 @@ impl WithStartNode for TestGraph {
4448
}
4549
}
4650

47-
impl WithNumNodes for TestGraph {
48-
fn num_nodes(&self) -> usize {
49-
self.num_nodes
50-
}
51-
}
52-
5351
impl WithPredecessors for TestGraph {
5452
fn predecessors(&self, node: usize) -> <Self as GraphPredecessors<'_>>::Iter {
5553
self.predecessors[&node].iter().cloned()

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

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

44
#[cfg(test)]
@@ -80,9 +80,7 @@ impl<N: Idx + Ord> VecGraph<N> {
8080

8181
impl<N: Idx> DirectedGraph for VecGraph<N> {
8282
type Node = N;
83-
}
8483

85-
impl<N: Idx> WithNumNodes for VecGraph<N> {
8684
fn num_nodes(&self) -> usize {
8785
self.node_starts.len() - 1
8886
}

compiler/rustc_middle/src/mir/basic_blocks.rs

-2
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ impl<'tcx> std::ops::Deref for BasicBlocks<'tcx> {
141141

142142
impl<'tcx> graph::DirectedGraph for BasicBlocks<'tcx> {
143143
type Node = BasicBlock;
144-
}
145144

146-
impl<'tcx> graph::WithNumNodes for BasicBlocks<'tcx> {
147145
#[inline]
148146
fn num_nodes(&self) -> usize {
149147
self.basic_blocks.len()

compiler/rustc_middle/src/mir/generic_graphviz.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::{self, Write};
55

66
pub struct GraphvizWriter<
77
'a,
8-
G: graph::DirectedGraph + graph::WithSuccessors + graph::WithStartNode + graph::WithNumNodes,
8+
G: graph::DirectedGraph + graph::WithSuccessors + graph::WithStartNode,
99
NodeContentFn: Fn(<G as graph::DirectedGraph>::Node) -> Vec<String>,
1010
EdgeLabelsFn: Fn(<G as graph::DirectedGraph>::Node) -> Vec<String>,
1111
> {
@@ -19,7 +19,7 @@ pub struct GraphvizWriter<
1919

2020
impl<
2121
'a,
22-
G: graph::DirectedGraph + graph::WithSuccessors + graph::WithStartNode + graph::WithNumNodes,
22+
G: graph::DirectedGraph + graph::WithSuccessors + graph::WithStartNode,
2323
NodeContentFn: Fn(<G as graph::DirectedGraph>::Node) -> Vec<String>,
2424
EdgeLabelsFn: Fn(<G as graph::DirectedGraph>::Node) -> Vec<String>,
2525
> GraphvizWriter<'a, G, NodeContentFn, EdgeLabelsFn>

compiler/rustc_mir_transform/src/coverage/counters.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::{self, Debug};
22

33
use rustc_data_structures::captures::Captures;
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_data_structures::graph::WithNumNodes;
5+
use rustc_data_structures::graph::DirectedGraph;
66
use rustc_index::IndexVec;
77
use rustc_middle::mir::coverage::{CounterId, CovTerm, Expression, ExpressionId, Op};
88

compiler/rustc_mir_transform/src/coverage/graph.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_data_structures::captures::Captures;
22
use rustc_data_structures::fx::FxHashSet;
33
use rustc_data_structures::graph::dominators::{self, Dominators};
4-
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes, WithStartNode};
4+
use rustc_data_structures::graph::{self, DirectedGraph, GraphSuccessors, WithStartNode};
55
use rustc_index::bit_set::BitSet;
66
use rustc_index::IndexVec;
77
use rustc_middle::mir::{self, BasicBlock, Terminator, TerminatorKind};
@@ -193,9 +193,7 @@ impl IndexMut<BasicCoverageBlock> for CoverageGraph {
193193

194194
impl graph::DirectedGraph for CoverageGraph {
195195
type Node = BasicCoverageBlock;
196-
}
197196

198-
impl graph::WithNumNodes for CoverageGraph {
199197
#[inline]
200198
fn num_nodes(&self) -> usize {
201199
self.bcbs.len()

compiler/rustc_mir_transform/src/coverage/spans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_data_structures::graph::WithNumNodes;
1+
use rustc_data_structures::graph::DirectedGraph;
22
use rustc_index::bit_set::BitSet;
33
use rustc_middle::mir;
44
use rustc_span::{BytePos, Span};

compiler/rustc_mir_transform/src/coverage/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use super::counters;
2828
use super::graph::{self, BasicCoverageBlock};
2929

3030
use itertools::Itertools;
31-
use rustc_data_structures::graph::WithNumNodes;
3231
use rustc_data_structures::graph::WithSuccessors;
32+
use rustc_data_structures::graph::DirectedGraph;
3333
use rustc_index::{Idx, IndexVec};
3434
use rustc_middle::mir::*;
3535
use rustc_middle::ty;

0 commit comments

Comments
 (0)