Skip to content

Commit 128604f

Browse files
committed
refactor(tree): Make debugging node ids easier
I found when debugging some issues with edges, having to track indices was making things much more difficult. My hope is this will help with little negative impact. We could put this behind a `#[cfg(debug_asserts)]` but I'm assuming it doesn't make enough of a performance difference to matter.
1 parent 822135d commit 128604f

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/cargo/ops/tree/graph.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ use std::collections::{HashMap, HashSet};
1313
#[derive(Debug, Copy, Clone)]
1414
pub struct NodeId {
1515
index: usize,
16+
#[allow(dead_code)] // intended for `derive(Debug)`
17+
debug: InternedString,
1618
}
1719

1820
impl NodeId {
19-
fn new(index: usize) -> Self {
20-
Self { index }
21+
fn new(index: usize, debug: InternedString) -> Self {
22+
Self { index, debug }
2123
}
2224
}
2325

@@ -63,6 +65,15 @@ pub enum Node {
6365
},
6466
}
6567

68+
impl Node {
69+
fn name(&self) -> InternedString {
70+
match self {
71+
Self::Package { package_id, .. } => package_id.name(),
72+
Self::Feature { name, .. } => *name,
73+
}
74+
}
75+
}
76+
6677
#[derive(Debug, Copy, Hash, Eq, Clone, PartialEq)]
6778
pub struct Edge {
6879
kind: EdgeKind,
@@ -160,7 +171,7 @@ impl<'a> Graph<'a> {
160171

161172
/// Adds a new node to the graph, returning its new index.
162173
fn add_node(&mut self, node: Node) -> NodeId {
163-
let from_index = NodeId::new(self.nodes.len());
174+
let from_index = NodeId::new(self.nodes.len(), node.name());
164175
self.nodes.push(node);
165176
self.edges.push(Edges::new());
166177
self.index.insert(self.node(from_index).clone(), from_index);
@@ -204,7 +215,7 @@ impl<'a> Graph<'a> {
204215
Node::Package { package_id, .. } => package_ids.contains(package_id),
205216
_ => false,
206217
})
207-
.map(|(i, node)| (node, NodeId::new(i)))
218+
.map(|(i, node)| (node, NodeId::new(i, node.name())))
208219
.collect();
209220
// Sort for consistent output (the same command should always return
210221
// the same output). "unstable" since nodes should always be unique.
@@ -278,7 +289,7 @@ impl<'a> Graph<'a> {
278289
for edge in node_edges.all() {
279290
let new_edge = Edge {
280291
kind: edge.kind(),
281-
node: NodeId::new(from_idx),
292+
node: NodeId::new(from_idx, self.nodes[from_idx].name()),
282293
};
283294
new_edges[edge.node().index].add_edge(new_edge);
284295
}
@@ -299,7 +310,7 @@ impl<'a> Graph<'a> {
299310
packages
300311
.entry(package_id.name())
301312
.or_insert_with(Vec::new)
302-
.push((node, NodeId::new(i)));
313+
.push((node, NodeId::new(i, node.name())));
303314
}
304315
}
305316

@@ -671,7 +682,7 @@ fn add_internal_features(graph: &mut Graph<'_>, resolve: &Resolve) {
671682
Node::Package { .. } => None,
672683
Node::Feature { node_index, name } => {
673684
let package_id = graph.package_id_for_index(*node_index);
674-
Some((package_id, *node_index, NodeId::new(i), *name))
685+
Some((package_id, *node_index, NodeId::new(i, *name), *name))
675686
}
676687
})
677688
.collect();

0 commit comments

Comments
 (0)