1 use crate::infer::{InferCtxt, TyOrConstInferVar};
2 use rustc_data_structures::fx::FxHashMap;
3 use rustc_data_structures::obligation_forest::ProcessResult;
4 use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome};
5 use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
6 use rustc_errors::ErrorReported;
7 use rustc_infer::traits::{SelectionError, TraitEngine, TraitEngineExt as _, TraitObligation};
8 use rustc_middle::mir::interpret::ErrorHandled;
9 use rustc_middle::thir::abstract_const::NotConstEvaluatable;
10 use rustc_middle::ty::error::{ExpectedFound, TypeError};
11 use rustc_middle::ty::subst::SubstsRef;
12 use rustc_middle::ty::ToPredicate;
13 use rustc_middle::ty::{self, Binder, Const, Ty, TypeFoldable};
14 use std::marker::PhantomData;
16 use super::const_evaluatable;
18 use super::select::SelectionContext;
20 use super::CodeAmbiguity;
21 use super::CodeProjectionError;
22 use super::CodeSelectionError;
23 use super::Unimplemented;
24 use super::{FulfillmentError, FulfillmentErrorCode};
25 use super::{ObligationCause, PredicateObligation};
27 use crate::traits::error_reporting::InferCtxtExt as _;
28 use crate::traits::project::PolyProjectionObligation;
29 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
31 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
32 /// Note that we include both the `ParamEnv` and the `Predicate`,
33 /// as the `ParamEnv` can influence whether fulfillment succeeds
35 type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
37 fn as_cache_key(&self) -> Self::CacheKey {
38 self.obligation.param_env.and(self.obligation.predicate)
42 /// The fulfillment context is used to drive trait resolution. It
43 /// consists of a list of obligations that must be (eventually)
44 /// satisfied. The job is to track which are satisfied, which yielded
45 /// errors, and which are still pending. At any point, users can call
46 /// `select_where_possible`, and the fulfillment context will try to do
47 /// selection, retaining only those obligations that remain
48 /// ambiguous. This may be helpful in pushing type inference
49 /// along. Once all type inference constraints have been generated, the
50 /// method `select_all_or_error` can be used to report any remaining
51 /// ambiguous cases as errors.
52 pub struct FulfillmentContext<'tcx> {
53 // A list of all obligations that have been registered with this
54 // fulfillment context.
55 predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
57 relationships: FxHashMap<ty::TyVid, ty::FoundRelationships>,
59 // Should this fulfillment context register type-lives-for-region
60 // obligations on its parent infcx? In some cases, region
61 // obligations are either already known to hold (normalization) or
62 // hopefully verifed elsewhere (type-impls-bound), and therefore
63 // should not be checked.
65 // Note that if we are normalizing a type that we already
66 // know is well-formed, there should be no harm setting this
67 // to true - all the region variables should be determinable
68 // using the RFC 447 rules, which don't depend on
69 // type-lives-for-region constraints, and because the type
70 // is well-formed, the constraints should hold.
71 register_region_obligations: bool,
72 // Is it OK to register obligations into this infcx inside
75 // The "primary fulfillment" in many cases in typeck lives
76 // outside of any snapshot, so any use of it inside a snapshot
77 // will lead to trouble and therefore is checked against, but
78 // other fulfillment contexts sometimes do live inside of
79 // a snapshot (they don't *straddle* a snapshot, so there
80 // is no trouble there).
81 usable_in_snapshot: bool,
84 #[derive(Clone, Debug)]
85 pub struct PendingPredicateObligation<'tcx> {
86 pub obligation: PredicateObligation<'tcx>,
87 // This is far more often read than modified, meaning that we
88 // should mostly optimize for reading speed, while modifying is not as relevant.
90 // For whatever reason using a boxed slice is slower than using a `Vec` here.
91 pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
94 // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
95 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
96 static_assert_size!(PendingPredicateObligation<'_>, 56);
98 impl<'a, 'tcx> FulfillmentContext<'tcx> {
99 /// Creates a new fulfillment context.
100 pub fn new() -> FulfillmentContext<'tcx> {
102 predicates: ObligationForest::new(),
103 relationships: FxHashMap::default(),
104 register_region_obligations: true,
105 usable_in_snapshot: false,
109 pub fn new_in_snapshot() -> FulfillmentContext<'tcx> {
111 predicates: ObligationForest::new(),
112 relationships: FxHashMap::default(),
113 register_region_obligations: true,
114 usable_in_snapshot: true,
118 pub fn new_ignoring_regions() -> FulfillmentContext<'tcx> {
120 predicates: ObligationForest::new(),
121 relationships: FxHashMap::default(),
122 register_region_obligations: false,
123 usable_in_snapshot: false,
127 /// Attempts to select obligations using `selcx`.
128 fn select(&mut self, selcx: &mut SelectionContext<'a, 'tcx>) -> Vec<FulfillmentError<'tcx>> {
129 let span = debug_span!("select", obligation_forest_size = ?self.predicates.len());
130 let _enter = span.enter();
132 let mut errors = Vec::new();
135 debug!("select: starting another iteration");
137 // Process pending obligations.
138 let outcome: Outcome<_, _> =
139 self.predicates.process_obligations(&mut FulfillProcessor {
141 register_region_obligations: self.register_region_obligations,
143 debug!("select: outcome={:#?}", outcome);
145 // FIXME: if we kept the original cache key, we could mark projection
146 // obligations as complete for the projection cache here.
148 errors.extend(outcome.errors.into_iter().map(to_fulfillment_error));
150 // If nothing new was added, no need to keep looping.
157 "select({} predicates remaining, {} errors) done",
158 self.predicates.len(),
166 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
167 /// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
168 /// creating a fresh type variable `$0` as well as a projection
169 /// predicate `<SomeType as SomeTrait>::X == $0`. When the
170 /// inference engine runs, it will attempt to find an impl of
171 /// `SomeTrait` or a where-clause that lets us unify `$0` with
172 /// something concrete. If this fails, we'll unify `$0` with
173 /// `projection_ty` again.
174 #[tracing::instrument(level = "debug", skip(self, infcx, param_env, cause))]
175 fn normalize_projection_type(
177 infcx: &InferCtxt<'_, 'tcx>,
178 param_env: ty::ParamEnv<'tcx>,
179 projection_ty: ty::ProjectionTy<'tcx>,
180 cause: ObligationCause<'tcx>,
182 debug_assert!(!projection_ty.has_escaping_bound_vars());
184 // FIXME(#20304) -- cache
186 let mut selcx = SelectionContext::new(infcx);
187 let mut obligations = vec![];
188 let normalized_ty = project::normalize_projection_type(
196 self.register_predicate_obligations(infcx, obligations);
198 debug!(?normalized_ty);
203 fn register_predicate_obligation(
205 infcx: &InferCtxt<'_, 'tcx>,
206 obligation: PredicateObligation<'tcx>,
208 // this helps to reduce duplicate errors, as well as making
209 // debug output much nicer to read and so on.
210 let obligation = infcx.resolve_vars_if_possible(obligation);
212 debug!(?obligation, "register_predicate_obligation");
214 assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
216 super::relationships::update(self, infcx, &obligation);
219 .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
222 fn select_all_or_error(&mut self, infcx: &InferCtxt<'_, 'tcx>) -> Vec<FulfillmentError<'tcx>> {
224 let errors = self.select_where_possible(infcx);
225 if !errors.is_empty() {
230 self.predicates.to_errors(CodeAmbiguity).into_iter().map(to_fulfillment_error).collect()
233 fn select_where_possible(
235 infcx: &InferCtxt<'_, 'tcx>,
236 ) -> Vec<FulfillmentError<'tcx>> {
237 let mut selcx = SelectionContext::new(infcx);
238 self.select(&mut selcx)
241 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
242 self.predicates.map_pending_obligations(|o| o.obligation.clone())
245 fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships> {
246 &mut self.relationships
250 struct FulfillProcessor<'a, 'b, 'tcx> {
251 selcx: &'a mut SelectionContext<'b, 'tcx>,
252 register_region_obligations: bool,
255 fn mk_pending(os: Vec<PredicateObligation<'tcx>>) -> Vec<PendingPredicateObligation<'tcx>> {
257 .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
261 impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
262 type Obligation = PendingPredicateObligation<'tcx>;
263 type Error = FulfillmentErrorCode<'tcx>;
265 /// Processes a predicate obligation and returns either:
266 /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
267 /// - `Unchanged` if we don't have enough info to be sure
268 /// - `Error(e)` if the predicate does not hold
270 /// This is always inlined, despite its size, because it has a single
271 /// callsite and it is called *very* frequently.
273 fn process_obligation(
275 pending_obligation: &mut Self::Obligation,
276 ) -> ProcessResult<Self::Obligation, Self::Error> {
277 // If we were stalled on some unresolved variables, first check whether
278 // any of them have been resolved; if not, don't bother doing more work
280 let change = match pending_obligation.stalled_on.len() {
281 // Match arms are in order of frequency, which matters because this
282 // code is so hot. 1 and 0 dominate; 2+ is fairly rare.
284 let infer_var = pending_obligation.stalled_on[0];
285 self.selcx.infcx().ty_or_const_infer_var_changed(infer_var)
288 // In this case we haven't changed, but wish to make a change.
292 // This `for` loop was once a call to `all()`, but this lower-level
293 // form was a perf win. See #64545 for details.
295 for &infer_var in &pending_obligation.stalled_on {
296 if self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) {
307 "process_predicate: pending obligation {:?} still stalled on {:?}",
308 self.selcx.infcx().resolve_vars_if_possible(pending_obligation.obligation.clone()),
309 pending_obligation.stalled_on
311 return ProcessResult::Unchanged;
314 self.progress_changed_obligations(pending_obligation)
317 fn process_backedge<'c, I>(
320 _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
322 I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
324 if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
325 debug!("process_child_obligations: coinductive match");
327 let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
328 self.selcx.infcx().report_overflow_error_cycle(&cycle);
333 impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
334 // The code calling this method is extremely hot and only rarely
335 // actually uses this, so move this part of the code
338 fn progress_changed_obligations(
340 pending_obligation: &mut PendingPredicateObligation<'tcx>,
341 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
342 pending_obligation.stalled_on.truncate(0);
344 let obligation = &mut pending_obligation.obligation;
346 if obligation.predicate.has_infer_types_or_consts() {
347 obligation.predicate =
348 self.selcx.infcx().resolve_vars_if_possible(obligation.predicate);
351 debug!(?obligation, ?obligation.cause, "process_obligation");
353 let infcx = self.selcx.infcx();
355 let binder = obligation.predicate.kind();
356 match binder.no_bound_vars() {
357 None => match binder.skip_binder() {
358 // Evaluation will discard candidates using the leak check.
359 // This means we need to pass it the bound version of our
361 ty::PredicateKind::Trait(trait_ref) => {
362 let trait_obligation = obligation.with(binder.rebind(trait_ref));
364 self.process_trait_obligation(
367 &mut pending_obligation.stalled_on,
370 ty::PredicateKind::Projection(data) => {
371 let project_obligation = obligation.with(binder.rebind(data));
373 self.process_projection_obligation(
376 &mut pending_obligation.stalled_on,
379 ty::PredicateKind::RegionOutlives(_)
380 | ty::PredicateKind::TypeOutlives(_)
381 | ty::PredicateKind::WellFormed(_)
382 | ty::PredicateKind::ObjectSafe(_)
383 | ty::PredicateKind::ClosureKind(..)
384 | ty::PredicateKind::Subtype(_)
385 | ty::PredicateKind::Coerce(_)
386 | ty::PredicateKind::ConstEvaluatable(..)
387 | ty::PredicateKind::ConstEquate(..) => {
389 ty::Binder::dummy(infcx.replace_bound_vars_with_placeholders(binder));
390 ProcessResult::Changed(mk_pending(vec![
391 obligation.with(pred.to_predicate(self.selcx.tcx())),
394 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
395 bug!("TypeWellFormedFromEnv is only used for Chalk")
398 Some(pred) => match pred {
399 ty::PredicateKind::Trait(data) => {
400 let trait_obligation = obligation.with(Binder::dummy(data));
402 self.process_trait_obligation(
405 &mut pending_obligation.stalled_on,
409 ty::PredicateKind::RegionOutlives(data) => {
410 match infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data)) {
411 Ok(()) => ProcessResult::Changed(vec![]),
412 Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)),
416 ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t_a, r_b)) => {
417 if self.register_region_obligations {
418 self.selcx.infcx().register_region_obligation_with_cause(
424 ProcessResult::Changed(vec![])
427 ty::PredicateKind::Projection(ref data) => {
428 let project_obligation = obligation.with(Binder::dummy(*data));
430 self.process_projection_obligation(
433 &mut pending_obligation.stalled_on,
437 ty::PredicateKind::ObjectSafe(trait_def_id) => {
438 if !self.selcx.tcx().is_object_safe(trait_def_id) {
439 ProcessResult::Error(CodeSelectionError(Unimplemented))
441 ProcessResult::Changed(vec![])
445 ty::PredicateKind::ClosureKind(_, closure_substs, kind) => {
446 match self.selcx.infcx().closure_kind(closure_substs) {
447 Some(closure_kind) => {
448 if closure_kind.extends(kind) {
449 ProcessResult::Changed(vec![])
451 ProcessResult::Error(CodeSelectionError(Unimplemented))
454 None => ProcessResult::Unchanged,
458 ty::PredicateKind::WellFormed(arg) => {
459 match wf::obligations(
461 obligation.param_env,
462 obligation.cause.body_id,
463 obligation.recursion_depth + 1,
465 obligation.cause.span,
468 pending_obligation.stalled_on =
469 vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
470 ProcessResult::Unchanged
472 Some(os) => ProcessResult::Changed(mk_pending(os)),
476 ty::PredicateKind::Subtype(subtype) => {
477 match self.selcx.infcx().subtype_predicate(
479 obligation.param_env,
480 Binder::dummy(subtype),
483 // None means that both are unresolved.
484 pending_obligation.stalled_on = vec![
485 TyOrConstInferVar::maybe_from_ty(subtype.a).unwrap(),
486 TyOrConstInferVar::maybe_from_ty(subtype.b).unwrap(),
488 ProcessResult::Unchanged
490 Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
493 ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
494 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
502 ty::PredicateKind::Coerce(coerce) => {
503 match self.selcx.infcx().coerce_predicate(
505 obligation.param_env,
506 Binder::dummy(coerce),
509 // None means that both are unresolved.
510 pending_obligation.stalled_on = vec![
511 TyOrConstInferVar::maybe_from_ty(coerce.a).unwrap(),
512 TyOrConstInferVar::maybe_from_ty(coerce.b).unwrap(),
514 ProcessResult::Unchanged
516 Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
518 let expected_found = ExpectedFound::new(false, coerce.a, coerce.b);
519 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
527 ty::PredicateKind::ConstEvaluatable(uv) => {
528 match const_evaluatable::is_const_evaluatable(
531 obligation.param_env,
532 obligation.cause.span,
534 Ok(()) => ProcessResult::Changed(vec![]),
535 Err(NotConstEvaluatable::MentionsInfer) => {
536 pending_obligation.stalled_on.clear();
537 pending_obligation.stalled_on.extend(
540 .filter_map(TyOrConstInferVar::maybe_from_generic_arg),
542 ProcessResult::Unchanged
545 e @ NotConstEvaluatable::MentionsParam
546 | e @ NotConstEvaluatable::Error(_),
547 ) => ProcessResult::Error(CodeSelectionError(
548 SelectionError::NotConstEvaluatable(e),
553 ty::PredicateKind::ConstEquate(c1, c2) => {
554 debug!(?c1, ?c2, "equating consts");
555 let tcx = self.selcx.tcx();
556 if tcx.features().generic_const_exprs {
557 // FIXME: we probably should only try to unify abstract constants
558 // if the constants depend on generic parameters.
560 // Let's just see where this breaks :shrug:
561 if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
564 if infcx.try_unify_abstract_consts(a.shrink(), b.shrink()) {
565 return ProcessResult::Changed(vec![]);
570 let stalled_on = &mut pending_obligation.stalled_on;
572 let mut evaluate = |c: &'tcx Const<'tcx>| {
573 if let ty::ConstKind::Unevaluated(unevaluated) = c.val {
574 match self.selcx.infcx().const_eval_resolve(
575 obligation.param_env,
577 Some(obligation.cause.span),
579 Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)),
580 Err(ErrorHandled::TooGeneric) => {
585 .filter_map(TyOrConstInferVar::maybe_from_generic_arg),
587 Err(ErrorHandled::TooGeneric)
589 Err(err) => Err(err),
596 match (evaluate(c1), evaluate(c2)) {
597 (Ok(c1), Ok(c2)) => {
601 .at(&obligation.cause, obligation.param_env)
604 Ok(_) => ProcessResult::Changed(vec![]),
605 Err(err) => ProcessResult::Error(
606 FulfillmentErrorCode::CodeConstEquateError(
607 ExpectedFound::new(true, c1, c2),
613 (Err(ErrorHandled::Reported(ErrorReported)), _)
614 | (_, Err(ErrorHandled::Reported(ErrorReported))) => ProcessResult::Error(
615 CodeSelectionError(SelectionError::NotConstEvaluatable(
616 NotConstEvaluatable::Error(ErrorReported),
619 (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => {
621 obligation.cause.span(self.selcx.tcx()),
622 "ConstEquate: const_eval_resolve returned an unexpected error"
625 (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
626 if c1.has_infer_types_or_consts() || c2.has_infer_types_or_consts() {
627 ProcessResult::Unchanged
629 // Two different constants using generic parameters ~> error.
630 let expected_found = ExpectedFound::new(true, c1, c2);
631 ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError(
633 TypeError::ConstMismatch(expected_found),
639 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
640 bug!("TypeWellFormedFromEnv is only used for Chalk")
646 #[instrument(level = "debug", skip(self, obligation, stalled_on))]
647 fn process_trait_obligation(
649 obligation: &PredicateObligation<'tcx>,
650 trait_obligation: TraitObligation<'tcx>,
651 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
652 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
653 let infcx = self.selcx.infcx();
654 if obligation.predicate.is_known_global() {
655 // no type variables present, can use evaluation for better caching.
656 // FIXME: consider caching errors too.
657 if infcx.predicate_must_hold_considering_regions(obligation) {
659 "selecting trait at depth {} evaluated to holds",
660 obligation.recursion_depth
662 return ProcessResult::Changed(vec![]);
666 match self.selcx.select(&trait_obligation) {
667 Ok(Some(impl_source)) => {
668 debug!("selecting trait at depth {} yielded Ok(Some)", obligation.recursion_depth);
669 ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))
672 debug!("selecting trait at depth {} yielded Ok(None)", obligation.recursion_depth);
674 // This is a bit subtle: for the most part, the
675 // only reason we can fail to make progress on
676 // trait selection is because we don't have enough
677 // information about the types in the trait.
679 stalled_on.extend(substs_infer_vars(
681 trait_obligation.predicate.map_bound(|pred| pred.trait_ref.substs),
685 "process_predicate: pending obligation {:?} now stalled on {:?}",
686 infcx.resolve_vars_if_possible(obligation.clone()),
690 ProcessResult::Unchanged
692 Err(selection_err) => {
693 debug!("selecting trait at depth {} yielded Err", obligation.recursion_depth);
695 ProcessResult::Error(CodeSelectionError(selection_err))
700 fn process_projection_obligation(
702 obligation: &PredicateObligation<'tcx>,
703 project_obligation: PolyProjectionObligation<'tcx>,
704 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
705 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
706 let tcx = self.selcx.tcx();
708 if obligation.predicate.is_global(tcx) {
709 // no type variables present, can use evaluation for better caching.
710 // FIXME: consider caching errors too.
711 if self.selcx.infcx().predicate_must_hold_considering_regions(obligation) {
712 return ProcessResult::Changed(vec![]);
714 tracing::debug!("Does NOT hold: {:?}", obligation);
718 match project::poly_project_and_unify_type(self.selcx, &project_obligation) {
719 Ok(Ok(Some(os))) => ProcessResult::Changed(mk_pending(os)),
722 stalled_on.extend(substs_infer_vars(
724 project_obligation.predicate.map_bound(|pred| pred.projection_ty.substs),
726 ProcessResult::Unchanged
728 // Let the caller handle the recursion
729 Ok(Err(project::InProgress)) => ProcessResult::Changed(mk_pending(vec![
730 project_obligation.with(project_obligation.predicate.to_predicate(tcx)),
732 Err(e) => ProcessResult::Error(CodeProjectionError(e)),
737 /// Returns the set of inference variables contained in `substs`.
738 fn substs_infer_vars<'a, 'tcx>(
739 selcx: &mut SelectionContext<'a, 'tcx>,
740 substs: ty::Binder<'tcx, SubstsRef<'tcx>>,
741 ) -> impl Iterator<Item = TyOrConstInferVar<'tcx>> {
742 let tcx = selcx.tcx();
745 .resolve_vars_if_possible(substs)
746 .skip_binder() // ok because this check doesn't care about regions
748 .filter(|arg| arg.has_infer_types_or_consts())
749 .flat_map(move |arg| {
750 let mut walker = arg.walk(tcx);
751 while let Some(c) = walker.next() {
752 if !c.has_infer_types_or_consts() {
753 walker.visited.remove(&c);
754 walker.skip_current_subtree();
757 walker.visited.into_iter()
759 .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
762 fn to_fulfillment_error<'tcx>(
763 error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
764 ) -> FulfillmentError<'tcx> {
765 let mut iter = error.backtrace.into_iter();
766 let obligation = iter.next().unwrap().obligation;
767 // The root obligation is the last item in the backtrace - if there's only
768 // one item, then it's the same as the main obligation
769 let root_obligation = iter.next_back().map_or_else(|| obligation.clone(), |e| e.obligation);
770 FulfillmentError::new(obligation, error.error, root_obligation)