1//! The `ObligationForest` is a utility data structure used in trait
2//! matching to track the set of outstanding obligations (those not yet
3//! resolved to success or error). It also tracks the "backtrace" of each
4//! pending obligation (why we are trying to figure this out in the first
5//! place).
6//!
7//! ### External view
8//!
9//! `ObligationForest` supports two main public operations (there are a
10//! few others not discussed here):
11//!
12//! 1. Add a new root obligations (`register_obligation`).
13//! 2. Process the pending obligations (`process_obligations`).
14//!
15//! When a new obligation `N` is added, it becomes the root of an
16//! obligation tree. This tree can also carry some per-tree state `T`,
17//! which is given at the same time. This tree is a singleton to start, so
18//! `N` is both the root and the only leaf. Each time the
19//! `process_obligations` method is called, it will invoke its callback
20//! with every pending obligation (so that will include `N`, the first
21//! time). The callback also receives a (mutable) reference to the
22//! per-tree state `T`. The callback should process the obligation `O`
23//! that it is given and return a `ProcessResult`:
24//!
25//! - `Unchanged` -> ambiguous result. Obligation was neither a success
26//! nor a failure. It is assumed that further attempts to process the
27//! obligation will yield the same result unless something in the
28//! surrounding environment changes.
29//! - `Changed(C)` - the obligation was *shallowly successful*. The
30//! vector `C` is a list of subobligations. The meaning of this is that
31//! `O` was successful on the assumption that all the obligations in `C`
32//! are also successful. Therefore, `O` is only considered a "true"
33//! success if `C` is empty. Otherwise, `O` is put into a suspended
34//! state and the obligations in `C` become the new pending
35//! obligations. They will be processed the next time you call
36//! `process_obligations`.
37//! - `Error(E)` -> obligation failed with error `E`. We will collect this
38//! error and return it from `process_obligations`, along with the
39//! "backtrace" of obligations (that is, the list of obligations up to
40//! and including the root of the failed obligation). No further
41//! obligations from that same tree will be processed, since the tree is
42//! now considered to be in error.
43//!
44//! When the call to `process_obligations` completes, you get back an `Outcome`,
45//! which includes two bits of information:
46//!
47//! - `completed`: a list of obligations where processing was fully
48//! completed without error (meaning that all transitive subobligations
49//! have also been completed). So, for example, if the callback from
50//! `process_obligations` returns `Changed(C)` for some obligation `O`,
51//! then `O` will be considered completed right away if `C` is the
52//! empty vector. Otherwise it will only be considered completed once
53//! all the obligations in `C` have been found completed.
54//! - `errors`: a list of errors that occurred and associated backtraces
55//! at the time of error, which can be used to give context to the user.
56//!
57//! Upon completion, none of the existing obligations were *shallowly
58//! successful* (that is, no callback returned `Changed(_)`). This implies that
59//! all obligations were either errors or returned an ambiguous result.
60//!
61//! ### Implementation details
62//!
63//! For the most part, comments specific to the implementation are in the
64//! code. This file only contains a very high-level overview. Basically,
65//! the forest is stored in a vector. Each element of the vector is a node
66//! in some tree. Each node in the vector has the index of its dependents,
67//! including the first dependent which is known as the parent. It also
68//! has a current state, described by `NodeState`. After each processing
69//! step, we compress the vector to remove completed and error nodes, which
70//! aren't needed anymore.
7172use std::cell::Cell;
73use std::collections::hash_map::Entry;
74use std::fmt::Debug;
75use std::hash;
76use std::marker::PhantomData;
7778use thin_vec::ThinVec;
79use tracing::debug;
8081use crate::fx::{FxHashMap, FxHashSet};
8283mod graphviz;
8485#[cfg(test)]
86mod tests;
8788pub trait ForestObligation: Clone + Debug {
89type CacheKey: Clone + hash::Hash + Eq + Debug;
9091/// 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
95fn as_cache_key(&self) -> Self::CacheKey;
96}
9798pub trait ObligationProcessor {
99type Obligation: ForestObligation;
100type Error: Debug;
101type OUT: OutcomeTrait<Obligation = Self::Obligation, Error = Error<Self::Obligation, Self::Error>>;
102103/// Implementations can provide a fast-path to obligation-processing
104 /// by counting the prefix of the passed iterator for which
105 /// `needs_process_obligation` would return false.
106fn skippable_obligations<'a>(
107&'a self,
108 _it: impl Iterator<Item = &'a Self::Obligation>,
109 ) -> usize {
1100
111}
112113fn needs_process_obligation(&self, _obligation: &Self::Obligation) -> bool;
114115fn process_obligation(
116&mut self,
117 obligation: &mut Self::Obligation,
118 ) -> ProcessResult<Self::Obligation, Self::Error>;
119120/// As we do the cycle check, we invoke this callback when we
121 /// encounter an actual cycle. `cycle` is an iterator that starts
122 /// at the start of the cycle in the stack and walks **toward the
123 /// top**.
124 ///
125 /// In other words, if we had O1 which required O2 which required
126 /// O3 which required O1, we would give an iterator yielding O1,
127 /// O2, O3 (O1 is not yielded twice).
128fn process_backedge<'c, I>(
129&mut self,
130 cycle: I,
131 _marker: PhantomData<&'c Self::Obligation>,
132 ) -> Result<(), Self::Error>
133where
134I: Clone + Iterator<Item = &'c Self::Obligation>;
135}
136137/// The result type used by `process_obligation`.
138// `repr(C)` to inhibit the niche filling optimization. Otherwise, the `match` appearing
139// in `process_obligations` is significantly slower, which can substantially affect
140// benchmarks like `rustc-perf`'s inflate and keccak.
141#[repr(C)]
142#[derive(#[automatically_derived]
impl<O: ::core::fmt::Debug, E: ::core::fmt::Debug> ::core::fmt::Debug for
ProcessResult<O, E> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
ProcessResult::Unchanged =>
::core::fmt::Formatter::write_str(f, "Unchanged"),
ProcessResult::Changed(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Changed", &__self_0),
ProcessResult::Error(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Error",
&__self_0),
}
}
}Debug)]
143pub enum ProcessResult<O, E> {
144 Unchanged,
145 Changed(ThinVec<O>),
146 Error(E),
147}
148149#[derive(#[automatically_derived]
impl ::core::clone::Clone for ObligationTreeId {
#[inline]
fn clone(&self) -> ObligationTreeId {
let _: ::core::clone::AssertParamIsClone<usize>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ObligationTreeId { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for ObligationTreeId {
#[inline]
fn eq(&self, other: &ObligationTreeId) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ObligationTreeId {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<usize>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for ObligationTreeId {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash, #[automatically_derived]
impl ::core::fmt::Debug for ObligationTreeId {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ObligationTreeId", &&self.0)
}
}Debug)]
150struct ObligationTreeId(usize);
151152pub struct ObligationForest<O: ForestObligation> {
153/// The list of obligations. In between calls to [Self::process_obligations],
154 /// this list only contains nodes in the `Pending` or `Waiting` state.
155 ///
156 /// `usize` indices are used here and throughout this module, rather than
157 /// [`rustc_index::newtype_index!`] indices, because this code is hot enough
158 /// that the `u32`-to-`usize` conversions that would be required are
159 /// significant, and space considerations are not important.
160nodes: Vec<Node<O>>,
161162/// A cache of predicates that have been successfully completed.
163done_cache: FxHashSet<O::CacheKey>,
164165/// A cache of the nodes in `nodes`, indexed by predicate. Unfortunately,
166 /// its contents are not guaranteed to match those of `nodes`. See the
167 /// comments in `Self::process_obligation` for details.
168active_cache: FxHashMap<O::CacheKey, usize>,
169170/// A vector reused in [Self::compress()] and [Self::find_cycles_from_node()],
171 /// to avoid allocating new vectors.
172reused_node_vec: Vec<usize>,
173174 obligation_tree_id_generator: ObligationTreeIdGenerator,
175176/// Per tree error cache. This is used to deduplicate errors,
177 /// which is necessary to avoid trait resolution overflow in
178 /// some cases.
179 ///
180 /// See [this][details] for details.
181 ///
182 /// [details]: https://github.com/rust-lang/rust/pull/53255#issuecomment-421184780
183error_cache: FxHashMap<ObligationTreeId, FxHashSet<O::CacheKey>>,
184}
185186#[derive(#[automatically_derived]
impl<O: ::core::fmt::Debug> ::core::fmt::Debug for Node<O> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f, "Node",
"obligation", &self.obligation, "state", &self.state,
"dependents", &self.dependents, "has_parent", &self.has_parent,
"obligation_tree_id", &&self.obligation_tree_id)
}
}Debug)]
187struct Node<O> {
188 obligation: O,
189 state: Cell<NodeState>,
190191/// Obligations that depend on this obligation for their completion. They
192 /// must all be in a non-pending state.
193dependents: Vec<usize>,
194195/// If true, `dependents[0]` points to a "parent" node, which requires
196 /// special treatment upon error but is otherwise treated the same.
197 /// (It would be more idiomatic to store the parent node in a separate
198 /// `Option<usize>` field, but that slows down the common case of
199 /// iterating over the parent and other descendants together.)
200has_parent: bool,
201202/// Identifier of the obligation tree to which this node belongs.
203obligation_tree_id: ObligationTreeId,
204}
205206impl<O> Node<O> {
207fn new(parent: Option<usize>, obligation: O, obligation_tree_id: ObligationTreeId) -> Node<O> {
208Node {
209obligation,
210 state: Cell::new(NodeState::Pending),
211 dependents: if let Some(parent_index) = parent { <[_]>::into_vec(::alloc::boxed::box_new([parent_index]))vec![parent_index] } else { ::alloc::vec::Vec::new()vec![] },
212 has_parent: parent.is_some(),
213obligation_tree_id,
214 }
215 }
216}
217218/// The state of one node in some tree within the forest. This represents the
219/// current state of processing for the obligation (of type `O`) associated
220/// with this node.
221///
222/// The non-`Error` state transitions are as follows.
223/// ```text
224/// (Pre-creation)
225/// |
226/// | register_obligation_at() (called by process_obligations() and
227/// v from outside the crate)
228/// Pending
229/// |
230/// | process_obligations()
231/// v
232/// Success
233/// | ^
234/// | | mark_successes()
235/// | v
236/// | Waiting
237/// |
238/// | process_cycles()
239/// v
240/// Done
241/// |
242/// | compress()
243/// v
244/// (Removed)
245/// ```
246/// The `Error` state can be introduced in several places, via `error_at()`.
247///
248/// Outside of `ObligationForest` methods, nodes should be either `Pending` or
249/// `Waiting`.
250#[derive(#[automatically_derived]
impl ::core::fmt::Debug for NodeState {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
NodeState::Pending => "Pending",
NodeState::Success => "Success",
NodeState::Waiting => "Waiting",
NodeState::Done => "Done",
NodeState::Error => "Error",
})
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for NodeState { }Copy, #[automatically_derived]
impl ::core::clone::Clone for NodeState {
#[inline]
fn clone(&self) -> NodeState { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for NodeState {
#[inline]
fn eq(&self, other: &NodeState) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for NodeState {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {}
}Eq)]
251enum NodeState {
252/// This obligation has not yet been selected successfully. Cannot have
253 /// subobligations.
254Pending,
255256/// This obligation was selected successfully, but may or may not have
257 /// subobligations.
258Success,
259260/// This obligation was selected successfully, but it has a pending
261 /// subobligation.
262Waiting,
263264/// This obligation, along with its subobligations, are complete, and will
265 /// be removed in the next collection.
266Done,
267268/// This obligation was resolved to an error. It will be removed by the
269 /// next compression step.
270Error,
271}
272273/// This trait allows us to have two different Outcome types:
274/// - the normal one that does as little as possible
275/// - one for tests that does some additional work and checking
276pub trait OutcomeTrait {
277type Error;
278type Obligation;
279280fn new() -> Self;
281fn record_completed(&mut self, outcome: &Self::Obligation);
282fn record_error(&mut self, error: Self::Error);
283}
284285#[derive(#[automatically_derived]
impl<O: ::core::fmt::Debug, E: ::core::fmt::Debug> ::core::fmt::Debug for
Outcome<O, E> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "Outcome",
"errors", &&self.errors)
}
}Debug)]
286pub struct Outcome<O, E> {
287/// Backtrace of obligations that were found to be in error.
288pub errors: Vec<Error<O, E>>,
289}
290291impl<O, E> OutcomeTraitfor Outcome<O, E> {
292type Error = Error<O, E>;
293type Obligation = O;
294295fn new() -> Self {
296Self { errors: ::alloc::vec::Vec::new()vec![] }
297 }
298299fn record_completed(&mut self, _outcome: &Self::Obligation) {
300// do nothing
301}
302303fn record_error(&mut self, error: Self::Error) {
304self.errors.push(error)
305 }
306}
307308#[derive(#[automatically_derived]
impl<O: ::core::fmt::Debug, E: ::core::fmt::Debug> ::core::fmt::Debug for
Error<O, E> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Error",
"error", &self.error, "backtrace", &&self.backtrace)
}
}Debug, #[automatically_derived]
impl<O: ::core::cmp::PartialEq, E: ::core::cmp::PartialEq>
::core::cmp::PartialEq for Error<O, E> {
#[inline]
fn eq(&self, other: &Error<O, E>) -> bool {
self.error == other.error && self.backtrace == other.backtrace
}
}PartialEq, #[automatically_derived]
impl<O: ::core::cmp::Eq, E: ::core::cmp::Eq> ::core::cmp::Eq for Error<O, E> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<E>;
let _: ::core::cmp::AssertParamIsEq<Vec<O>>;
}
}Eq)]
309pub struct Error<O, E> {
310pub error: E,
311pub backtrace: Vec<O>,
312}
313314mod helper {
315use super::*;
316pub(super) type ObligationTreeIdGenerator = impl Iterator<Item = ObligationTreeId>;
317impl<O: ForestObligation> ObligationForest<O> {
318#[define_opaque(ObligationTreeIdGenerator)]
319pub fn new() -> ObligationForest<O> {
320ObligationForest {
321 nodes: ::alloc::vec::Vec::new()vec![],
322 done_cache: Default::default(),
323 active_cache: Default::default(),
324 reused_node_vec: ::alloc::vec::Vec::new()vec![],
325 obligation_tree_id_generator: (0..).map(ObligationTreeId),
326 error_cache: Default::default(),
327 }
328 }
329 }
330}
331use helper::*;
332333impl<O: ForestObligation> ObligationForest<O> {
334/// Returns the total number of nodes in the forest that have not
335 /// yet been fully resolved.
336pub fn len(&self) -> usize {
337self.nodes.len()
338 }
339340/// Registers an obligation.
341pub fn register_obligation(&mut self, obligation: O) {
342// Ignore errors here - there is no guarantee of success.
343let _ = self.register_obligation_at(obligation, None);
344 }
345346// Returns Err(()) if we already know this obligation failed.
347fn register_obligation_at(&mut self, obligation: O, parent: Option<usize>) -> Result<(), ()> {
348let cache_key = obligation.as_cache_key();
349if self.done_cache.contains(&cache_key) {
350{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_data_structures/src/obligation_forest/mod.rs:350",
"rustc_data_structures::obligation_forest",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_data_structures/src/obligation_forest/mod.rs"),
::tracing_core::__macro_support::Option::Some(350u32),
::tracing_core::__macro_support::Option::Some("rustc_data_structures::obligation_forest"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("register_obligation_at: ignoring already done obligation: {0:?}",
obligation) as &dyn Value))])
});
} else { ; }
};debug!("register_obligation_at: ignoring already done obligation: {:?}", obligation);
351return Ok(());
352 }
353354match self.active_cache.entry(cache_key) {
355 Entry::Occupied(o) => {
356let node = &mut self.nodes[*o.get()];
357if let Some(parent_index) = parent {
358// If the node is already in `active_cache`, it has already
359 // had its chance to be marked with a parent. So if it's
360 // not already present, just dump `parent` into the
361 // dependents as a non-parent.
362if !node.dependents.contains(&parent_index) {
363node.dependents.push(parent_index);
364 }
365 }
366if let NodeState::Error = node.state.get() { Err(()) } else { Ok(()) }
367 }
368 Entry::Vacant(v) => {
369let obligation_tree_id = match parent {
370Some(parent_index) => self.nodes[parent_index].obligation_tree_id,
371None => self.obligation_tree_id_generator.next().unwrap(),
372 };
373374let already_failed = parent.is_some()
375 && self376 .error_cache
377 .get(&obligation_tree_id)
378 .is_some_and(|errors| errors.contains(v.key()));
379380if already_failed {
381Err(())
382 } else {
383let new_index = self.nodes.len();
384v.insert(new_index);
385self.nodes.push(Node::new(parent, obligation, obligation_tree_id));
386Ok(())
387 }
388 }
389 }
390 }
391392/// Converts all remaining obligations to the given error.
393pub fn to_errors<E: Clone>(&mut self, error: E) -> Vec<Error<O, E>> {
394let errors = self395 .nodes
396 .iter()
397 .enumerate()
398 .filter(|(_index, node)| node.state.get() == NodeState::Pending)
399 .map(|(index, _node)| Error { error: error.clone(), backtrace: self.error_at(index) })
400 .collect();
401402self.compress(|_| if !false { ::core::panicking::panic("assertion failed: false") }assert!(false));
403errors404 }
405406/// Returns the set of obligations that are in a pending state.
407pub fn map_pending_obligations<P, F, R>(&self, f: F) -> R
408where
409F: Fn(&O) -> P,
410 R: FromIterator<P>,
411 {
412self.nodes
413 .iter()
414 .filter(|node| node.state.get() == NodeState::Pending)
415 .map(|node| f(&node.obligation))
416 .collect()
417 }
418419pub fn has_pending_obligations(&self) -> bool {
420self.nodes.iter().any(|node| node.state.get() == NodeState::Pending)
421 }
422423fn insert_into_error_cache(&mut self, index: usize) {
424let node = &self.nodes[index];
425self.error_cache
426 .entry(node.obligation_tree_id)
427 .or_default()
428 .insert(node.obligation.as_cache_key());
429 }
430431/// Performs a fixpoint computation over the obligation list.
432#[inline(never)]
433pub fn process_obligations<P>(&mut self, processor: &mut P) -> P::OUT
434where
435P: ObligationProcessor<Obligation = O>,
436 {
437let mut outcome = P::OUT::new();
438439// Fixpoint computation: we repeat until the inner loop stalls.
440loop {
441let mut has_changed = false;
442443// This is the super fast path for cheap-to-check conditions.
444let mut index =
445processor.skippable_obligations(self.nodes.iter().map(|n| &n.obligation));
446447// Note that the loop body can append new nodes, and those new nodes
448 // will then be processed by subsequent iterations of the loop.
449 //
450 // We can't use an iterator for the loop because `self.nodes` is
451 // appended to and the borrow checker would complain. We also can't use
452 // `for index in 0..self.nodes.len() { ... }` because the range would
453 // be computed with the initial length, and we would miss the appended
454 // nodes. Therefore we use a `while` loop.
455while let Some(node) = self.nodes.get_mut(index) {
456// This is the moderately fast path when the prefix skipping above didn't work out.
457if node.state.get() != NodeState::Pending
458 || !processor.needs_process_obligation(&node.obligation)
459 {
460 index += 1;
461continue;
462 }
463464// `processor.process_obligation` can modify the predicate within
465 // `node.obligation`, and that predicate is the key used for
466 // `self.active_cache`. This means that `self.active_cache` can get
467 // out of sync with `nodes`. It's not very common, but it does
468 // happen, and code in `compress` has to allow for it.
469470 // This code is much less hot.
471match processor.process_obligation(&mut node.obligation) {
472 ProcessResult::Unchanged => {
473// No change in state.
474}
475 ProcessResult::Changed(children) => {
476// We are not (yet) stalled.
477has_changed = true;
478 node.state.set(NodeState::Success);
479480for child in children {
481let st = self.register_obligation_at(child, Some(index));
482if let Err(()) = st {
483// Error already reported - propagate it
484 // to our node.
485self.error_at(index);
486 }
487 }
488 }
489 ProcessResult::Error(err) => {
490 has_changed = true;
491 outcome.record_error(Error { error: err, backtrace: self.error_at(index) });
492 }
493 }
494 index += 1;
495 }
496497// If unchanged, then we saw no successful obligations, which means
498 // there is no point in further iteration. This is based on the
499 // assumption that when trait matching returns `Error` or
500 // `Unchanged`, those results do not affect environmental inference
501 // state. (Note that this will occur if we invoke
502 // `process_obligations` with no pending obligations.)
503if !has_changed {
504break;
505 }
506507self.mark_successes();
508self.process_cycles(processor, &mut outcome);
509self.compress(|obl| outcome.record_completed(obl));
510 }
511512outcome513 }
514515/// Returns a vector of obligations for `p` and all of its
516 /// ancestors, putting them into the error state in the process.
517fn error_at(&self, mut index: usize) -> Vec<O> {
518let mut error_stack: Vec<usize> = ::alloc::vec::Vec::new()vec![];
519let mut trace = ::alloc::vec::Vec::new()vec![];
520521loop {
522let node = &self.nodes[index];
523node.state.set(NodeState::Error);
524trace.push(node.obligation.clone());
525if node.has_parent {
526// The first dependent is the parent, which is treated
527 // specially.
528error_stack.extend(node.dependents.iter().skip(1));
529index = node.dependents[0];
530 } else {
531// No parent; treat all dependents non-specially.
532error_stack.extend(node.dependents.iter());
533break;
534 }
535 }
536537while let Some(index) = error_stack.pop() {
538let node = &self.nodes[index];
539if node.state.get() != NodeState::Error {
540 node.state.set(NodeState::Error);
541 error_stack.extend(node.dependents.iter());
542 }
543 }
544545trace546 }
547548/// Mark all `Waiting` nodes as `Success`, except those that depend on a
549 /// pending node.
550fn mark_successes(&self) {
551// Convert all `Waiting` nodes to `Success`.
552for node in &self.nodes {
553if node.state.get() == NodeState::Waiting {
554 node.state.set(NodeState::Success);
555 }
556 }
557558// Convert `Success` nodes that depend on a pending node back to
559 // `Waiting`.
560for node in &self.nodes {
561if node.state.get() == NodeState::Pending {
562// This call site is hot.
563self.inlined_mark_dependents_as_waiting(node);
564 }
565 }
566 }
567568// This always-inlined function is for the hot call site.
569#[inline(always)]
570fn inlined_mark_dependents_as_waiting(&self, node: &Node<O>) {
571for &index in node.dependents.iter() {
572let node = &self.nodes[index];
573let state = node.state.get();
574if state == NodeState::Success {
575// This call site is cold.
576self.uninlined_mark_dependents_as_waiting(node);
577 } else {
578if true {
if !(state == NodeState::Waiting || state == NodeState::Error) {
::core::panicking::panic("assertion failed: state == NodeState::Waiting || state == NodeState::Error")
};
}debug_assert!(state == NodeState::Waiting || state == NodeState::Error)579 }
580 }
581 }
582583// This never-inlined function is for the cold call site.
584#[inline(never)]
585fn uninlined_mark_dependents_as_waiting(&self, node: &Node<O>) {
586// Mark node Waiting in the cold uninlined code instead of the hot inlined
587node.state.set(NodeState::Waiting);
588self.inlined_mark_dependents_as_waiting(node)
589 }
590591/// Report cycles between all `Success` nodes, and convert all `Success`
592 /// nodes to `Done`. This must be called after `mark_successes`.
593fn process_cycles<P>(&mut self, processor: &mut P, outcome: &mut P::OUT)
594where
595P: ObligationProcessor<Obligation = O>,
596 {
597let mut stack = std::mem::take(&mut self.reused_node_vec);
598for (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.
602if node.state.get() == NodeState::Success {
603self.find_cycles_from_node(&mut stack, processor, index, outcome);
604 }
605 }
606607if true {
if !stack.is_empty() {
::core::panicking::panic("assertion failed: stack.is_empty()")
};
};debug_assert!(stack.is_empty());
608self.reused_node_vec = stack;
609 }
610611fn find_cycles_from_node<P>(
612&self,
613 stack: &mut Vec<usize>,
614 processor: &mut P,
615 index: usize,
616 outcome: &mut P::OUT,
617 ) where
618P: ObligationProcessor<Obligation = O>,
619 {
620let node = &self.nodes[index];
621if node.state.get() == NodeState::Success {
622match stack.iter().rposition(|&n| n == index) {
623None => {
624stack.push(index);
625for &dep_index in node.dependents.iter() {
626self.find_cycles_from_node(stack, processor, dep_index, outcome);
627 }
628stack.pop();
629node.state.set(NodeState::Done);
630 }
631Some(rpos) => {
632// Cycle detected.
633let result = processor.process_backedge(
634stack[rpos..].iter().map(|&i| &self.nodes[i].obligation),
635PhantomData,
636 );
637if let Err(err) = result {
638outcome.record_error(Error { error: err, backtrace: self.error_at(index) });
639 }
640 }
641 }
642 }
643 }
644645/// Compresses the vector, removing all popped nodes. This adjusts the
646 /// indices and hence invalidates any outstanding indices. `process_cycles`
647 /// must be run beforehand to remove any cycles on `Success` nodes.
648#[inline(never)]
649fn compress(&mut self, mut outcome_cb: impl FnMut(&O)) {
650let orig_nodes_len = self.nodes.len();
651let mut node_rewrites: Vec<_> = std::mem::take(&mut self.reused_node_vec);
652if true {
if !node_rewrites.is_empty() {
::core::panicking::panic("assertion failed: node_rewrites.is_empty()")
};
};debug_assert!(node_rewrites.is_empty());
653node_rewrites.extend(0..orig_nodes_len);
654let mut dead_nodes = 0;
655656// Move removable nodes to the end, preserving the order of the
657 // remaining nodes.
658 //
659 // LOOP INVARIANT:
660 // self.nodes[0..index - dead_nodes] are the first remaining nodes
661 // self.nodes[index - dead_nodes..index] are all dead
662 // self.nodes[index..] are unchanged
663for index in 0..orig_nodes_len {
664let node = &self.nodes[index];
665match node.state.get() {
666 NodeState::Pending | NodeState::Waiting => {
667if dead_nodes > 0 {
668self.nodes.swap(index, index - dead_nodes);
669 node_rewrites[index] -= dead_nodes;
670 }
671 }
672 NodeState::Done => {
673// The removal lookup might fail because the contents of
674 // `self.active_cache` are not guaranteed to match those of
675 // `self.nodes`. See the comment in `process_obligation`
676 // for more details.
677let cache_key = node.obligation.as_cache_key();
678self.active_cache.remove(&cache_key);
679self.done_cache.insert(cache_key);
680681// Extract the success stories.
682outcome_cb(&node.obligation);
683 node_rewrites[index] = orig_nodes_len;
684 dead_nodes += 1;
685 }
686 NodeState::Error => {
687// We *intentionally* remove the node from the cache at this point. Otherwise
688 // tests must come up with a different type on every type error they
689 // check against.
690self.active_cache.remove(&node.obligation.as_cache_key());
691self.insert_into_error_cache(index);
692 node_rewrites[index] = orig_nodes_len;
693 dead_nodes += 1;
694 }
695 NodeState::Success => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
696 }
697 }
698699if dead_nodes > 0 {
700// Remove the dead nodes and rewrite indices.
701self.nodes.truncate(orig_nodes_len - dead_nodes);
702self.apply_rewrites(&node_rewrites);
703 }
704705node_rewrites.truncate(0);
706self.reused_node_vec = node_rewrites;
707 }
708709#[inline(never)]
710fn apply_rewrites(&mut self, node_rewrites: &[usize]) {
711let orig_nodes_len = node_rewrites.len();
712713for node in &mut self.nodes {
714let mut i = 0;
715while let Some(dependent) = node.dependents.get_mut(i) {
716let new_index = node_rewrites[*dependent];
717if new_index >= orig_nodes_len {
718 node.dependents.swap_remove(i);
719if i == 0 && node.has_parent {
720// We just removed the parent.
721node.has_parent = false;
722 }
723 } else {
724*dependent = new_index;
725 i += 1;
726 }
727 }
728 }
729730// This updating of `self.active_cache` is necessary because the
731 // removal of nodes within `compress` can fail. See above.
732self.active_cache.retain(|_predicate, index| {
733let new_index = node_rewrites[*index];
734if new_index >= orig_nodes_len {
735false
736} else {
737*index = new_index;
738true
739}
740 });
741 }
742}