Skip to content

Commit ef83c93

Browse files
committed
refactor: Add a NodeIndex alias to clarify ObligationForest
1 parent 18b1a6d commit ef83c93

File tree

1 file changed

+30
-17
lines changed
  • src/librustc_data_structures/obligation_forest

1 file changed

+30
-17
lines changed

src/librustc_data_structures/obligation_forest/mod.rs

+30-17
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,24 @@ struct ObligationTreeId(usize);
128128
type ObligationTreeIdGenerator =
129129
::std::iter::Map<::std::ops::RangeFrom<usize>, fn(usize) -> ObligationTreeId>;
130130

131+
/// `usize` indices are used here and throughout this module, rather than
132+
/// `rustc_index::newtype_index!` indices, because this code is hot enough
133+
/// that the `u32`-to-`usize` conversions that would be required are
134+
/// significant, and space considerations are not important.
135+
type NodeIndex = usize;
136+
131137
pub struct ObligationForest<O: ForestObligation> {
132138
/// The list of obligations. In between calls to `process_obligations`,
133139
/// this list only contains nodes in the `Pending` or `Waiting` state.
134-
///
135-
/// `usize` indices are used here and throughout this module, rather than
136-
/// `rustc_index::newtype_index!` indices, because this code is hot enough
137-
/// that the `u32`-to-`usize` conversions that would be required are
138-
/// significant, and space considerations are not important.
139140
nodes: Vec<Node<O>>,
140141

141142
/// A cache of the nodes in `nodes`, indexed by predicate. Unfortunately,
142143
/// its contents are not guaranteed to match those of `nodes`. See the
143144
/// comments in `process_obligation` for details.
144-
active_cache: FxHashMap<O::Predicate, Option<usize>>,
145+
active_cache: FxHashMap<O::Predicate, Option<NodeIndex>>,
145146

146147
/// A vector reused in compress(), to avoid allocating new vectors.
147-
node_rewrites: Vec<usize>,
148+
node_rewrites: Vec<NodeIndex>,
148149

149150
obligation_tree_id_generator: ObligationTreeIdGenerator,
150151

@@ -165,12 +166,12 @@ struct Node<O> {
165166

166167
/// Obligations that depend on this obligation for their completion. They
167168
/// must all be in a non-pending state.
168-
dependents: Vec<usize>,
169+
dependents: Vec<NodeIndex>,
169170

170171
/// If true, dependents[0] points to a "parent" node, which requires
171172
/// special treatment upon error but is otherwise treated the same.
172173
/// (It would be more idiomatic to store the parent node in a separate
173-
/// `Option<usize>` field, but that slows down the common case of
174+
/// `Option<NodeIndex>` field, but that slows down the common case of
174175
/// iterating over the parent and other descendants together.)
175176
has_parent: bool,
176177

@@ -179,7 +180,11 @@ struct Node<O> {
179180
}
180181

181182
impl<O> Node<O> {
182-
fn new(parent: Option<usize>, obligation: O, obligation_tree_id: ObligationTreeId) -> Node<O> {
183+
fn new(
184+
parent: Option<NodeIndex>,
185+
obligation: O,
186+
obligation_tree_id: ObligationTreeId,
187+
) -> Node<O> {
183188
Node {
184189
obligation,
185190
state: Cell::new(NodeState::Pending),
@@ -301,7 +306,11 @@ impl<O: ForestObligation> ObligationForest<O> {
301306
}
302307

303308
// Returns Err(()) if we already know this obligation failed.
304-
fn register_obligation_at(&mut self, obligation: O, parent: Option<usize>) -> Result<(), ()> {
309+
fn register_obligation_at(
310+
&mut self,
311+
obligation: O,
312+
parent: Option<NodeIndex>,
313+
) -> Result<(), ()> {
305314
match self.active_cache.entry(obligation.as_predicate().clone()) {
306315
Entry::Occupied(o) => {
307316
let index = match o.get() {
@@ -372,7 +381,7 @@ impl<O: ForestObligation> ObligationForest<O> {
372381
.collect()
373382
}
374383

375-
fn insert_into_error_cache(&mut self, index: usize) {
384+
fn insert_into_error_cache(&mut self, index: NodeIndex) {
376385
let node = &self.nodes[index];
377386
self.error_cache
378387
.entry(node.obligation_tree_id)
@@ -462,8 +471,8 @@ impl<O: ForestObligation> ObligationForest<O> {
462471

463472
/// Returns a vector of obligations for `p` and all of its
464473
/// ancestors, putting them into the error state in the process.
465-
fn error_at(&self, mut index: usize) -> Vec<O> {
466-
let mut error_stack: Vec<usize> = vec![];
474+
fn error_at(&self, mut index: NodeIndex) -> Vec<O> {
475+
let mut error_stack: Vec<NodeIndex> = vec![];
467476
let mut trace = vec![];
468477

469478
loop {
@@ -555,8 +564,12 @@ impl<O: ForestObligation> ObligationForest<O> {
555564
debug_assert!(stack.is_empty());
556565
}
557566

558-
fn find_cycles_from_node<P>(&self, stack: &mut Vec<usize>, processor: &mut P, index: usize)
559-
where
567+
fn find_cycles_from_node<P>(
568+
&self,
569+
stack: &mut Vec<NodeIndex>,
570+
processor: &mut P,
571+
index: NodeIndex,
572+
) where
560573
P: ObligationProcessor<Obligation = O>,
561574
{
562575
let node = &self.nodes[index];
@@ -643,7 +656,7 @@ impl<O: ForestObligation> ObligationForest<O> {
643656
if do_completed == DoCompleted::Yes { Some(removed_done_obligations) } else { None }
644657
}
645658

646-
fn apply_rewrites(&mut self, node_rewrites: &[usize]) {
659+
fn apply_rewrites(&mut self, node_rewrites: &[NodeIndex]) {
647660
let orig_nodes_len = node_rewrites.len();
648661
let remove_node_marker = orig_nodes_len + 1;
649662

0 commit comments

Comments
 (0)