]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_data_structures/src/obligation_forest/mod.rs
Rollup merge of #101997 - cuviper:drop-legacy-pm, r=nikic
[rust.git] / compiler / rustc_data_structures / src / obligation_forest / mod.rs
1 //! The `ObligationForest` is a utility data structure used in trait
2 //! matching to track the set of outstanding obligations (those not yet
3 //! resolved to success or error). It also tracks the "backtrace" of each
4 //! pending obligation (why we are trying to figure this out in the first
5 //! place).
6 //!
7 //! ### External view
8 //!
9 //! `ObligationForest` supports two main public operations (there are a
10 //! few others not discussed here):
11 //!
12 //! 1. Add a new root obligations (`register_obligation`).
13 //! 2. Process the pending obligations (`process_obligations`).
14 //!
15 //! When a new obligation `N` is added, it becomes the root of an
16 //! obligation tree. This tree can also carry some per-tree state `T`,
17 //! which is given at the same time. This tree is a singleton to start, so
18 //! `N` is both the root and the only leaf. Each time the
19 //! `process_obligations` method is called, it will invoke its callback
20 //! with every pending obligation (so that will include `N`, the first
21 //! time). The callback also receives a (mutable) reference to the
22 //! per-tree state `T`. The callback should process the obligation `O`
23 //! that it is given and return a `ProcessResult`:
24 //!
25 //! - `Unchanged` -> ambiguous result. Obligation was neither a success
26 //!   nor a failure. It is assumed that further attempts to process the
27 //!   obligation will yield the same result unless something in the
28 //!   surrounding environment changes.
29 //! - `Changed(C)` - the obligation was *shallowly successful*. The
30 //!   vector `C` is a list of subobligations. The meaning of this is that
31 //!   `O` was successful on the assumption that all the obligations in `C`
32 //!   are also successful. Therefore, `O` is only considered a "true"
33 //!   success if `C` is empty. Otherwise, `O` is put into a suspended
34 //!   state and the obligations in `C` become the new pending
35 //!   obligations. They will be processed the next time you call
36 //!   `process_obligations`.
37 //! - `Error(E)` -> obligation failed with error `E`. We will collect this
38 //!   error and return it from `process_obligations`, along with the
39 //!   "backtrace" of obligations (that is, the list of obligations up to
40 //!   and including the root of the failed obligation). No further
41 //!   obligations from that same tree will be processed, since the tree is
42 //!   now considered to be in error.
43 //!
44 //! When the call to `process_obligations` completes, you get back an `Outcome`,
45 //! which includes two bits of information:
46 //!
47 //! - `completed`: a list of obligations where processing was fully
48 //!   completed without error (meaning that all transitive subobligations
49 //!   have also been completed). So, for example, if the callback from
50 //!   `process_obligations` returns `Changed(C)` for some obligation `O`,
51 //!   then `O` will be considered completed right away if `C` is the
52 //!   empty vector. Otherwise it will only be considered completed once
53 //!   all the obligations in `C` have been found completed.
54 //! - `errors`: a list of errors that occurred and associated backtraces
55 //!   at the time of error, which can be used to give context to the user.
56 //!
57 //! Upon completion, none of the existing obligations were *shallowly
58 //! successful* (that is, no callback returned `Changed(_)`). This implies that
59 //! all obligations were either errors or returned an ambiguous result.
60 //!
61 //! ### Implementation details
62 //!
63 //! For the most part, comments specific to the implementation are in the
64 //! code. This file only contains a very high-level overview. Basically,
65 //! the forest is stored in a vector. Each element of the vector is a node
66 //! in some tree. Each node in the vector has the index of its dependents,
67 //! including the first dependent which is known as the parent. It also
68 //! has a current state, described by `NodeState`. After each processing
69 //! step, we compress the vector to remove completed and error nodes, which
70 //! aren't needed anymore.
71
72 use crate::fx::{FxHashMap, FxHashSet};
73
74 use std::cell::Cell;
75 use std::collections::hash_map::Entry;
76 use std::fmt::Debug;
77 use std::hash;
78 use std::marker::PhantomData;
79
80 mod graphviz;
81
82 #[cfg(test)]
83 mod tests;
84
85 pub trait ForestObligation: Clone + Debug {
86     type CacheKey: Clone + hash::Hash + Eq + Debug;
87
88     /// Converts this `ForestObligation` suitable for use as a cache key.
89     /// If two distinct `ForestObligations`s return the same cache key,
90     /// then it must be sound to use the result of processing one obligation
91     /// (e.g. success for error) for the other obligation
92     fn as_cache_key(&self) -> Self::CacheKey;
93 }
94
95 pub trait ObligationProcessor {
96     type Obligation: ForestObligation;
97     type Error: Debug;
98     type OUT: OutcomeTrait<
99         Obligation = Self::Obligation,
100         Error = Error<Self::Obligation, Self::Error>,
101     >;
102
103     fn needs_process_obligation(&self, obligation: &Self::Obligation) -> bool;
104
105     fn process_obligation(
106         &mut self,
107         obligation: &mut Self::Obligation,
108     ) -> ProcessResult<Self::Obligation, Self::Error>;
109
110     /// As we do the cycle check, we invoke this callback when we
111     /// encounter an actual cycle. `cycle` is an iterator that starts
112     /// at the start of the cycle in the stack and walks **toward the
113     /// top**.
114     ///
115     /// In other words, if we had O1 which required O2 which required
116     /// O3 which required O1, we would give an iterator yielding O1,
117     /// O2, O3 (O1 is not yielded twice).
118     fn process_backedge<'c, I>(
119         &mut self,
120         cycle: I,
121         _marker: PhantomData<&'c Self::Obligation>,
122     ) -> Result<(), Self::Error>
123     where
124         I: Clone + Iterator<Item = &'c Self::Obligation>;
125 }
126
127 /// The result type used by `process_obligation`.
128 // `repr(C)` to inhibit the niche filling optimization. Otherwise, the `match` appearing
129 // in `process_obligations` is significantly slower, which can substantially affect
130 // benchmarks like `rustc-perf`'s inflate and keccak.
131 #[repr(C)]
132 #[derive(Debug)]
133 pub enum ProcessResult<O, E> {
134     Unchanged,
135     Changed(Vec<O>),
136     Error(E),
137 }
138
139 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
140 struct ObligationTreeId(usize);
141
142 type ObligationTreeIdGenerator =
143     std::iter::Map<std::ops::RangeFrom<usize>, fn(usize) -> ObligationTreeId>;
144
145 pub struct ObligationForest<O: ForestObligation> {
146     /// The list of obligations. In between calls to [Self::process_obligations],
147     /// this list only contains nodes in the `Pending` or `Waiting` state.
148     ///
149     /// `usize` indices are used here and throughout this module, rather than
150     /// [`rustc_index::newtype_index!`] indices, because this code is hot enough
151     /// that the `u32`-to-`usize` conversions that would be required are
152     /// significant, and space considerations are not important.
153     nodes: Vec<Node<O>>,
154
155     /// A cache of predicates that have been successfully completed.
156     done_cache: FxHashSet<O::CacheKey>,
157
158     /// A cache of the nodes in `nodes`, indexed by predicate. Unfortunately,
159     /// its contents are not guaranteed to match those of `nodes`. See the
160     /// comments in `Self::process_obligation` for details.
161     active_cache: FxHashMap<O::CacheKey, usize>,
162
163     /// A vector reused in [Self::compress()] and [Self::find_cycles_from_node()],
164     /// to avoid allocating new vectors.
165     reused_node_vec: Vec<usize>,
166
167     obligation_tree_id_generator: ObligationTreeIdGenerator,
168
169     /// Per tree error cache. This is used to deduplicate errors,
170     /// which is necessary to avoid trait resolution overflow in
171     /// some cases.
172     ///
173     /// See [this][details] for details.
174     ///
175     /// [details]: https://github.com/rust-lang/rust/pull/53255#issuecomment-421184780
176     error_cache: FxHashMap<ObligationTreeId, FxHashSet<O::CacheKey>>,
177 }
178
179 #[derive(Debug)]
180 struct Node<O> {
181     obligation: O,
182     state: Cell<NodeState>,
183
184     /// Obligations that depend on this obligation for their completion. They
185     /// must all be in a non-pending state.
186     dependents: Vec<usize>,
187
188     /// If true, `dependents[0]` points to a "parent" node, which requires
189     /// special treatment upon error but is otherwise treated the same.
190     /// (It would be more idiomatic to store the parent node in a separate
191     /// `Option<usize>` field, but that slows down the common case of
192     /// iterating over the parent and other descendants together.)
193     has_parent: bool,
194
195     /// Identifier of the obligation tree to which this node belongs.
196     obligation_tree_id: ObligationTreeId,
197 }
198
199 impl<O> Node<O> {
200     fn new(parent: Option<usize>, obligation: O, obligation_tree_id: ObligationTreeId) -> Node<O> {
201         Node {
202             obligation,
203             state: Cell::new(NodeState::Pending),
204             dependents: if let Some(parent_index) = parent { vec![parent_index] } else { vec![] },
205             has_parent: parent.is_some(),
206             obligation_tree_id,
207         }
208     }
209 }
210
211 /// The state of one node in some tree within the forest. This represents the
212 /// current state of processing for the obligation (of type `O`) associated
213 /// with this node.
214 ///
215 /// The non-`Error` state transitions are as follows.
216 /// ```text
217 /// (Pre-creation)
218 ///  |
219 ///  |     register_obligation_at() (called by process_obligations() and
220 ///  v                               from outside the crate)
221 /// Pending
222 ///  |
223 ///  |     process_obligations()
224 ///  v
225 /// Success
226 ///  |  ^
227 ///  |  |  mark_successes()
228 ///  |  v
229 ///  |  Waiting
230 ///  |
231 ///  |     process_cycles()
232 ///  v
233 /// Done
234 ///  |
235 ///  |     compress()
236 ///  v
237 /// (Removed)
238 /// ```
239 /// The `Error` state can be introduced in several places, via `error_at()`.
240 ///
241 /// Outside of `ObligationForest` methods, nodes should be either `Pending` or
242 /// `Waiting`.
243 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
244 enum NodeState {
245     /// This obligation has not yet been selected successfully. Cannot have
246     /// subobligations.
247     Pending,
248
249     /// This obligation was selected successfully, but may or may not have
250     /// subobligations.
251     Success,
252
253     /// This obligation was selected successfully, but it has a pending
254     /// subobligation.
255     Waiting,
256
257     /// This obligation, along with its subobligations, are complete, and will
258     /// be removed in the next collection.
259     Done,
260
261     /// This obligation was resolved to an error. It will be removed by the
262     /// next compression step.
263     Error,
264 }
265
266 /// This trait allows us to have two different Outcome types:
267 ///  - the normal one that does as little as possible
268 ///  - one for tests that does some additional work and checking
269 pub trait OutcomeTrait {
270     type Error;
271     type Obligation;
272
273     fn new() -> Self;
274     fn record_completed(&mut self, outcome: &Self::Obligation);
275     fn record_error(&mut self, error: Self::Error);
276 }
277
278 #[derive(Debug)]
279 pub struct Outcome<O, E> {
280     /// Backtrace of obligations that were found to be in error.
281     pub errors: Vec<Error<O, E>>,
282 }
283
284 impl<O, E> OutcomeTrait for Outcome<O, E> {
285     type Error = Error<O, E>;
286     type Obligation = O;
287
288     fn new() -> Self {
289         Self { errors: vec![] }
290     }
291
292     fn record_completed(&mut self, _outcome: &Self::Obligation) {
293         // do nothing
294     }
295
296     fn record_error(&mut self, error: Self::Error) {
297         self.errors.push(error)
298     }
299 }
300
301 #[derive(Debug, PartialEq, Eq)]
302 pub struct Error<O, E> {
303     pub error: E,
304     pub backtrace: Vec<O>,
305 }
306
307 impl<O: ForestObligation> ObligationForest<O> {
308     pub fn new() -> ObligationForest<O> {
309         ObligationForest {
310             nodes: vec![],
311             done_cache: Default::default(),
312             active_cache: Default::default(),
313             reused_node_vec: vec![],
314             obligation_tree_id_generator: (0..).map(ObligationTreeId),
315             error_cache: Default::default(),
316         }
317     }
318
319     /// Returns the total number of nodes in the forest that have not
320     /// yet been fully resolved.
321     pub fn len(&self) -> usize {
322         self.nodes.len()
323     }
324
325     /// Registers an obligation.
326     pub fn register_obligation(&mut self, obligation: O) {
327         // Ignore errors here - there is no guarantee of success.
328         let _ = self.register_obligation_at(obligation, None);
329     }
330
331     // Returns Err(()) if we already know this obligation failed.
332     fn register_obligation_at(&mut self, obligation: O, parent: Option<usize>) -> Result<(), ()> {
333         let cache_key = obligation.as_cache_key();
334         if self.done_cache.contains(&cache_key) {
335             debug!("register_obligation_at: ignoring already done obligation: {:?}", obligation);
336             return Ok(());
337         }
338
339         match self.active_cache.entry(cache_key) {
340             Entry::Occupied(o) => {
341                 let node = &mut self.nodes[*o.get()];
342                 if let Some(parent_index) = parent {
343                     // If the node is already in `active_cache`, it has already
344                     // had its chance to be marked with a parent. So if it's
345                     // not already present, just dump `parent` into the
346                     // dependents as a non-parent.
347                     if !node.dependents.contains(&parent_index) {
348                         node.dependents.push(parent_index);
349                     }
350                 }
351                 if let NodeState::Error = node.state.get() { Err(()) } else { Ok(()) }
352             }
353             Entry::Vacant(v) => {
354                 let obligation_tree_id = match parent {
355                     Some(parent_index) => self.nodes[parent_index].obligation_tree_id,
356                     None => self.obligation_tree_id_generator.next().unwrap(),
357                 };
358
359                 let already_failed = parent.is_some()
360                     && self
361                         .error_cache
362                         .get(&obligation_tree_id)
363                         .map_or(false, |errors| errors.contains(v.key()));
364
365                 if already_failed {
366                     Err(())
367                 } else {
368                     let new_index = self.nodes.len();
369                     v.insert(new_index);
370                     self.nodes.push(Node::new(parent, obligation, obligation_tree_id));
371                     Ok(())
372                 }
373             }
374         }
375     }
376
377     /// Converts all remaining obligations to the given error.
378     pub fn to_errors<E: Clone>(&mut self, error: E) -> Vec<Error<O, E>> {
379         let errors = self
380             .nodes
381             .iter()
382             .enumerate()
383             .filter(|(_index, node)| node.state.get() == NodeState::Pending)
384             .map(|(index, _node)| Error { error: error.clone(), backtrace: self.error_at(index) })
385             .collect();
386
387         self.compress(|_| assert!(false));
388         errors
389     }
390
391     /// Returns the set of obligations that are in a pending state.
392     pub fn map_pending_obligations<P, F>(&self, f: F) -> Vec<P>
393     where
394         F: Fn(&O) -> P,
395     {
396         self.nodes
397             .iter()
398             .filter(|node| node.state.get() == NodeState::Pending)
399             .map(|node| f(&node.obligation))
400             .collect()
401     }
402
403     fn insert_into_error_cache(&mut self, index: usize) {
404         let node = &self.nodes[index];
405         self.error_cache
406             .entry(node.obligation_tree_id)
407             .or_default()
408             .insert(node.obligation.as_cache_key());
409     }
410
411     /// Performs a fixpoint computation over the obligation list.
412     #[inline(never)]
413     pub fn process_obligations<P>(&mut self, processor: &mut P) -> P::OUT
414     where
415         P: ObligationProcessor<Obligation = O>,
416     {
417         let mut outcome = P::OUT::new();
418
419         // Fixpoint computation: we repeat until the inner loop stalls.
420         loop {
421             let mut has_changed = false;
422
423             // Note that the loop body can append new nodes, and those new nodes
424             // will then be processed by subsequent iterations of the loop.
425             //
426             // We can't use an iterator for the loop because `self.nodes` is
427             // appended to and the borrow checker would complain. We also can't use
428             // `for index in 0..self.nodes.len() { ... }` because the range would
429             // be computed with the initial length, and we would miss the appended
430             // nodes. Therefore we use a `while` loop.
431             let mut index = 0;
432             while let Some(node) = self.nodes.get_mut(index) {
433                 if node.state.get() != NodeState::Pending
434                     || !processor.needs_process_obligation(&node.obligation)
435                 {
436                     index += 1;
437                     continue;
438                 }
439
440                 // `processor.process_obligation` can modify the predicate within
441                 // `node.obligation`, and that predicate is the key used for
442                 // `self.active_cache`. This means that `self.active_cache` can get
443                 // out of sync with `nodes`. It's not very common, but it does
444                 // happen, and code in `compress` has to allow for it.
445
446                 match processor.process_obligation(&mut node.obligation) {
447                     ProcessResult::Unchanged => {
448                         // No change in state.
449                     }
450                     ProcessResult::Changed(children) => {
451                         // We are not (yet) stalled.
452                         has_changed = true;
453                         node.state.set(NodeState::Success);
454
455                         for child in children {
456                             let st = self.register_obligation_at(child, Some(index));
457                             if let Err(()) = st {
458                                 // Error already reported - propagate it
459                                 // to our node.
460                                 self.error_at(index);
461                             }
462                         }
463                     }
464                     ProcessResult::Error(err) => {
465                         has_changed = true;
466                         outcome.record_error(Error { error: err, backtrace: self.error_at(index) });
467                     }
468                 }
469                 index += 1;
470             }
471
472             // If unchanged, then we saw no successful obligations, which means
473             // there is no point in further iteration. This is based on the
474             // assumption that when trait matching returns `Error` or
475             // `Unchanged`, those results do not affect environmental inference
476             // state. (Note that this will occur if we invoke
477             // `process_obligations` with no pending obligations.)
478             if !has_changed {
479                 break;
480             }
481
482             self.mark_successes();
483             self.process_cycles(processor, &mut outcome);
484             self.compress(|obl| outcome.record_completed(obl));
485         }
486
487         outcome
488     }
489
490     /// Returns a vector of obligations for `p` and all of its
491     /// ancestors, putting them into the error state in the process.
492     fn error_at(&self, mut index: usize) -> Vec<O> {
493         let mut error_stack: Vec<usize> = vec![];
494         let mut trace = vec![];
495
496         loop {
497             let node = &self.nodes[index];
498             node.state.set(NodeState::Error);
499             trace.push(node.obligation.clone());
500             if node.has_parent {
501                 // The first dependent is the parent, which is treated
502                 // specially.
503                 error_stack.extend(node.dependents.iter().skip(1));
504                 index = node.dependents[0];
505             } else {
506                 // No parent; treat all dependents non-specially.
507                 error_stack.extend(node.dependents.iter());
508                 break;
509             }
510         }
511
512         while let Some(index) = error_stack.pop() {
513             let node = &self.nodes[index];
514             if node.state.get() != NodeState::Error {
515                 node.state.set(NodeState::Error);
516                 error_stack.extend(node.dependents.iter());
517             }
518         }
519
520         trace
521     }
522
523     /// Mark all `Waiting` nodes as `Success`, except those that depend on a
524     /// pending node.
525     fn mark_successes(&self) {
526         // Convert all `Waiting` nodes to `Success`.
527         for node in &self.nodes {
528             if node.state.get() == NodeState::Waiting {
529                 node.state.set(NodeState::Success);
530             }
531         }
532
533         // Convert `Success` nodes that depend on a pending node back to
534         // `Waiting`.
535         for node in &self.nodes {
536             if node.state.get() == NodeState::Pending {
537                 // This call site is hot.
538                 self.inlined_mark_dependents_as_waiting(node);
539             }
540         }
541     }
542
543     // This always-inlined function is for the hot call site.
544     #[inline(always)]
545     fn inlined_mark_dependents_as_waiting(&self, node: &Node<O>) {
546         for &index in node.dependents.iter() {
547             let node = &self.nodes[index];
548             let state = node.state.get();
549             if state == NodeState::Success {
550                 // This call site is cold.
551                 self.uninlined_mark_dependents_as_waiting(node);
552             } else {
553                 debug_assert!(state == NodeState::Waiting || state == NodeState::Error)
554             }
555         }
556     }
557
558     // This never-inlined function is for the cold call site.
559     #[inline(never)]
560     fn uninlined_mark_dependents_as_waiting(&self, node: &Node<O>) {
561         // Mark node Waiting in the cold uninlined code instead of the hot inlined
562         node.state.set(NodeState::Waiting);
563         self.inlined_mark_dependents_as_waiting(node)
564     }
565
566     /// Report cycles between all `Success` nodes, and convert all `Success`
567     /// nodes to `Done`. This must be called after `mark_successes`.
568     fn process_cycles<P>(&mut self, processor: &mut P, outcome: &mut P::OUT)
569     where
570         P: ObligationProcessor<Obligation = O>,
571     {
572         let mut stack = std::mem::take(&mut self.reused_node_vec);
573         for (index, node) in self.nodes.iter().enumerate() {
574             // For some benchmarks this state test is extremely hot. It's a win
575             // to handle the no-op cases immediately to avoid the cost of the
576             // function call.
577             if node.state.get() == NodeState::Success {
578                 self.find_cycles_from_node(&mut stack, processor, index, outcome);
579             }
580         }
581
582         debug_assert!(stack.is_empty());
583         self.reused_node_vec = stack;
584     }
585
586     fn find_cycles_from_node<P>(
587         &self,
588         stack: &mut Vec<usize>,
589         processor: &mut P,
590         index: usize,
591         outcome: &mut P::OUT,
592     ) where
593         P: ObligationProcessor<Obligation = O>,
594     {
595         let node = &self.nodes[index];
596         if node.state.get() == NodeState::Success {
597             match stack.iter().rposition(|&n| n == index) {
598                 None => {
599                     stack.push(index);
600                     for &dep_index in node.dependents.iter() {
601                         self.find_cycles_from_node(stack, processor, dep_index, outcome);
602                     }
603                     stack.pop();
604                     node.state.set(NodeState::Done);
605                 }
606                 Some(rpos) => {
607                     // Cycle detected.
608                     let result = processor.process_backedge(
609                         stack[rpos..].iter().map(|&i| &self.nodes[i].obligation),
610                         PhantomData,
611                     );
612                     if let Err(err) = result {
613                         outcome.record_error(Error { error: err, backtrace: self.error_at(index) });
614                     }
615                 }
616             }
617         }
618     }
619
620     /// Compresses the vector, removing all popped nodes. This adjusts the
621     /// indices and hence invalidates any outstanding indices. `process_cycles`
622     /// must be run beforehand to remove any cycles on `Success` nodes.
623     #[inline(never)]
624     fn compress(&mut self, mut outcome_cb: impl FnMut(&O)) {
625         let orig_nodes_len = self.nodes.len();
626         let mut node_rewrites: Vec<_> = std::mem::take(&mut self.reused_node_vec);
627         debug_assert!(node_rewrites.is_empty());
628         node_rewrites.extend(0..orig_nodes_len);
629         let mut dead_nodes = 0;
630
631         // Move removable nodes to the end, preserving the order of the
632         // remaining nodes.
633         //
634         // LOOP INVARIANT:
635         //     self.nodes[0..index - dead_nodes] are the first remaining nodes
636         //     self.nodes[index - dead_nodes..index] are all dead
637         //     self.nodes[index..] are unchanged
638         for index in 0..orig_nodes_len {
639             let node = &self.nodes[index];
640             match node.state.get() {
641                 NodeState::Pending | NodeState::Waiting => {
642                     if dead_nodes > 0 {
643                         self.nodes.swap(index, index - dead_nodes);
644                         node_rewrites[index] -= dead_nodes;
645                     }
646                 }
647                 NodeState::Done => {
648                     // The removal lookup might fail because the contents of
649                     // `self.active_cache` are not guaranteed to match those of
650                     // `self.nodes`. See the comment in `process_obligation`
651                     // for more details.
652                     let cache_key = node.obligation.as_cache_key();
653                     self.active_cache.remove(&cache_key);
654                     self.done_cache.insert(cache_key);
655
656                     // Extract the success stories.
657                     outcome_cb(&node.obligation);
658                     node_rewrites[index] = orig_nodes_len;
659                     dead_nodes += 1;
660                 }
661                 NodeState::Error => {
662                     // We *intentionally* remove the node from the cache at this point. Otherwise
663                     // tests must come up with a different type on every type error they
664                     // check against.
665                     self.active_cache.remove(&node.obligation.as_cache_key());
666                     self.insert_into_error_cache(index);
667                     node_rewrites[index] = orig_nodes_len;
668                     dead_nodes += 1;
669                 }
670                 NodeState::Success => unreachable!(),
671             }
672         }
673
674         if dead_nodes > 0 {
675             // Remove the dead nodes and rewrite indices.
676             self.nodes.truncate(orig_nodes_len - dead_nodes);
677             self.apply_rewrites(&node_rewrites);
678         }
679
680         node_rewrites.truncate(0);
681         self.reused_node_vec = node_rewrites;
682     }
683
684     #[inline(never)]
685     fn apply_rewrites(&mut self, node_rewrites: &[usize]) {
686         let orig_nodes_len = node_rewrites.len();
687
688         for node in &mut self.nodes {
689             let mut i = 0;
690             while let Some(dependent) = node.dependents.get_mut(i) {
691                 let new_index = node_rewrites[*dependent];
692                 if new_index >= orig_nodes_len {
693                     node.dependents.swap_remove(i);
694                     if i == 0 && node.has_parent {
695                         // We just removed the parent.
696                         node.has_parent = false;
697                     }
698                 } else {
699                     *dependent = new_index;
700                     i += 1;
701                 }
702             }
703         }
704
705         // This updating of `self.active_cache` is necessary because the
706         // removal of nodes within `compress` can fail. See above.
707         self.active_cache.retain(|_predicate, index| {
708             let new_index = node_rewrites[*index];
709             if new_index >= orig_nodes_len {
710                 false
711             } else {
712                 *index = new_index;
713                 true
714             }
715         });
716     }
717 }