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