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_infer::traits::ProjectionCacheKey;
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;
17 use super::project::{self, ProjectAndUnifyResult};
18 use super::select::SelectionContext;
20 use super::CodeAmbiguity;
21 use super::CodeProjectionError;
22 use super::CodeSelectionError;
23 use super::EvaluationResult;
24 use super::Unimplemented;
25 use super::{FulfillmentError, FulfillmentErrorCode};
26 use super::{ObligationCause, PredicateObligation};
28 use crate::traits::error_reporting::InferCtxtExt as _;
29 use crate::traits::project::PolyProjectionObligation;
30 use crate::traits::project::ProjectionCacheKeyExt as _;
31 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
33 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
34 /// Note that we include both the `ParamEnv` and the `Predicate`,
35 /// as the `ParamEnv` can influence whether fulfillment succeeds
37 type CacheKey = ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>;
39 fn as_cache_key(&self) -> Self::CacheKey {
40 self.obligation.param_env.and(self.obligation.predicate)
44 /// The fulfillment context is used to drive trait resolution. It
45 /// consists of a list of obligations that must be (eventually)
46 /// satisfied. The job is to track which are satisfied, which yielded
47 /// errors, and which are still pending. At any point, users can call
48 /// `select_where_possible`, and the fulfillment context will try to do
49 /// selection, retaining only those obligations that remain
50 /// ambiguous. This may be helpful in pushing type inference
51 /// along. Once all type inference constraints have been generated, the
52 /// method `select_all_or_error` can be used to report any remaining
53 /// ambiguous cases as errors.
54 pub struct FulfillmentContext<'tcx> {
55 // A list of all obligations that have been registered with this
56 // fulfillment context.
57 predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
59 relationships: FxHashMap<ty::TyVid, ty::FoundRelationships>,
61 // Should this fulfillment context register type-lives-for-region
62 // obligations on its parent infcx? In some cases, region
63 // obligations are either already known to hold (normalization) or
64 // hopefully verified elsewhere (type-impls-bound), and therefore
65 // should not be checked.
67 // Note that if we are normalizing a type that we already
68 // know is well-formed, there should be no harm setting this
69 // to true - all the region variables should be determinable
70 // using the RFC 447 rules, which don't depend on
71 // type-lives-for-region constraints, and because the type
72 // is well-formed, the constraints should hold.
73 register_region_obligations: bool,
74 // Is it OK to register obligations into this infcx inside
77 // The "primary fulfillment" in many cases in typeck lives
78 // outside of any snapshot, so any use of it inside a snapshot
79 // will lead to trouble and therefore is checked against, but
80 // other fulfillment contexts sometimes do live inside of
81 // a snapshot (they don't *straddle* a snapshot, so there
82 // is no trouble there).
83 usable_in_snapshot: bool,
86 #[derive(Clone, Debug)]
87 pub struct PendingPredicateObligation<'tcx> {
88 pub obligation: PredicateObligation<'tcx>,
89 // This is far more often read than modified, meaning that we
90 // should mostly optimize for reading speed, while modifying is not as relevant.
92 // For whatever reason using a boxed slice is slower than using a `Vec` here.
93 pub stalled_on: Vec<TyOrConstInferVar<'tcx>>,
96 // `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
97 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
98 static_assert_size!(PendingPredicateObligation<'_>, 72);
100 impl<'a, 'tcx> FulfillmentContext<'tcx> {
101 /// Creates a new fulfillment context.
102 pub fn new() -> FulfillmentContext<'tcx> {
104 predicates: ObligationForest::new(),
105 relationships: FxHashMap::default(),
106 register_region_obligations: true,
107 usable_in_snapshot: false,
111 pub fn new_in_snapshot() -> FulfillmentContext<'tcx> {
113 predicates: ObligationForest::new(),
114 relationships: FxHashMap::default(),
115 register_region_obligations: true,
116 usable_in_snapshot: true,
120 pub fn new_ignoring_regions() -> FulfillmentContext<'tcx> {
122 predicates: ObligationForest::new(),
123 relationships: FxHashMap::default(),
124 register_region_obligations: false,
125 usable_in_snapshot: false,
129 /// Attempts to select obligations using `selcx`.
130 fn select(&mut self, selcx: &mut SelectionContext<'a, 'tcx>) -> Vec<FulfillmentError<'tcx>> {
131 let span = debug_span!("select", obligation_forest_size = ?self.predicates.len());
132 let _enter = span.enter();
134 let mut errors = Vec::new();
137 debug!("select: starting another iteration");
139 // Process pending obligations.
140 let outcome: Outcome<_, _> =
141 self.predicates.process_obligations(&mut FulfillProcessor {
143 register_region_obligations: self.register_region_obligations,
145 debug!("select: outcome={:#?}", outcome);
147 // FIXME: if we kept the original cache key, we could mark projection
148 // obligations as complete for the projection cache here.
150 errors.extend(outcome.errors.into_iter().map(to_fulfillment_error));
152 // If nothing new was added, no need to keep looping.
159 "select({} predicates remaining, {} errors) done",
160 self.predicates.len(),
168 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
169 /// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
170 /// creating a fresh type variable `$0` as well as a projection
171 /// predicate `<SomeType as SomeTrait>::X == $0`. When the
172 /// inference engine runs, it will attempt to find an impl of
173 /// `SomeTrait` or a where-clause that lets us unify `$0` with
174 /// something concrete. If this fails, we'll unify `$0` with
175 /// `projection_ty` again.
176 #[tracing::instrument(level = "debug", skip(self, infcx, param_env, cause))]
177 fn normalize_projection_type(
179 infcx: &InferCtxt<'_, 'tcx>,
180 param_env: ty::ParamEnv<'tcx>,
181 projection_ty: ty::ProjectionTy<'tcx>,
182 cause: ObligationCause<'tcx>,
184 debug_assert!(!projection_ty.has_escaping_bound_vars());
186 // FIXME(#20304) -- cache
188 let mut selcx = SelectionContext::new(infcx);
189 let mut obligations = vec![];
190 let normalized_ty = project::normalize_projection_type(
198 self.register_predicate_obligations(infcx, obligations);
200 debug!(?normalized_ty);
202 normalized_ty.ty().unwrap()
205 fn register_predicate_obligation(
207 infcx: &InferCtxt<'_, 'tcx>,
208 obligation: PredicateObligation<'tcx>,
210 // this helps to reduce duplicate errors, as well as making
211 // debug output much nicer to read and so on.
212 let obligation = infcx.resolve_vars_if_possible(obligation);
214 debug!(?obligation, "register_predicate_obligation");
216 assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
218 super::relationships::update(self, infcx, &obligation);
221 .register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
224 fn select_all_or_error(&mut self, infcx: &InferCtxt<'_, 'tcx>) -> Vec<FulfillmentError<'tcx>> {
226 let errors = self.select_where_possible(infcx);
227 if !errors.is_empty() {
232 self.predicates.to_errors(CodeAmbiguity).into_iter().map(to_fulfillment_error).collect()
235 fn select_where_possible(
237 infcx: &InferCtxt<'_, 'tcx>,
238 ) -> Vec<FulfillmentError<'tcx>> {
239 let mut selcx = SelectionContext::new(infcx);
240 self.select(&mut selcx)
243 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
244 self.predicates.map_pending_obligations(|o| o.obligation.clone())
247 fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships> {
248 &mut self.relationships
252 struct FulfillProcessor<'a, 'b, 'tcx> {
253 selcx: &'a mut SelectionContext<'b, 'tcx>,
254 register_region_obligations: bool,
257 fn mk_pending(os: Vec<PredicateObligation<'_>>) -> Vec<PendingPredicateObligation<'_>> {
259 .map(|o| PendingPredicateObligation { obligation: o, stalled_on: vec![] })
263 impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
264 type Obligation = PendingPredicateObligation<'tcx>;
265 type Error = FulfillmentErrorCode<'tcx>;
267 /// Processes a predicate obligation and returns either:
268 /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
269 /// - `Unchanged` if we don't have enough info to be sure
270 /// - `Error(e)` if the predicate does not hold
272 /// This is always inlined, despite its size, because it has a single
273 /// callsite and it is called *very* frequently.
275 fn process_obligation(
277 pending_obligation: &mut Self::Obligation,
278 ) -> ProcessResult<Self::Obligation, Self::Error> {
279 // If we were stalled on some unresolved variables, first check whether
280 // any of them have been resolved; if not, don't bother doing more work
282 let change = match pending_obligation.stalled_on.len() {
283 // Match arms are in order of frequency, which matters because this
284 // code is so hot. 1 and 0 dominate; 2+ is fairly rare.
286 let infer_var = pending_obligation.stalled_on[0];
287 self.selcx.infcx().ty_or_const_infer_var_changed(infer_var)
290 // In this case we haven't changed, but wish to make a change.
294 // This `for` loop was once a call to `all()`, but this lower-level
295 // form was a perf win. See #64545 for details.
297 for &infer_var in &pending_obligation.stalled_on {
298 if self.selcx.infcx().ty_or_const_infer_var_changed(infer_var) {
309 "process_predicate: pending obligation {:?} still stalled on {:?}",
310 self.selcx.infcx().resolve_vars_if_possible(pending_obligation.obligation.clone()),
311 pending_obligation.stalled_on
313 return ProcessResult::Unchanged;
316 self.process_changed_obligations(pending_obligation)
319 fn process_backedge<'c, I>(
322 _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>,
324 I: Clone + Iterator<Item = &'c PendingPredicateObligation<'tcx>>,
326 if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
327 debug!("process_child_obligations: coinductive match");
329 let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
330 self.selcx.infcx().report_overflow_error_cycle(&cycle);
335 impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
336 // The code calling this method is extremely hot and only rarely
337 // actually uses this, so move this part of the code
340 fn process_changed_obligations(
342 pending_obligation: &mut PendingPredicateObligation<'tcx>,
343 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
344 pending_obligation.stalled_on.truncate(0);
346 let obligation = &mut pending_obligation.obligation;
348 debug!(?obligation, "process_obligation pre-resolve");
350 if obligation.predicate.has_infer_types_or_consts() {
351 obligation.predicate =
352 self.selcx.infcx().resolve_vars_if_possible(obligation.predicate);
355 debug!(?obligation, ?obligation.cause, "process_obligation");
357 let infcx = self.selcx.infcx();
359 if obligation.predicate.has_projections() {
360 let mut obligations = Vec::new();
361 let predicate = crate::traits::project::try_normalize_with_depth_to(
363 obligation.param_env,
364 obligation.cause.clone(),
365 obligation.recursion_depth + 1,
366 obligation.predicate,
369 if predicate != obligation.predicate {
370 obligations.push(obligation.with(predicate));
371 return ProcessResult::Changed(mk_pending(obligations));
374 let binder = obligation.predicate.kind();
375 match binder.no_bound_vars() {
376 None => match binder.skip_binder() {
377 // Evaluation will discard candidates using the leak check.
378 // This means we need to pass it the bound version of our
380 ty::PredicateKind::Trait(trait_ref) => {
381 let trait_obligation = obligation.with(binder.rebind(trait_ref));
383 self.process_trait_obligation(
386 &mut pending_obligation.stalled_on,
389 ty::PredicateKind::Projection(data) => {
390 let project_obligation = obligation.with(binder.rebind(data));
392 self.process_projection_obligation(
395 &mut pending_obligation.stalled_on,
398 ty::PredicateKind::RegionOutlives(_)
399 | ty::PredicateKind::TypeOutlives(_)
400 | ty::PredicateKind::WellFormed(_)
401 | ty::PredicateKind::ObjectSafe(_)
402 | ty::PredicateKind::ClosureKind(..)
403 | ty::PredicateKind::Subtype(_)
404 | ty::PredicateKind::Coerce(_)
405 | ty::PredicateKind::ConstEvaluatable(..)
406 | ty::PredicateKind::ConstEquate(..) => {
408 ty::Binder::dummy(infcx.replace_bound_vars_with_placeholders(binder));
409 ProcessResult::Changed(mk_pending(vec![
410 obligation.with(pred.to_predicate(self.selcx.tcx())),
413 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
414 bug!("TypeWellFormedFromEnv is only used for Chalk")
417 Some(pred) => match pred {
418 ty::PredicateKind::Trait(data) => {
419 let trait_obligation = obligation.with(Binder::dummy(data));
421 self.process_trait_obligation(
424 &mut pending_obligation.stalled_on,
428 ty::PredicateKind::RegionOutlives(data) => {
429 match infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data)) {
430 Ok(()) => ProcessResult::Changed(vec![]),
431 Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)),
435 ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t_a, r_b)) => {
436 if self.register_region_obligations {
437 self.selcx.infcx().register_region_obligation_with_cause(
443 ProcessResult::Changed(vec![])
446 ty::PredicateKind::Projection(ref data) => {
447 let project_obligation = obligation.with(Binder::dummy(*data));
449 self.process_projection_obligation(
452 &mut pending_obligation.stalled_on,
456 ty::PredicateKind::ObjectSafe(trait_def_id) => {
457 if !self.selcx.tcx().is_object_safe(trait_def_id) {
458 ProcessResult::Error(CodeSelectionError(Unimplemented))
460 ProcessResult::Changed(vec![])
464 ty::PredicateKind::ClosureKind(_, closure_substs, kind) => {
465 match self.selcx.infcx().closure_kind(closure_substs) {
466 Some(closure_kind) => {
467 if closure_kind.extends(kind) {
468 ProcessResult::Changed(vec![])
470 ProcessResult::Error(CodeSelectionError(Unimplemented))
473 None => ProcessResult::Unchanged,
477 ty::PredicateKind::WellFormed(arg) => {
478 match wf::obligations(
480 obligation.param_env,
481 obligation.cause.body_id,
482 obligation.recursion_depth + 1,
484 obligation.cause.span,
487 pending_obligation.stalled_on =
488 vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
489 ProcessResult::Unchanged
491 Some(os) => ProcessResult::Changed(mk_pending(os)),
495 ty::PredicateKind::Subtype(subtype) => {
496 match self.selcx.infcx().subtype_predicate(
498 obligation.param_env,
499 Binder::dummy(subtype),
502 // None means that both are unresolved.
503 pending_obligation.stalled_on = vec![
504 TyOrConstInferVar::maybe_from_ty(subtype.a).unwrap(),
505 TyOrConstInferVar::maybe_from_ty(subtype.b).unwrap(),
507 ProcessResult::Unchanged
509 Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
512 ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b);
513 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
521 ty::PredicateKind::Coerce(coerce) => {
522 match self.selcx.infcx().coerce_predicate(
524 obligation.param_env,
525 Binder::dummy(coerce),
528 // None means that both are unresolved.
529 pending_obligation.stalled_on = vec![
530 TyOrConstInferVar::maybe_from_ty(coerce.a).unwrap(),
531 TyOrConstInferVar::maybe_from_ty(coerce.b).unwrap(),
533 ProcessResult::Unchanged
535 Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)),
537 let expected_found = ExpectedFound::new(false, coerce.a, coerce.b);
538 ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError(
546 ty::PredicateKind::ConstEvaluatable(uv) => {
547 match const_evaluatable::is_const_evaluatable(
550 obligation.param_env,
551 obligation.cause.span,
553 Ok(()) => ProcessResult::Changed(vec![]),
554 Err(NotConstEvaluatable::MentionsInfer) => {
555 pending_obligation.stalled_on.clear();
556 pending_obligation.stalled_on.extend(
559 .filter_map(TyOrConstInferVar::maybe_from_generic_arg),
561 ProcessResult::Unchanged
564 e @ NotConstEvaluatable::MentionsParam
565 | e @ NotConstEvaluatable::Error(_),
566 ) => ProcessResult::Error(CodeSelectionError(
567 SelectionError::NotConstEvaluatable(e),
572 ty::PredicateKind::ConstEquate(c1, c2) => {
573 debug!(?c1, ?c2, "equating consts");
574 let tcx = self.selcx.tcx();
575 if tcx.features().generic_const_exprs {
576 // FIXME: we probably should only try to unify abstract constants
577 // if the constants depend on generic parameters.
579 // Let's just see where this breaks :shrug:
580 if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
583 if infcx.try_unify_abstract_consts(
586 obligation.param_env,
588 return ProcessResult::Changed(vec![]);
593 let stalled_on = &mut pending_obligation.stalled_on;
595 let mut evaluate = |c: Const<'tcx>| {
596 if let ty::ConstKind::Unevaluated(unevaluated) = c.val() {
597 match self.selcx.infcx().const_eval_resolve(
598 obligation.param_env,
600 Some(obligation.cause.span),
602 Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty())),
603 Err(ErrorHandled::TooGeneric) => {
608 .filter_map(TyOrConstInferVar::maybe_from_generic_arg),
610 Err(ErrorHandled::TooGeneric)
612 Err(err) => Err(err),
619 match (evaluate(c1), evaluate(c2)) {
620 (Ok(c1), Ok(c2)) => {
624 .at(&obligation.cause, obligation.param_env)
627 Ok(_) => ProcessResult::Changed(vec![]),
628 Err(err) => ProcessResult::Error(
629 FulfillmentErrorCode::CodeConstEquateError(
630 ExpectedFound::new(true, c1, c2),
636 (Err(ErrorHandled::Reported(reported)), _)
637 | (_, Err(ErrorHandled::Reported(reported))) => ProcessResult::Error(
638 CodeSelectionError(SelectionError::NotConstEvaluatable(
639 NotConstEvaluatable::Error(reported),
642 (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => {
644 obligation.cause.span(self.selcx.tcx()),
645 "ConstEquate: const_eval_resolve returned an unexpected error"
648 (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
649 if c1.has_infer_types_or_consts() || c2.has_infer_types_or_consts() {
650 ProcessResult::Unchanged
652 // Two different constants using generic parameters ~> error.
653 let expected_found = ExpectedFound::new(true, c1, c2);
654 ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError(
656 TypeError::ConstMismatch(expected_found),
662 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
663 bug!("TypeWellFormedFromEnv is only used for Chalk")
669 #[instrument(level = "debug", skip(self, obligation, stalled_on))]
670 fn process_trait_obligation(
672 obligation: &PredicateObligation<'tcx>,
673 trait_obligation: TraitObligation<'tcx>,
674 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
675 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
676 let infcx = self.selcx.infcx();
677 if obligation.predicate.is_global() {
678 // no type variables present, can use evaluation for better caching.
679 // FIXME: consider caching errors too.
680 if infcx.predicate_must_hold_considering_regions(obligation) {
682 "selecting trait at depth {} evaluated to holds",
683 obligation.recursion_depth
685 return ProcessResult::Changed(vec![]);
689 match self.selcx.select(&trait_obligation) {
690 Ok(Some(impl_source)) => {
691 debug!("selecting trait at depth {} yielded Ok(Some)", obligation.recursion_depth);
692 ProcessResult::Changed(mk_pending(impl_source.nested_obligations()))
695 debug!("selecting trait at depth {} yielded Ok(None)", obligation.recursion_depth);
697 // This is a bit subtle: for the most part, the
698 // only reason we can fail to make progress on
699 // trait selection is because we don't have enough
700 // information about the types in the trait.
702 stalled_on.extend(substs_infer_vars(
704 trait_obligation.predicate.map_bound(|pred| pred.trait_ref.substs),
708 "process_predicate: pending obligation {:?} now stalled on {:?}",
709 infcx.resolve_vars_if_possible(obligation.clone()),
713 ProcessResult::Unchanged
715 Err(selection_err) => {
716 debug!("selecting trait at depth {} yielded Err", obligation.recursion_depth);
718 ProcessResult::Error(CodeSelectionError(selection_err))
723 fn process_projection_obligation(
725 obligation: &PredicateObligation<'tcx>,
726 project_obligation: PolyProjectionObligation<'tcx>,
727 stalled_on: &mut Vec<TyOrConstInferVar<'tcx>>,
728 ) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
729 let tcx = self.selcx.tcx();
731 if obligation.predicate.is_global() {
732 // no type variables present, can use evaluation for better caching.
733 // FIXME: consider caching errors too.
734 if self.selcx.infcx().predicate_must_hold_considering_regions(obligation) {
735 if let Some(key) = ProjectionCacheKey::from_poly_projection_predicate(
737 project_obligation.predicate,
739 // If `predicate_must_hold_considering_regions` succeeds, then we've
740 // evaluated all sub-obligations. We can therefore mark the 'root'
741 // obligation as complete, and skip evaluating sub-obligations.
747 .complete(key, EvaluationResult::EvaluatedToOk);
749 return ProcessResult::Changed(vec![]);
751 tracing::debug!("Does NOT hold: {:?}", obligation);
755 match project::poly_project_and_unify_type(self.selcx, &project_obligation) {
756 ProjectAndUnifyResult::Holds(os) => ProcessResult::Changed(mk_pending(os)),
757 ProjectAndUnifyResult::FailedNormalization => {
759 stalled_on.extend(substs_infer_vars(
761 project_obligation.predicate.map_bound(|pred| pred.projection_ty.substs),
763 ProcessResult::Unchanged
765 // Let the caller handle the recursion
766 ProjectAndUnifyResult::Recursive => ProcessResult::Changed(mk_pending(vec![
767 project_obligation.with(project_obligation.predicate.to_predicate(tcx)),
769 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => {
770 ProcessResult::Error(CodeProjectionError(e))
776 /// Returns the set of inference variables contained in `substs`.
777 fn substs_infer_vars<'a, 'tcx>(
778 selcx: &mut SelectionContext<'a, 'tcx>,
779 substs: ty::Binder<'tcx, SubstsRef<'tcx>>,
780 ) -> impl Iterator<Item = TyOrConstInferVar<'tcx>> {
783 .resolve_vars_if_possible(substs)
784 .skip_binder() // ok because this check doesn't care about regions
786 .filter(|arg| arg.has_infer_types_or_consts())
788 let mut walker = arg.walk();
789 while let Some(c) = walker.next() {
790 if !c.has_infer_types_or_consts() {
791 walker.visited.remove(&c);
792 walker.skip_current_subtree();
795 walker.visited.into_iter()
797 .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
800 fn to_fulfillment_error<'tcx>(
801 error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>,
802 ) -> FulfillmentError<'tcx> {
803 let mut iter = error.backtrace.into_iter();
804 let obligation = iter.next().unwrap().obligation;
805 // The root obligation is the last item in the backtrace - if there's only
806 // one item, then it's the same as the main obligation
807 let root_obligation = iter.next_back().map_or_else(|| obligation.clone(), |e| e.obligation);
808 FulfillmentError::new(obligation, error.error, root_obligation)