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