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