Skip to content

Commit 74e5184

Browse files
Rollup merge of #140692 - Zalathar:linked-graph, r=wesleywiser
Rename `graph::implementation::Graph` to `LinkedGraph` One of the more confusing parts of the `rustc_data_structures::graph` module is this mysteriously-named “Graph” type, which turns out to be an older standalone graph implementation that predates the traits used by the rest of the graph module. This graph type is still used in a couple of places (for reporting certain lifetime errors, and by certain debugging/test-only checks of the query dependency graph), but hasn't had much attention in years. This PR renames that old graph type from `implementation::Graph` to `linked_graph::LinkedGraph` to give it a more distinct identity (and make existing uses easier to find), and adds some notes to gently discourage any further use in new code. No functional change.
2 parents 3b82d46 + 9e7fb67 commit 74e5184

File tree

6 files changed

+39
-23
lines changed

6 files changed

+39
-23
lines changed

compiler/rustc_data_structures/src/graph/implementation/mod.rs renamed to compiler/rustc_data_structures/src/graph/linked_graph/mod.rs

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A graph module for use in dataflow, region resolution, and elsewhere.
1+
//! See [`LinkedGraph`].
22
//!
33
//! # Interface details
44
//!
@@ -28,7 +28,23 @@ use tracing::debug;
2828
#[cfg(test)]
2929
mod tests;
3030

31-
pub struct Graph<N, E> {
31+
/// A concrete graph implementation that supports:
32+
/// - Nodes and/or edges labelled with custom data types (`N` and `E` respectively).
33+
/// - Incremental addition of new nodes/edges (but not removal).
34+
/// - Flat storage of node/edge data in a pair of vectors.
35+
/// - Iteration over any node's out-edges or in-edges, via linked lists
36+
/// threaded through the node/edge data.
37+
///
38+
/// # Caution
39+
/// This is an older graph implementation that is still used by some pieces
40+
/// of diagnostic/debugging code. New code that needs a graph data structure
41+
/// should consider using `VecGraph` instead, or implementing its own
42+
/// special-purpose graph with the specific features needed.
43+
///
44+
/// This graph implementation predates the later [graph traits](crate::graph),
45+
/// and does not implement those traits, so it has its own implementations of a
46+
/// few basic graph algorithms.
47+
pub struct LinkedGraph<N, E> {
3248
nodes: Vec<Node<N>>,
3349
edges: Vec<Edge<E>>,
3450
}
@@ -71,13 +87,13 @@ impl NodeIndex {
7187
}
7288
}
7389

74-
impl<N: Debug, E: Debug> Graph<N, E> {
75-
pub fn new() -> Graph<N, E> {
76-
Graph { nodes: Vec::new(), edges: Vec::new() }
90+
impl<N: Debug, E: Debug> LinkedGraph<N, E> {
91+
pub fn new() -> Self {
92+
Self { nodes: Vec::new(), edges: Vec::new() }
7793
}
7894

79-
pub fn with_capacity(nodes: usize, edges: usize) -> Graph<N, E> {
80-
Graph { nodes: Vec::with_capacity(nodes), edges: Vec::with_capacity(edges) }
95+
pub fn with_capacity(nodes: usize, edges: usize) -> Self {
96+
Self { nodes: Vec::with_capacity(nodes), edges: Vec::with_capacity(edges) }
8197
}
8298

8399
// # Simple accessors
@@ -249,7 +265,7 @@ impl<N: Debug, E: Debug> Graph<N, E> {
249265
// # Iterators
250266

251267
pub struct AdjacentEdges<'g, N, E> {
252-
graph: &'g Graph<N, E>,
268+
graph: &'g LinkedGraph<N, E>,
253269
direction: Direction,
254270
next: EdgeIndex,
255271
}
@@ -285,15 +301,15 @@ impl<'g, N: Debug, E: Debug> Iterator for AdjacentEdges<'g, N, E> {
285301
}
286302

287303
pub struct DepthFirstTraversal<'g, N, E> {
288-
graph: &'g Graph<N, E>,
304+
graph: &'g LinkedGraph<N, E>,
289305
stack: Vec<NodeIndex>,
290306
visited: DenseBitSet<usize>,
291307
direction: Direction,
292308
}
293309

294310
impl<'g, N: Debug, E: Debug> DepthFirstTraversal<'g, N, E> {
295311
pub fn with_start_node(
296-
graph: &'g Graph<N, E>,
312+
graph: &'g LinkedGraph<N, E>,
297313
start_node: NodeIndex,
298314
direction: Direction,
299315
) -> Self {

compiler/rustc_data_structures/src/graph/implementation/tests.rs renamed to compiler/rustc_data_structures/src/graph/linked_graph/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use tracing::debug;
22

3-
use crate::graph::implementation::*;
3+
use super::{Debug, LinkedGraph, NodeIndex};
44

5-
type TestGraph = Graph<&'static str, &'static str>;
5+
type TestGraph = LinkedGraph<&'static str, &'static str>;
66

77
fn create_graph() -> TestGraph {
8-
let mut graph = Graph::new();
8+
let mut graph = LinkedGraph::new();
99

1010
// Create a simple graph
1111
//
@@ -56,7 +56,7 @@ fn each_edge() {
5656
}
5757

5858
fn test_adjacent_edges<N: PartialEq + Debug, E: PartialEq + Debug>(
59-
graph: &Graph<N, E>,
59+
graph: &LinkedGraph<N, E>,
6060
start_index: NodeIndex,
6161
start_data: N,
6262
expected_incoming: &[(E, N)],

compiler/rustc_data_structures/src/graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_index::Idx;
22

33
pub mod dominators;
4-
pub mod implementation;
54
pub mod iterate;
5+
pub mod linked_graph;
66
mod reference;
77
pub mod reversed;
88
pub mod scc;

compiler/rustc_incremental/src/assert_dep_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::fs::{self, File};
3838
use std::io::Write;
3939

4040
use rustc_data_structures::fx::FxIndexSet;
41-
use rustc_data_structures::graph::implementation::{Direction, INCOMING, NodeIndex, OUTGOING};
41+
use rustc_data_structures::graph::linked_graph::{Direction, INCOMING, NodeIndex, OUTGOING};
4242
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
4343
use rustc_hir::intravisit::{self, Visitor};
4444
use rustc_middle::dep_graph::{

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use std::fmt;
44

55
use rustc_data_structures::fx::FxHashSet;
6-
use rustc_data_structures::graph::implementation::{
7-
Direction, Graph, INCOMING, NodeIndex, OUTGOING,
6+
use rustc_data_structures::graph::linked_graph::{
7+
Direction, INCOMING, LinkedGraph, NodeIndex, OUTGOING,
88
};
99
use rustc_data_structures::intern::Interned;
1010
use rustc_data_structures::unord::UnordSet;
@@ -118,7 +118,7 @@ struct RegionAndOrigin<'tcx> {
118118
origin: SubregionOrigin<'tcx>,
119119
}
120120

121-
type RegionGraph<'tcx> = Graph<(), Constraint<'tcx>>;
121+
type RegionGraph<'tcx> = LinkedGraph<(), Constraint<'tcx>>;
122122

123123
struct LexicalResolver<'cx, 'tcx> {
124124
region_rels: &'cx RegionRelations<'cx, 'tcx>,
@@ -668,7 +668,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
668668
fn construct_graph(&self) -> RegionGraph<'tcx> {
669669
let num_vars = self.num_vars();
670670

671-
let mut graph = Graph::new();
671+
let mut graph = LinkedGraph::new();
672672

673673
for _ in 0..num_vars {
674674
graph.add_node(());

compiler/rustc_query_system/src/dep_graph/query.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use rustc_data_structures::fx::FxHashMap;
2-
use rustc_data_structures::graph::implementation::{Direction, Graph, INCOMING, NodeIndex};
2+
use rustc_data_structures::graph::linked_graph::{Direction, INCOMING, LinkedGraph, NodeIndex};
33
use rustc_index::IndexVec;
44

55
use super::{DepNode, DepNodeIndex};
66

77
pub struct DepGraphQuery {
8-
pub graph: Graph<DepNode, ()>,
8+
pub graph: LinkedGraph<DepNode, ()>,
99
pub indices: FxHashMap<DepNode, NodeIndex>,
1010
pub dep_index_to_index: IndexVec<DepNodeIndex, Option<NodeIndex>>,
1111
}
@@ -15,7 +15,7 @@ impl DepGraphQuery {
1515
let node_count = prev_node_count + prev_node_count / 4;
1616
let edge_count = 6 * node_count;
1717

18-
let graph = Graph::with_capacity(node_count, edge_count);
18+
let graph = LinkedGraph::with_capacity(node_count, edge_count);
1919
let indices = FxHashMap::default();
2020
let dep_index_to_index = IndexVec::new();
2121

0 commit comments

Comments
 (0)