1 //! Code for projecting associated types out of trait references.
3 use super::specialization_graph;
4 use super::translate_substs;
6 use super::MismatchedProjectionTypes;
8 use super::ObligationCause;
9 use super::PredicateObligation;
11 use super::SelectionContext;
12 use super::SelectionError;
14 ImplSourceClosureData, ImplSourceDiscriminantKindData, ImplSourceFnPointerData,
15 ImplSourceGeneratorData, ImplSourcePointeeData, ImplSourceUserDefinedData,
17 use super::{Normalized, NormalizedTy, ProjectionCacheEntry, ProjectionCacheKey};
19 use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
20 use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
21 use crate::traits::error_reporting::InferCtxtExt as _;
22 use crate::traits::select::ProjectionMatchesProjection;
23 use rustc_data_structures::sso::SsoHashSet;
24 use rustc_data_structures::stack::ensure_sufficient_stack;
25 use rustc_errors::ErrorGuaranteed;
26 use rustc_hir::def::DefKind;
27 use rustc_hir::def_id::DefId;
28 use rustc_hir::lang_items::LangItem;
29 use rustc_infer::infer::resolve::OpportunisticRegionResolver;
30 use rustc_middle::traits::select::OverflowError;
31 use rustc_middle::ty::fold::{MaxUniverse, TypeFoldable, TypeFolder};
32 use rustc_middle::ty::subst::Subst;
33 use rustc_middle::ty::{self, Term, ToPredicate, Ty, TyCtxt};
34 use rustc_span::symbol::sym;
36 use std::collections::BTreeMap;
38 pub use rustc_middle::traits::Reveal;
40 pub type PolyProjectionObligation<'tcx> = Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
42 pub type ProjectionObligation<'tcx> = Obligation<'tcx, ty::ProjectionPredicate<'tcx>>;
44 pub type ProjectionTyObligation<'tcx> = Obligation<'tcx, ty::ProjectionTy<'tcx>>;
46 pub(super) struct InProgress;
48 /// When attempting to resolve `<T as TraitRef>::Name` ...
50 pub enum ProjectionError<'tcx> {
51 /// ...we found multiple sources of information and couldn't resolve the ambiguity.
54 /// ...an error occurred matching `T : TraitRef`
55 TraitSelectionError(SelectionError<'tcx>),
58 #[derive(PartialEq, Eq, Debug)]
59 enum ProjectionCandidate<'tcx> {
60 /// From a where-clause in the env or object type
61 ParamEnv(ty::PolyProjectionPredicate<'tcx>),
63 /// From the definition of `Trait` when you have something like <<A as Trait>::B as Trait2>::C
64 TraitDef(ty::PolyProjectionPredicate<'tcx>),
66 /// Bounds specified on an object type
67 Object(ty::PolyProjectionPredicate<'tcx>),
69 /// From an "impl" (or a "pseudo-impl" returned by select)
70 Select(Selection<'tcx>),
73 enum ProjectionCandidateSet<'tcx> {
75 Single(ProjectionCandidate<'tcx>),
77 Error(SelectionError<'tcx>),
80 impl<'tcx> ProjectionCandidateSet<'tcx> {
81 fn mark_ambiguous(&mut self) {
82 *self = ProjectionCandidateSet::Ambiguous;
85 fn mark_error(&mut self, err: SelectionError<'tcx>) {
86 *self = ProjectionCandidateSet::Error(err);
89 // Returns true if the push was successful, or false if the candidate
90 // was discarded -- this could be because of ambiguity, or because
91 // a higher-priority candidate is already there.
92 fn push_candidate(&mut self, candidate: ProjectionCandidate<'tcx>) -> bool {
93 use self::ProjectionCandidate::*;
94 use self::ProjectionCandidateSet::*;
96 // This wacky variable is just used to try and
97 // make code readable and avoid confusing paths.
98 // It is assigned a "value" of `()` only on those
99 // paths in which we wish to convert `*self` to
100 // ambiguous (and return false, because the candidate
101 // was not used). On other paths, it is not assigned,
102 // and hence if those paths *could* reach the code that
103 // comes after the match, this fn would not compile.
104 let convert_to_ambiguous;
108 *self = Single(candidate);
113 // Duplicates can happen inside ParamEnv. In the case, we
114 // perform a lazy deduplication.
115 if current == &candidate {
119 // Prefer where-clauses. As in select, if there are multiple
120 // candidates, we prefer where-clause candidates over impls. This
121 // may seem a bit surprising, since impls are the source of
122 // "truth" in some sense, but in fact some of the impls that SEEM
123 // applicable are not, because of nested obligations. Where
124 // clauses are the safer choice. See the comment on
125 // `select::SelectionCandidate` and #21974 for more details.
126 match (current, candidate) {
127 (ParamEnv(..), ParamEnv(..)) => convert_to_ambiguous = (),
128 (ParamEnv(..), _) => return false,
129 (_, ParamEnv(..)) => unreachable!(),
130 (_, _) => convert_to_ambiguous = (),
134 Ambiguous | Error(..) => {
139 // We only ever get here when we moved from a single candidate
141 let () = convert_to_ambiguous;
147 /// Takes the place of a
149 /// Result<Option<Vec<PredicateObligation<'tcx>>>, InProgress>,
150 /// MismatchedProjectionTypes<'tcx>,
152 pub(super) enum ProjectAndUnifyResult<'tcx> {
153 Holds(Vec<PredicateObligation<'tcx>>),
156 MismatchedProjectionTypes(MismatchedProjectionTypes<'tcx>),
159 /// Evaluates constraints of the form:
161 /// for<...> <T as Trait>::U == V
163 /// If successful, this may result in additional obligations. Also returns
164 /// the projection cache key used to track these additional obligations.
168 /// - `Err(_)`: the projection can be normalized, but is not equal to the
170 /// - `Ok(Err(InProgress))`: this is called recursively while normalizing
171 /// the same projection.
172 /// - `Ok(Ok(None))`: The projection cannot be normalized due to ambiguity
173 /// (resolving some inference variables in the projection may fix this).
174 /// - `Ok(Ok(Some(obligations)))`: The projection bound holds subject to
175 /// the given obligations. If the projection cannot be normalized because
176 /// the required trait bound doesn't hold this returned with `obligations`
177 /// being a predicate that cannot be proven.
178 #[instrument(level = "debug", skip(selcx))]
179 pub(super) fn poly_project_and_unify_type<'cx, 'tcx>(
180 selcx: &mut SelectionContext<'cx, 'tcx>,
181 obligation: &PolyProjectionObligation<'tcx>,
182 ) -> ProjectAndUnifyResult<'tcx> {
183 let infcx = selcx.infcx();
184 let r = infcx.commit_if_ok(|_snapshot| {
185 let old_universe = infcx.universe();
186 let placeholder_predicate =
187 infcx.replace_bound_vars_with_placeholders(obligation.predicate);
188 let new_universe = infcx.universe();
190 let placeholder_obligation = obligation.with(placeholder_predicate);
191 match project_and_unify_type(selcx, &placeholder_obligation) {
192 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => Err(e),
193 ProjectAndUnifyResult::Holds(obligations)
194 if old_universe != new_universe
195 && selcx.tcx().features().generic_associated_types_extended =>
197 // If the `generic_associated_types_extended` feature is active, then we ignore any
198 // obligations references lifetimes from any universe greater than or equal to the
199 // universe just created. Otherwise, we can end up with something like `for<'a> I: 'a`,
200 // which isn't quite what we want. Ideally, we want either an implied
201 // `for<'a where I: 'a> I: 'a` or we want to "lazily" check these hold when we
202 // substitute concrete regions. There is design work to be done here; until then,
203 // however, this allows experimenting potential GAT features without running into
204 // well-formedness issues.
205 let new_obligations = obligations
207 .filter(|obligation| {
208 let mut visitor = MaxUniverse::new();
209 obligation.predicate.visit_with(&mut visitor);
210 visitor.max_universe() < new_universe
213 Ok(ProjectAndUnifyResult::Holds(new_obligations))
221 Err(err) => ProjectAndUnifyResult::MismatchedProjectionTypes(err),
225 /// Evaluates constraints of the form:
227 /// <T as Trait>::U == V
229 /// If successful, this may result in additional obligations.
231 /// See [poly_project_and_unify_type] for an explanation of the return value.
232 #[tracing::instrument(level = "debug", skip(selcx))]
233 fn project_and_unify_type<'cx, 'tcx>(
234 selcx: &mut SelectionContext<'cx, 'tcx>,
235 obligation: &ProjectionObligation<'tcx>,
236 ) -> ProjectAndUnifyResult<'tcx> {
237 let mut obligations = vec![];
239 let infcx = selcx.infcx();
240 let normalized = match opt_normalize_projection_type(
242 obligation.param_env,
243 obligation.predicate.projection_ty,
244 obligation.cause.clone(),
245 obligation.recursion_depth,
249 Ok(None) => return ProjectAndUnifyResult::FailedNormalization,
250 Err(InProgress) => return ProjectAndUnifyResult::Recursive,
252 debug!(?normalized, ?obligations, "project_and_unify_type result");
253 let actual = obligation.predicate.term;
254 // HACK: lazy TAIT would regress src/test/ui/impl-trait/nested-return-type2.rs, so we add
255 // a back-compat hack hat converts the RPITs into inference vars, just like they were before
257 // This does not affect TAITs in general, as tested in the nested-return-type-tait* tests.
258 let InferOk { value: actual, obligations: new } =
259 selcx.infcx().replace_opaque_types_with_inference_vars(
261 obligation.cause.body_id,
262 obligation.cause.span,
263 obligation.param_env,
265 obligations.extend(new);
267 match infcx.at(&obligation.cause, obligation.param_env).eq(normalized, actual) {
268 Ok(InferOk { obligations: inferred_obligations, value: () }) => {
269 obligations.extend(inferred_obligations);
270 ProjectAndUnifyResult::Holds(obligations)
273 debug!("equating types encountered error {:?}", err);
274 ProjectAndUnifyResult::MismatchedProjectionTypes(MismatchedProjectionTypes { err })
279 /// Normalizes any associated type projections in `value`, replacing
280 /// them with a fully resolved type where possible. The return value
281 /// combines the normalized result and any additional obligations that
282 /// were incurred as result.
283 pub fn normalize<'a, 'b, 'tcx, T>(
284 selcx: &'a mut SelectionContext<'b, 'tcx>,
285 param_env: ty::ParamEnv<'tcx>,
286 cause: ObligationCause<'tcx>,
288 ) -> Normalized<'tcx, T>
290 T: TypeFoldable<'tcx>,
292 let mut obligations = Vec::new();
293 let value = normalize_to(selcx, param_env, cause, value, &mut obligations);
294 Normalized { value, obligations }
297 pub fn normalize_to<'a, 'b, 'tcx, T>(
298 selcx: &'a mut SelectionContext<'b, 'tcx>,
299 param_env: ty::ParamEnv<'tcx>,
300 cause: ObligationCause<'tcx>,
302 obligations: &mut Vec<PredicateObligation<'tcx>>,
305 T: TypeFoldable<'tcx>,
307 normalize_with_depth_to(selcx, param_env, cause, 0, value, obligations)
310 /// As `normalize`, but with a custom depth.
311 pub fn normalize_with_depth<'a, 'b, 'tcx, T>(
312 selcx: &'a mut SelectionContext<'b, 'tcx>,
313 param_env: ty::ParamEnv<'tcx>,
314 cause: ObligationCause<'tcx>,
317 ) -> Normalized<'tcx, T>
319 T: TypeFoldable<'tcx>,
321 let mut obligations = Vec::new();
322 let value = normalize_with_depth_to(selcx, param_env, cause, depth, value, &mut obligations);
323 Normalized { value, obligations }
326 #[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
327 pub fn normalize_with_depth_to<'a, 'b, 'tcx, T>(
328 selcx: &'a mut SelectionContext<'b, 'tcx>,
329 param_env: ty::ParamEnv<'tcx>,
330 cause: ObligationCause<'tcx>,
333 obligations: &mut Vec<PredicateObligation<'tcx>>,
336 T: TypeFoldable<'tcx>,
338 debug!(obligations.len = obligations.len());
339 let mut normalizer = AssocTypeNormalizer::new(selcx, param_env, cause, depth, obligations);
340 let result = ensure_sufficient_stack(|| normalizer.fold(value));
341 debug!(?result, obligations.len = normalizer.obligations.len());
342 debug!(?normalizer.obligations,);
346 #[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
347 pub fn try_normalize_with_depth_to<'a, 'b, 'tcx, T>(
348 selcx: &'a mut SelectionContext<'b, 'tcx>,
349 param_env: ty::ParamEnv<'tcx>,
350 cause: ObligationCause<'tcx>,
353 obligations: &mut Vec<PredicateObligation<'tcx>>,
356 T: TypeFoldable<'tcx>,
358 debug!(obligations.len = obligations.len());
359 let mut normalizer = AssocTypeNormalizer::new_without_eager_inference_replacement(
366 let result = ensure_sufficient_stack(|| normalizer.fold(value));
367 debug!(?result, obligations.len = normalizer.obligations.len());
368 debug!(?normalizer.obligations,);
372 pub(crate) fn needs_normalization<'tcx, T: TypeFoldable<'tcx>>(value: &T, reveal: Reveal) -> bool {
374 Reveal::UserFacing => value
375 .has_type_flags(ty::TypeFlags::HAS_TY_PROJECTION | ty::TypeFlags::HAS_CT_PROJECTION),
376 Reveal::All => value.has_type_flags(
377 ty::TypeFlags::HAS_TY_PROJECTION
378 | ty::TypeFlags::HAS_TY_OPAQUE
379 | ty::TypeFlags::HAS_CT_PROJECTION,
384 struct AssocTypeNormalizer<'a, 'b, 'tcx> {
385 selcx: &'a mut SelectionContext<'b, 'tcx>,
386 param_env: ty::ParamEnv<'tcx>,
387 cause: ObligationCause<'tcx>,
388 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
390 universes: Vec<Option<ty::UniverseIndex>>,
391 /// If true, when a projection is unable to be completed, an inference
392 /// variable will be created and an obligation registered to project to that
393 /// inference variable. Also, constants will be eagerly evaluated.
394 eager_inference_replacement: bool,
397 impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
399 selcx: &'a mut SelectionContext<'b, 'tcx>,
400 param_env: ty::ParamEnv<'tcx>,
401 cause: ObligationCause<'tcx>,
403 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
404 ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
405 AssocTypeNormalizer {
412 eager_inference_replacement: true,
416 fn new_without_eager_inference_replacement(
417 selcx: &'a mut SelectionContext<'b, 'tcx>,
418 param_env: ty::ParamEnv<'tcx>,
419 cause: ObligationCause<'tcx>,
421 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
422 ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
423 AssocTypeNormalizer {
430 eager_inference_replacement: false,
434 fn fold<T: TypeFoldable<'tcx>>(&mut self, value: T) -> T {
435 let value = self.selcx.infcx().resolve_vars_if_possible(value);
439 !value.has_escaping_bound_vars(),
440 "Normalizing {:?} without wrapping in a `Binder`",
444 if !needs_normalization(&value, self.param_env.reveal()) {
447 value.fold_with(self)
452 impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
453 fn tcx<'c>(&'c self) -> TyCtxt<'tcx> {
457 fn fold_binder<T: TypeFoldable<'tcx>>(
459 t: ty::Binder<'tcx, T>,
460 ) -> ty::Binder<'tcx, T> {
461 self.universes.push(None);
462 let t = t.super_fold_with(self);
463 self.universes.pop();
467 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
468 if !needs_normalization(&ty, self.param_env.reveal()) {
472 // We try to be a little clever here as a performance optimization in
473 // cases where there are nested projections under binders.
476 // for<'a> fn(<T as Foo>::One<'a, Box<dyn Bar<'a, Item=<T as Foo>::Two<'a>>>>)
478 // We normalize the substs on the projection before the projecting, but
479 // if we're naive, we'll
480 // replace bound vars on inner, project inner, replace placeholders on inner,
481 // replace bound vars on outer, project outer, replace placeholders on outer
483 // However, if we're a bit more clever, we can replace the bound vars
484 // on the entire type before normalizing nested projections, meaning we
485 // replace bound vars on outer, project inner,
486 // project outer, replace placeholders on outer
488 // This is possible because the inner `'a` will already be a placeholder
489 // when we need to normalize the inner projection
491 // On the other hand, this does add a bit of complexity, since we only
492 // replace bound vars if the current type is a `Projection` and we need
493 // to make sure we don't forget to fold the substs regardless.
496 // This is really important. While we *can* handle this, this has
497 // severe performance implications for large opaque types with
498 // late-bound regions. See `issue-88862` benchmark.
499 ty::Opaque(def_id, substs) if !substs.has_escaping_bound_vars() => {
500 // Only normalize `impl Trait` outside of type inference, usually in codegen.
501 match self.param_env.reveal() {
502 Reveal::UserFacing => ty.super_fold_with(self),
505 let recursion_limit = self.tcx().recursion_limit();
506 if !recursion_limit.value_within_limit(self.depth) {
507 let obligation = Obligation::with_depth(
513 self.selcx.infcx().report_overflow_error(&obligation, true);
516 let substs = substs.super_fold_with(self);
517 let generic_ty = self.tcx().type_of(def_id);
518 let concrete_ty = generic_ty.subst(self.tcx(), substs);
520 let folded_ty = self.fold_ty(concrete_ty);
527 ty::Projection(data) if !data.has_escaping_bound_vars() => {
528 // This branch is *mostly* just an optimization: when we don't
529 // have escaping bound vars, we don't need to replace them with
530 // placeholders (see branch below). *Also*, we know that we can
531 // register an obligation to *later* project, since we know
532 // there won't be bound vars there.
534 let data = data.super_fold_with(self);
535 let normalized_ty = if self.eager_inference_replacement {
536 normalize_projection_type(
542 &mut self.obligations,
545 opt_normalize_projection_type(
551 &mut self.obligations,
555 .unwrap_or_else(|| ty::Term::Ty(ty.super_fold_with(self)))
561 obligations.len = ?self.obligations.len(),
562 "AssocTypeNormalizer: normalized type"
564 normalized_ty.ty().unwrap()
567 ty::Projection(data) => {
568 // If there are escaping bound vars, we temporarily replace the
569 // bound vars with placeholders. Note though, that in the case
570 // that we still can't project for whatever reason (e.g. self
571 // type isn't known enough), we *can't* register an obligation
572 // and return an inference variable (since then that obligation
573 // would have bound vars and that's a can of worms). Instead,
574 // we just give up and fall back to pretending like we never tried!
576 // Note: this isn't necessarily the final approach here; we may
577 // want to figure out how to register obligations with escaping vars
578 // or handle this some other way.
580 let infcx = self.selcx.infcx();
581 let (data, mapped_regions, mapped_types, mapped_consts) =
582 BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, data);
583 let data = data.super_fold_with(self);
584 let normalized_ty = opt_normalize_projection_type(
590 &mut self.obligations,
594 .map(|term| term.ty().unwrap())
595 .map(|normalized_ty| {
596 PlaceholderReplacer::replace_placeholders(
605 .unwrap_or_else(|| ty.super_fold_with(self));
611 obligations.len = ?self.obligations.len(),
612 "AssocTypeNormalizer: normalized type"
617 _ => ty.super_fold_with(self),
621 fn fold_const(&mut self, constant: ty::Const<'tcx>) -> ty::Const<'tcx> {
622 if self.selcx.tcx().lazy_normalization() || !self.eager_inference_replacement {
625 let constant = constant.super_fold_with(self);
626 constant.eval(self.selcx.tcx(), self.param_env)
631 pub struct BoundVarReplacer<'me, 'tcx> {
632 infcx: &'me InferCtxt<'me, 'tcx>,
633 // These three maps track the bound variable that were replaced by placeholders. It might be
634 // nice to remove these since we already have the `kind` in the placeholder; we really just need
635 // the `var` (but we *could* bring that into scope if we were to track them as we pass them).
636 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
637 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
638 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
639 // The current depth relative to *this* folding, *not* the entire normalization. In other words,
640 // the depth of binders we've passed here.
641 current_index: ty::DebruijnIndex,
642 // The `UniverseIndex` of the binding levels above us. These are optional, since we are lazy:
643 // we don't actually create a universe until we see a bound var we have to replace.
644 universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
647 impl<'me, 'tcx> BoundVarReplacer<'me, 'tcx> {
648 /// Returns `Some` if we *were* able to replace bound vars. If there are any bound vars that
649 /// use a binding level above `universe_indices.len()`, we fail.
650 pub fn replace_bound_vars<T: TypeFoldable<'tcx>>(
651 infcx: &'me InferCtxt<'me, 'tcx>,
652 universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
656 BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
657 BTreeMap<ty::PlaceholderType, ty::BoundTy>,
658 BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
660 let mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion> = BTreeMap::new();
661 let mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy> = BTreeMap::new();
662 let mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar> = BTreeMap::new();
664 let mut replacer = BoundVarReplacer {
669 current_index: ty::INNERMOST,
673 let value = value.super_fold_with(&mut replacer);
675 (value, replacer.mapped_regions, replacer.mapped_types, replacer.mapped_consts)
678 fn universe_for(&mut self, debruijn: ty::DebruijnIndex) -> ty::UniverseIndex {
679 let infcx = self.infcx;
681 self.universe_indices.len() + self.current_index.as_usize() - debruijn.as_usize() - 1;
682 let universe = self.universe_indices[index].unwrap_or_else(|| {
683 for i in self.universe_indices.iter_mut().take(index + 1) {
684 *i = i.or_else(|| Some(infcx.create_next_universe()))
686 self.universe_indices[index].unwrap()
692 impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
693 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
697 fn fold_binder<T: TypeFoldable<'tcx>>(
699 t: ty::Binder<'tcx, T>,
700 ) -> ty::Binder<'tcx, T> {
701 self.current_index.shift_in(1);
702 let t = t.super_fold_with(self);
703 self.current_index.shift_out(1);
707 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
709 ty::ReLateBound(debruijn, _)
710 if debruijn.as_usize() + 1
711 > self.current_index.as_usize() + self.universe_indices.len() =>
713 bug!("Bound vars outside of `self.universe_indices`");
715 ty::ReLateBound(debruijn, br) if debruijn >= self.current_index => {
716 let universe = self.universe_for(debruijn);
717 let p = ty::PlaceholderRegion { universe, name: br.kind };
718 self.mapped_regions.insert(p, br);
719 self.infcx.tcx.mk_region(ty::RePlaceholder(p))
725 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
727 ty::Bound(debruijn, _)
728 if debruijn.as_usize() + 1
729 > self.current_index.as_usize() + self.universe_indices.len() =>
731 bug!("Bound vars outside of `self.universe_indices`");
733 ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
734 let universe = self.universe_for(debruijn);
735 let p = ty::PlaceholderType { universe, name: bound_ty.var };
736 self.mapped_types.insert(p, bound_ty);
737 self.infcx.tcx.mk_ty(ty::Placeholder(p))
739 _ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
744 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
746 ty::ConstKind::Bound(debruijn, _)
747 if debruijn.as_usize() + 1
748 > self.current_index.as_usize() + self.universe_indices.len() =>
750 bug!("Bound vars outside of `self.universe_indices`");
752 ty::ConstKind::Bound(debruijn, bound_const) if debruijn >= self.current_index => {
753 let universe = self.universe_for(debruijn);
754 let p = ty::PlaceholderConst {
756 name: ty::BoundConst { var: bound_const, ty: ct.ty() },
758 self.mapped_consts.insert(p, bound_const);
761 .mk_const(ty::ConstS { val: ty::ConstKind::Placeholder(p), ty: ct.ty() })
763 _ if ct.has_vars_bound_at_or_above(self.current_index) => ct.super_fold_with(self),
769 // The inverse of `BoundVarReplacer`: replaces placeholders with the bound vars from which they came.
770 pub struct PlaceholderReplacer<'me, 'tcx> {
771 infcx: &'me InferCtxt<'me, 'tcx>,
772 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
773 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
774 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
775 universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
776 current_index: ty::DebruijnIndex,
779 impl<'me, 'tcx> PlaceholderReplacer<'me, 'tcx> {
780 pub fn replace_placeholders<T: TypeFoldable<'tcx>>(
781 infcx: &'me InferCtxt<'me, 'tcx>,
782 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
783 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
784 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
785 universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
788 let mut replacer = PlaceholderReplacer {
794 current_index: ty::INNERMOST,
796 value.super_fold_with(&mut replacer)
800 impl<'tcx> TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> {
801 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
805 fn fold_binder<T: TypeFoldable<'tcx>>(
807 t: ty::Binder<'tcx, T>,
808 ) -> ty::Binder<'tcx, T> {
809 if !t.has_placeholders() && !t.has_infer_regions() {
812 self.current_index.shift_in(1);
813 let t = t.super_fold_with(self);
814 self.current_index.shift_out(1);
818 fn fold_region(&mut self, r0: ty::Region<'tcx>) -> ty::Region<'tcx> {
824 .unwrap_region_constraints()
825 .opportunistic_resolve_region(self.infcx.tcx, r0),
830 ty::RePlaceholder(p) => {
831 let replace_var = self.mapped_regions.get(&p);
833 Some(replace_var) => {
837 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
838 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
839 let db = ty::DebruijnIndex::from_usize(
840 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
842 self.tcx().mk_region(ty::ReLateBound(db, *replace_var))
850 debug!(?r0, ?r1, ?r2, "fold_region");
855 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
857 ty::Placeholder(p) => {
858 let replace_var = self.mapped_types.get(&p);
860 Some(replace_var) => {
864 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
865 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
866 let db = ty::DebruijnIndex::from_usize(
867 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
869 self.tcx().mk_ty(ty::Bound(db, *replace_var))
875 _ if ty.has_placeholders() || ty.has_infer_regions() => ty.super_fold_with(self),
880 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
881 if let ty::ConstKind::Placeholder(p) = ct.val() {
882 let replace_var = self.mapped_consts.get(&p);
884 Some(replace_var) => {
888 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
889 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
890 let db = ty::DebruijnIndex::from_usize(
891 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
893 self.tcx().mk_const(ty::ConstS {
894 val: ty::ConstKind::Bound(db, *replace_var),
901 ct.super_fold_with(self)
906 /// The guts of `normalize`: normalize a specific projection like `<T
907 /// as Trait>::Item`. The result is always a type (and possibly
908 /// additional obligations). If ambiguity arises, which implies that
909 /// there are unresolved type variables in the projection, we will
910 /// substitute a fresh type variable `$X` and generate a new
911 /// obligation `<T as Trait>::Item == $X` for later.
912 pub fn normalize_projection_type<'a, 'b, 'tcx>(
913 selcx: &'a mut SelectionContext<'b, 'tcx>,
914 param_env: ty::ParamEnv<'tcx>,
915 projection_ty: ty::ProjectionTy<'tcx>,
916 cause: ObligationCause<'tcx>,
918 obligations: &mut Vec<PredicateObligation<'tcx>>,
920 opt_normalize_projection_type(
930 .unwrap_or_else(move || {
931 // if we bottom out in ambiguity, create a type variable
932 // and a deferred predicate to resolve this when more type
933 // information is available.
937 .infer_projection(param_env, projection_ty, cause, depth + 1, obligations)
942 /// The guts of `normalize`: normalize a specific projection like `<T
943 /// as Trait>::Item`. The result is always a type (and possibly
944 /// additional obligations). Returns `None` in the case of ambiguity,
945 /// which indicates that there are unbound type variables.
947 /// This function used to return `Option<NormalizedTy<'tcx>>`, which contains a
948 /// `Ty<'tcx>` and an obligations vector. But that obligation vector was very
949 /// often immediately appended to another obligations vector. So now this
950 /// function takes an obligations vector and appends to it directly, which is
951 /// slightly uglier but avoids the need for an extra short-lived allocation.
952 #[instrument(level = "debug", skip(selcx, param_env, cause, obligations))]
953 fn opt_normalize_projection_type<'a, 'b, 'tcx>(
954 selcx: &'a mut SelectionContext<'b, 'tcx>,
955 param_env: ty::ParamEnv<'tcx>,
956 projection_ty: ty::ProjectionTy<'tcx>,
957 cause: ObligationCause<'tcx>,
959 obligations: &mut Vec<PredicateObligation<'tcx>>,
960 ) -> Result<Option<Term<'tcx>>, InProgress> {
961 let infcx = selcx.infcx();
962 // Don't use the projection cache in intercrate mode -
963 // the `infcx` may be re-used between intercrate in non-intercrate
964 // mode, which could lead to using incorrect cache results.
965 let use_cache = !selcx.is_intercrate();
967 let projection_ty = infcx.resolve_vars_if_possible(projection_ty);
968 let cache_key = ProjectionCacheKey::new(projection_ty);
970 // FIXME(#20304) For now, I am caching here, which is good, but it
971 // means we don't capture the type variables that are created in
972 // the case of ambiguity. Which means we may create a large stream
973 // of such variables. OTOH, if we move the caching up a level, we
974 // would not benefit from caching when proving `T: Trait<U=Foo>`
975 // bounds. It might be the case that we want two distinct caches,
976 // or else another kind of cache entry.
978 let cache_result = if use_cache {
979 infcx.inner.borrow_mut().projection_cache().try_start(cache_key)
984 Ok(()) => debug!("no cache"),
985 Err(ProjectionCacheEntry::Ambiguous) => {
986 // If we found ambiguity the last time, that means we will continue
987 // to do so until some type in the key changes (and we know it
988 // hasn't, because we just fully resolved it).
989 debug!("found cache entry: ambiguous");
992 Err(ProjectionCacheEntry::InProgress) => {
993 // Under lazy normalization, this can arise when
994 // bootstrapping. That is, imagine an environment with a
995 // where-clause like `A::B == u32`. Now, if we are asked
996 // to normalize `A::B`, we will want to check the
997 // where-clauses in scope. So we will try to unify `A::B`
998 // with `A::B`, which can trigger a recursive
1001 debug!("found cache entry: in-progress");
1003 // Cache that normalizing this projection resulted in a cycle. This
1004 // should ensure that, unless this happens within a snapshot that's
1005 // rolled back, fulfillment or evaluation will notice the cycle.
1008 infcx.inner.borrow_mut().projection_cache().recur(cache_key);
1010 return Err(InProgress);
1012 Err(ProjectionCacheEntry::Recur) => {
1013 debug!("recur cache");
1014 return Err(InProgress);
1016 Err(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => {
1017 // This is the hottest path in this function.
1019 // If we find the value in the cache, then return it along
1020 // with the obligations that went along with it. Note
1021 // that, when using a fulfillment context, these
1022 // obligations could in principle be ignored: they have
1023 // already been registered when the cache entry was
1024 // created (and hence the new ones will quickly be
1025 // discarded as duplicated). But when doing trait
1026 // evaluation this is not the case, and dropping the trait
1027 // evaluations can causes ICEs (e.g., #43132).
1028 debug!(?ty, "found normalized ty");
1029 obligations.extend(ty.obligations);
1030 return Ok(Some(ty.value));
1032 Err(ProjectionCacheEntry::Error) => {
1033 debug!("opt_normalize_projection_type: found error");
1034 let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
1035 obligations.extend(result.obligations);
1036 return Ok(Some(result.value.into()));
1040 let obligation = Obligation::with_depth(cause.clone(), depth, param_env, projection_ty);
1042 match project(selcx, &obligation) {
1043 Ok(Projected::Progress(Progress {
1044 term: projected_term,
1045 obligations: mut projected_obligations,
1047 // if projection succeeded, then what we get out of this
1048 // is also non-normalized (consider: it was derived from
1049 // an impl, where-clause etc) and hence we must
1052 let projected_term = selcx.infcx().resolve_vars_if_possible(projected_term);
1054 let mut result = if projected_term.has_projections() {
1055 let mut normalizer = AssocTypeNormalizer::new(
1060 &mut projected_obligations,
1062 let normalized_ty = normalizer.fold(projected_term);
1064 Normalized { value: normalized_ty, obligations: projected_obligations }
1066 Normalized { value: projected_term, obligations: projected_obligations }
1069 let mut deduped: SsoHashSet<_> = Default::default();
1070 result.obligations.drain_filter(|projected_obligation| {
1071 if !deduped.insert(projected_obligation.clone()) {
1078 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
1080 obligations.extend(result.obligations);
1081 Ok(Some(result.value))
1083 Ok(Projected::NoProgress(projected_ty)) => {
1084 let result = Normalized { value: projected_ty, obligations: vec![] };
1086 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
1088 // No need to extend `obligations`.
1089 Ok(Some(result.value))
1091 Err(ProjectionError::TooManyCandidates) => {
1092 debug!("opt_normalize_projection_type: too many candidates");
1094 infcx.inner.borrow_mut().projection_cache().ambiguous(cache_key);
1098 Err(ProjectionError::TraitSelectionError(_)) => {
1099 debug!("opt_normalize_projection_type: ERROR");
1100 // if we got an error processing the `T as Trait` part,
1101 // just return `ty::err` but add the obligation `T :
1102 // Trait`, which when processed will cause the error to be
1106 infcx.inner.borrow_mut().projection_cache().error(cache_key);
1108 let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
1109 obligations.extend(result.obligations);
1110 Ok(Some(result.value.into()))
1115 /// If we are projecting `<T as Trait>::Item`, but `T: Trait` does not
1116 /// hold. In various error cases, we cannot generate a valid
1117 /// normalized projection. Therefore, we create an inference variable
1118 /// return an associated obligation that, when fulfilled, will lead to
1121 /// Note that we used to return `Error` here, but that was quite
1122 /// dubious -- the premise was that an error would *eventually* be
1123 /// reported, when the obligation was processed. But in general once
1124 /// you see an `Error` you are supposed to be able to assume that an
1125 /// error *has been* reported, so that you can take whatever heuristic
1126 /// paths you want to take. To make things worse, it was possible for
1127 /// cycles to arise, where you basically had a setup like `<MyType<$0>
1128 /// as Trait>::Foo == $0`. Here, normalizing `<MyType<$0> as
1129 /// Trait>::Foo> to `[type error]` would lead to an obligation of
1130 /// `<MyType<[type error]> as Trait>::Foo`. We are supposed to report
1131 /// an error for this obligation, but we legitimately should not,
1132 /// because it contains `[type error]`. Yuck! (See issue #29857 for
1133 /// one case where this arose.)
1134 fn normalize_to_error<'a, 'tcx>(
1135 selcx: &mut SelectionContext<'a, 'tcx>,
1136 param_env: ty::ParamEnv<'tcx>,
1137 projection_ty: ty::ProjectionTy<'tcx>,
1138 cause: ObligationCause<'tcx>,
1140 ) -> NormalizedTy<'tcx> {
1141 let trait_ref = ty::Binder::dummy(projection_ty.trait_ref(selcx.tcx()));
1142 let trait_obligation = Obligation {
1144 recursion_depth: depth,
1146 predicate: trait_ref.without_const().to_predicate(selcx.tcx()),
1148 let tcx = selcx.infcx().tcx;
1149 let def_id = projection_ty.item_def_id;
1150 let new_value = selcx.infcx().next_ty_var(TypeVariableOrigin {
1151 kind: TypeVariableOriginKind::NormalizeProjectionType,
1152 span: tcx.def_span(def_id),
1154 Normalized { value: new_value, obligations: vec![trait_obligation] }
1157 enum Projected<'tcx> {
1158 Progress(Progress<'tcx>),
1159 NoProgress(ty::Term<'tcx>),
1162 struct Progress<'tcx> {
1163 term: ty::Term<'tcx>,
1164 obligations: Vec<PredicateObligation<'tcx>>,
1167 impl<'tcx> Progress<'tcx> {
1168 fn error(tcx: TyCtxt<'tcx>) -> Self {
1169 Progress { term: tcx.ty_error().into(), obligations: vec![] }
1172 fn with_addl_obligations(mut self, mut obligations: Vec<PredicateObligation<'tcx>>) -> Self {
1173 self.obligations.append(&mut obligations);
1178 /// Computes the result of a projection type (if we can).
1181 /// - `obligation` must be fully normalized
1182 #[tracing::instrument(level = "info", skip(selcx))]
1183 fn project<'cx, 'tcx>(
1184 selcx: &mut SelectionContext<'cx, 'tcx>,
1185 obligation: &ProjectionTyObligation<'tcx>,
1186 ) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1187 if !selcx.tcx().recursion_limit().value_within_limit(obligation.recursion_depth) {
1188 // This should really be an immediate error, but some existing code
1189 // relies on being able to recover from this.
1190 return Err(ProjectionError::TraitSelectionError(SelectionError::Overflow(
1191 OverflowError::Canonical,
1195 if obligation.predicate.references_error() {
1196 return Ok(Projected::Progress(Progress::error(selcx.tcx())));
1199 let mut candidates = ProjectionCandidateSet::None;
1201 // Make sure that the following procedures are kept in order. ParamEnv
1202 // needs to be first because it has highest priority, and Select checks
1203 // the return value of push_candidate which assumes it's ran at last.
1204 assemble_candidates_from_param_env(selcx, obligation, &mut candidates);
1206 assemble_candidates_from_trait_def(selcx, obligation, &mut candidates);
1208 assemble_candidates_from_object_ty(selcx, obligation, &mut candidates);
1210 if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(_)) = candidates {
1211 // Avoid normalization cycle from selection (see
1212 // `assemble_candidates_from_object_ty`).
1213 // FIXME(lazy_normalization): Lazy normalization should save us from
1214 // having to special case this.
1216 assemble_candidates_from_impls(selcx, obligation, &mut candidates);
1220 ProjectionCandidateSet::Single(candidate) => {
1221 Ok(Projected::Progress(confirm_candidate(selcx, obligation, candidate)))
1223 ProjectionCandidateSet::None => Ok(Projected::NoProgress(
1224 // FIXME(associated_const_generics): this may need to change in the future?
1225 // need to investigate whether or not this is fine.
1228 .mk_projection(obligation.predicate.item_def_id, obligation.predicate.substs)
1231 // Error occurred while trying to processing impls.
1232 ProjectionCandidateSet::Error(e) => Err(ProjectionError::TraitSelectionError(e)),
1233 // Inherent ambiguity that prevents us from even enumerating the
1235 ProjectionCandidateSet::Ambiguous => Err(ProjectionError::TooManyCandidates),
1239 /// The first thing we have to do is scan through the parameter
1240 /// environment to see whether there are any projection predicates
1241 /// there that can answer this question.
1242 fn assemble_candidates_from_param_env<'cx, 'tcx>(
1243 selcx: &mut SelectionContext<'cx, 'tcx>,
1244 obligation: &ProjectionTyObligation<'tcx>,
1245 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1247 assemble_candidates_from_predicates(
1251 ProjectionCandidate::ParamEnv,
1252 obligation.param_env.caller_bounds().iter(),
1257 /// In the case of a nested projection like <<A as Foo>::FooT as Bar>::BarT, we may find
1258 /// that the definition of `Foo` has some clues:
1262 /// type FooT : Bar<BarT=i32>
1266 /// Here, for example, we could conclude that the result is `i32`.
1267 fn assemble_candidates_from_trait_def<'cx, 'tcx>(
1268 selcx: &mut SelectionContext<'cx, 'tcx>,
1269 obligation: &ProjectionTyObligation<'tcx>,
1270 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1272 debug!("assemble_candidates_from_trait_def(..)");
1274 let tcx = selcx.tcx();
1275 // Check whether the self-type is itself a projection.
1276 // If so, extract what we know from the trait and try to come up with a good answer.
1277 let bounds = match *obligation.predicate.self_ty().kind() {
1278 ty::Projection(ref data) => tcx.item_bounds(data.item_def_id).subst(tcx, data.substs),
1279 ty::Opaque(def_id, substs) => tcx.item_bounds(def_id).subst(tcx, substs),
1280 ty::Infer(ty::TyVar(_)) => {
1281 // If the self-type is an inference variable, then it MAY wind up
1282 // being a projected type, so induce an ambiguity.
1283 candidate_set.mark_ambiguous();
1289 assemble_candidates_from_predicates(
1293 ProjectionCandidate::TraitDef,
1299 /// In the case of a trait object like
1300 /// `<dyn Iterator<Item = ()> as Iterator>::Item` we can use the existential
1301 /// predicate in the trait object.
1303 /// We don't go through the select candidate for these bounds to avoid cycles:
1304 /// In the above case, `dyn Iterator<Item = ()>: Iterator` would create a
1305 /// nested obligation of `<dyn Iterator<Item = ()> as Iterator>::Item: Sized`,
1306 /// this then has to be normalized without having to prove
1307 /// `dyn Iterator<Item = ()>: Iterator` again.
1308 fn assemble_candidates_from_object_ty<'cx, 'tcx>(
1309 selcx: &mut SelectionContext<'cx, 'tcx>,
1310 obligation: &ProjectionTyObligation<'tcx>,
1311 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1313 debug!("assemble_candidates_from_object_ty(..)");
1315 let tcx = selcx.tcx();
1317 let self_ty = obligation.predicate.self_ty();
1318 let object_ty = selcx.infcx().shallow_resolve(self_ty);
1319 let data = match object_ty.kind() {
1320 ty::Dynamic(data, ..) => data,
1321 ty::Infer(ty::TyVar(_)) => {
1322 // If the self-type is an inference variable, then it MAY wind up
1323 // being an object type, so induce an ambiguity.
1324 candidate_set.mark_ambiguous();
1329 let env_predicates = data
1330 .projection_bounds()
1331 .filter(|bound| bound.item_def_id() == obligation.predicate.item_def_id)
1332 .map(|p| p.with_self_ty(tcx, object_ty).to_predicate(tcx));
1334 assemble_candidates_from_predicates(
1338 ProjectionCandidate::Object,
1344 #[tracing::instrument(
1346 skip(selcx, candidate_set, ctor, env_predicates, potentially_unnormalized_candidates)
1348 fn assemble_candidates_from_predicates<'cx, 'tcx>(
1349 selcx: &mut SelectionContext<'cx, 'tcx>,
1350 obligation: &ProjectionTyObligation<'tcx>,
1351 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1352 ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionCandidate<'tcx>,
1353 env_predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
1354 potentially_unnormalized_candidates: bool,
1356 let infcx = selcx.infcx();
1357 for predicate in env_predicates {
1358 let bound_predicate = predicate.kind();
1359 if let ty::PredicateKind::Projection(data) = predicate.kind().skip_binder() {
1360 let data = bound_predicate.rebind(data);
1361 if data.projection_def_id() != obligation.predicate.item_def_id {
1365 let is_match = infcx.probe(|_| {
1366 selcx.match_projection_projections(
1369 potentially_unnormalized_candidates,
1374 ProjectionMatchesProjection::Yes => {
1375 candidate_set.push_candidate(ctor(data));
1377 if potentially_unnormalized_candidates
1378 && !obligation.predicate.has_infer_types_or_consts()
1380 // HACK: Pick the first trait def candidate for a fully
1381 // inferred predicate. This is to allow duplicates that
1382 // differ only in normalization.
1386 ProjectionMatchesProjection::Ambiguous => {
1387 candidate_set.mark_ambiguous();
1389 ProjectionMatchesProjection::No => {}
1395 #[tracing::instrument(level = "debug", skip(selcx, obligation, candidate_set))]
1396 fn assemble_candidates_from_impls<'cx, 'tcx>(
1397 selcx: &mut SelectionContext<'cx, 'tcx>,
1398 obligation: &ProjectionTyObligation<'tcx>,
1399 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1401 // If we are resolving `<T as TraitRef<...>>::Item == Type`,
1402 // start out by selecting the predicate `T as TraitRef<...>`:
1403 let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx()));
1404 let trait_obligation = obligation.with(poly_trait_ref.to_poly_trait_predicate());
1405 let _ = selcx.infcx().commit_if_ok(|_| {
1406 let impl_source = match selcx.select(&trait_obligation) {
1407 Ok(Some(impl_source)) => impl_source,
1409 candidate_set.mark_ambiguous();
1413 debug!(error = ?e, "selection error");
1414 candidate_set.mark_error(e);
1419 let eligible = match &impl_source {
1420 super::ImplSource::Closure(_)
1421 | super::ImplSource::Generator(_)
1422 | super::ImplSource::FnPointer(_)
1423 | super::ImplSource::TraitAlias(_) => true,
1424 super::ImplSource::UserDefined(impl_data) => {
1425 // We have to be careful when projecting out of an
1426 // impl because of specialization. If we are not in
1427 // codegen (i.e., projection mode is not "any"), and the
1428 // impl's type is declared as default, then we disable
1429 // projection (even if the trait ref is fully
1430 // monomorphic). In the case where trait ref is not
1431 // fully monomorphic (i.e., includes type parameters),
1432 // this is because those type parameters may
1433 // ultimately be bound to types from other crates that
1434 // may have specialized impls we can't see. In the
1435 // case where the trait ref IS fully monomorphic, this
1436 // is a policy decision that we made in the RFC in
1437 // order to preserve flexibility for the crate that
1438 // defined the specializable impl to specialize later
1439 // for existing types.
1441 // In either case, we handle this by not adding a
1442 // candidate for an impl if it contains a `default`
1445 // NOTE: This should be kept in sync with the similar code in
1446 // `rustc_ty_utils::instance::resolve_associated_item()`.
1448 assoc_def(selcx, impl_data.impl_def_id, obligation.predicate.item_def_id)
1449 .map_err(|ErrorGuaranteed { .. }| ())?;
1451 if node_item.is_final() {
1452 // Non-specializable items are always projectable.
1455 // Only reveal a specializable default if we're past type-checking
1456 // and the obligation is monomorphic, otherwise passes such as
1457 // transmute checking and polymorphic MIR optimizations could
1458 // get a result which isn't correct for all monomorphizations.
1459 if obligation.param_env.reveal() == Reveal::All {
1460 // NOTE(eddyb) inference variables can resolve to parameters, so
1461 // assume `poly_trait_ref` isn't monomorphic, if it contains any.
1462 let poly_trait_ref = selcx.infcx().resolve_vars_if_possible(poly_trait_ref);
1463 !poly_trait_ref.still_further_specializable()
1466 assoc_ty = ?selcx.tcx().def_path_str(node_item.item.def_id),
1467 ?obligation.predicate,
1468 "assemble_candidates_from_impls: not eligible due to default",
1474 super::ImplSource::DiscriminantKind(..) => {
1475 // While `DiscriminantKind` is automatically implemented for every type,
1476 // the concrete discriminant may not be known yet.
1478 // Any type with multiple potential discriminant types is therefore not eligible.
1479 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1481 match self_ty.kind() {
1499 | ty::GeneratorWitness(..)
1502 // Integers and floats always have `u8` as their discriminant.
1503 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1509 | ty::Placeholder(..)
1511 | ty::Error(_) => false,
1514 super::ImplSource::Pointee(..) => {
1515 // While `Pointee` is automatically implemented for every type,
1516 // the concrete metadata type may not be known yet.
1518 // Any type with multiple potential metadata types is therefore not eligible.
1519 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1521 let tail = selcx.tcx().struct_tail_with_normalize(self_ty, |ty| {
1522 // We throw away any obligations we get from this, since we normalize
1523 // and confirm these obligations once again during confirmation
1524 normalize_with_depth(
1526 obligation.param_env,
1527 obligation.cause.clone(),
1528 obligation.recursion_depth + 1,
1550 | ty::GeneratorWitness(..)
1552 // Extern types have unit metadata, according to RFC 2850
1554 // If returned by `struct_tail_without_normalization` this is a unit struct
1555 // without any fields, or not a struct, and therefore is Sized.
1557 // If returned by `struct_tail_without_normalization` this is the empty tuple.
1559 // Integers and floats are always Sized, and so have unit type metadata.
1560 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1562 // type parameters, opaques, and unnormalized projections have pointer
1563 // metadata if they're known (e.g. by the param_env) to be sized
1564 ty::Param(_) | ty::Projection(..) | ty::Opaque(..)
1565 if tail.is_sized(selcx.tcx().at(obligation.cause.span), obligation.param_env) =>
1570 // FIXME(compiler-errors): are Bound and Placeholder types ever known sized?
1572 | ty::Projection(..)
1575 | ty::Placeholder(..)
1578 if tail.has_infer_types() {
1579 candidate_set.mark_ambiguous();
1585 super::ImplSource::Param(..) => {
1586 // This case tell us nothing about the value of an
1587 // associated type. Consider:
1590 // trait SomeTrait { type Foo; }
1591 // fn foo<T:SomeTrait>(...) { }
1594 // If the user writes `<T as SomeTrait>::Foo`, then the `T
1595 // : SomeTrait` binding does not help us decide what the
1596 // type `Foo` is (at least, not more specifically than
1597 // what we already knew).
1599 // But wait, you say! What about an example like this:
1602 // fn bar<T:SomeTrait<Foo=usize>>(...) { ... }
1605 // Doesn't the `T : SomeTrait<Foo=usize>` predicate help
1606 // resolve `T::Foo`? And of course it does, but in fact
1607 // that single predicate is desugared into two predicates
1608 // in the compiler: a trait predicate (`T : SomeTrait`) and a
1609 // projection. And the projection where clause is handled
1610 // in `assemble_candidates_from_param_env`.
1613 super::ImplSource::Object(_) => {
1614 // Handled by the `Object` projection candidate. See
1615 // `assemble_candidates_from_object_ty` for an explanation of
1616 // why we special case object types.
1619 super::ImplSource::AutoImpl(..)
1620 | super::ImplSource::Builtin(..)
1621 | super::ImplSource::TraitUpcasting(_)
1622 | super::ImplSource::ConstDestruct(_) => {
1623 // These traits have no associated types.
1624 selcx.tcx().sess.delay_span_bug(
1625 obligation.cause.span,
1626 &format!("Cannot project an associated type from `{:?}`", impl_source),
1633 if candidate_set.push_candidate(ProjectionCandidate::Select(impl_source)) {
1644 fn confirm_candidate<'cx, 'tcx>(
1645 selcx: &mut SelectionContext<'cx, 'tcx>,
1646 obligation: &ProjectionTyObligation<'tcx>,
1647 candidate: ProjectionCandidate<'tcx>,
1648 ) -> Progress<'tcx> {
1649 debug!(?obligation, ?candidate, "confirm_candidate");
1650 let mut progress = match candidate {
1651 ProjectionCandidate::ParamEnv(poly_projection)
1652 | ProjectionCandidate::Object(poly_projection) => {
1653 confirm_param_env_candidate(selcx, obligation, poly_projection, false)
1656 ProjectionCandidate::TraitDef(poly_projection) => {
1657 confirm_param_env_candidate(selcx, obligation, poly_projection, true)
1660 ProjectionCandidate::Select(impl_source) => {
1661 confirm_select_candidate(selcx, obligation, impl_source)
1665 // When checking for cycle during evaluation, we compare predicates with
1666 // "syntactic" equality. Since normalization generally introduces a type
1667 // with new region variables, we need to resolve them to existing variables
1668 // when possible for this to work. See `auto-trait-projection-recursion.rs`
1669 // for a case where this matters.
1670 if progress.term.has_infer_regions() {
1672 progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx()));
1677 fn confirm_select_candidate<'cx, 'tcx>(
1678 selcx: &mut SelectionContext<'cx, 'tcx>,
1679 obligation: &ProjectionTyObligation<'tcx>,
1680 impl_source: Selection<'tcx>,
1681 ) -> Progress<'tcx> {
1683 super::ImplSource::UserDefined(data) => confirm_impl_candidate(selcx, obligation, data),
1684 super::ImplSource::Generator(data) => confirm_generator_candidate(selcx, obligation, data),
1685 super::ImplSource::Closure(data) => confirm_closure_candidate(selcx, obligation, data),
1686 super::ImplSource::FnPointer(data) => confirm_fn_pointer_candidate(selcx, obligation, data),
1687 super::ImplSource::DiscriminantKind(data) => {
1688 confirm_discriminant_kind_candidate(selcx, obligation, data)
1690 super::ImplSource::Pointee(data) => confirm_pointee_candidate(selcx, obligation, data),
1691 super::ImplSource::Object(_)
1692 | super::ImplSource::AutoImpl(..)
1693 | super::ImplSource::Param(..)
1694 | super::ImplSource::Builtin(..)
1695 | super::ImplSource::TraitUpcasting(_)
1696 | super::ImplSource::TraitAlias(..)
1697 | super::ImplSource::ConstDestruct(_) => {
1698 // we don't create Select candidates with this kind of resolution
1700 obligation.cause.span,
1701 "Cannot project an associated type from `{:?}`",
1708 fn confirm_generator_candidate<'cx, 'tcx>(
1709 selcx: &mut SelectionContext<'cx, 'tcx>,
1710 obligation: &ProjectionTyObligation<'tcx>,
1711 impl_source: ImplSourceGeneratorData<'tcx, PredicateObligation<'tcx>>,
1712 ) -> Progress<'tcx> {
1713 let gen_sig = impl_source.substs.as_generator().poly_sig();
1714 let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1716 obligation.param_env,
1717 obligation.cause.clone(),
1718 obligation.recursion_depth + 1,
1722 debug!(?obligation, ?gen_sig, ?obligations, "confirm_generator_candidate");
1724 let tcx = selcx.tcx();
1726 let gen_def_id = tcx.require_lang_item(LangItem::Generator, None);
1728 let predicate = super::util::generator_trait_ref_and_outputs(
1731 obligation.predicate.self_ty(),
1734 .map_bound(|(trait_ref, yield_ty, return_ty)| {
1735 let name = tcx.associated_item(obligation.predicate.item_def_id).name;
1736 let ty = if name == sym::Return {
1738 } else if name == sym::Yield {
1744 ty::ProjectionPredicate {
1745 projection_ty: ty::ProjectionTy {
1746 substs: trait_ref.substs,
1747 item_def_id: obligation.predicate.item_def_id,
1753 confirm_param_env_candidate(selcx, obligation, predicate, false)
1754 .with_addl_obligations(impl_source.nested)
1755 .with_addl_obligations(obligations)
1758 fn confirm_discriminant_kind_candidate<'cx, 'tcx>(
1759 selcx: &mut SelectionContext<'cx, 'tcx>,
1760 obligation: &ProjectionTyObligation<'tcx>,
1761 _: ImplSourceDiscriminantKindData,
1762 ) -> Progress<'tcx> {
1763 let tcx = selcx.tcx();
1765 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1766 // We get here from `poly_project_and_unify_type` which replaces bound vars
1767 // with placeholders
1768 debug_assert!(!self_ty.has_escaping_bound_vars());
1769 let substs = tcx.mk_substs([self_ty.into()].iter());
1771 let discriminant_def_id = tcx.require_lang_item(LangItem::Discriminant, None);
1773 let predicate = ty::ProjectionPredicate {
1774 projection_ty: ty::ProjectionTy { substs, item_def_id: discriminant_def_id },
1775 term: self_ty.discriminant_ty(tcx).into(),
1778 // We get here from `poly_project_and_unify_type` which replaces bound vars
1779 // with placeholders, so dummy is okay here.
1780 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1783 fn confirm_pointee_candidate<'cx, 'tcx>(
1784 selcx: &mut SelectionContext<'cx, 'tcx>,
1785 obligation: &ProjectionTyObligation<'tcx>,
1786 _: ImplSourcePointeeData,
1787 ) -> Progress<'tcx> {
1788 let tcx = selcx.tcx();
1789 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1791 let mut obligations = vec![];
1792 let (metadata_ty, check_is_sized) = self_ty.ptr_metadata_ty(tcx, |ty| {
1793 normalize_with_depth_to(
1795 obligation.param_env,
1796 obligation.cause.clone(),
1797 obligation.recursion_depth + 1,
1803 let sized_predicate = ty::Binder::dummy(ty::TraitRef::new(
1804 tcx.require_lang_item(LangItem::Sized, None),
1805 tcx.mk_substs_trait(self_ty, &[]),
1809 obligations.push(Obligation::new(
1810 obligation.cause.clone(),
1811 obligation.param_env,
1816 let substs = tcx.mk_substs([self_ty.into()].iter());
1817 let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
1819 let predicate = ty::ProjectionPredicate {
1820 projection_ty: ty::ProjectionTy { substs, item_def_id: metadata_def_id },
1821 term: metadata_ty.into(),
1824 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1825 .with_addl_obligations(obligations)
1828 fn confirm_fn_pointer_candidate<'cx, 'tcx>(
1829 selcx: &mut SelectionContext<'cx, 'tcx>,
1830 obligation: &ProjectionTyObligation<'tcx>,
1831 fn_pointer_impl_source: ImplSourceFnPointerData<'tcx, PredicateObligation<'tcx>>,
1832 ) -> Progress<'tcx> {
1833 let fn_type = selcx.infcx().shallow_resolve(fn_pointer_impl_source.fn_ty);
1834 let sig = fn_type.fn_sig(selcx.tcx());
1835 let Normalized { value: sig, obligations } = normalize_with_depth(
1837 obligation.param_env,
1838 obligation.cause.clone(),
1839 obligation.recursion_depth + 1,
1843 confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
1844 .with_addl_obligations(fn_pointer_impl_source.nested)
1845 .with_addl_obligations(obligations)
1848 fn confirm_closure_candidate<'cx, 'tcx>(
1849 selcx: &mut SelectionContext<'cx, 'tcx>,
1850 obligation: &ProjectionTyObligation<'tcx>,
1851 impl_source: ImplSourceClosureData<'tcx, PredicateObligation<'tcx>>,
1852 ) -> Progress<'tcx> {
1853 let closure_sig = impl_source.substs.as_closure().sig();
1854 let Normalized { value: closure_sig, obligations } = normalize_with_depth(
1856 obligation.param_env,
1857 obligation.cause.clone(),
1858 obligation.recursion_depth + 1,
1862 debug!(?obligation, ?closure_sig, ?obligations, "confirm_closure_candidate");
1864 confirm_callable_candidate(selcx, obligation, closure_sig, util::TupleArgumentsFlag::No)
1865 .with_addl_obligations(impl_source.nested)
1866 .with_addl_obligations(obligations)
1869 fn confirm_callable_candidate<'cx, 'tcx>(
1870 selcx: &mut SelectionContext<'cx, 'tcx>,
1871 obligation: &ProjectionTyObligation<'tcx>,
1872 fn_sig: ty::PolyFnSig<'tcx>,
1873 flag: util::TupleArgumentsFlag,
1874 ) -> Progress<'tcx> {
1875 let tcx = selcx.tcx();
1877 debug!(?obligation, ?fn_sig, "confirm_callable_candidate");
1879 let fn_once_def_id = tcx.require_lang_item(LangItem::FnOnce, None);
1880 let fn_once_output_def_id = tcx.require_lang_item(LangItem::FnOnceOutput, None);
1882 let predicate = super::util::closure_trait_ref_and_return_type(
1885 obligation.predicate.self_ty(),
1889 .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
1890 projection_ty: ty::ProjectionTy {
1891 substs: trait_ref.substs,
1892 item_def_id: fn_once_output_def_id,
1894 term: ret_type.into(),
1897 confirm_param_env_candidate(selcx, obligation, predicate, true)
1900 fn confirm_param_env_candidate<'cx, 'tcx>(
1901 selcx: &mut SelectionContext<'cx, 'tcx>,
1902 obligation: &ProjectionTyObligation<'tcx>,
1903 poly_cache_entry: ty::PolyProjectionPredicate<'tcx>,
1904 potentially_unnormalized_candidate: bool,
1905 ) -> Progress<'tcx> {
1906 let infcx = selcx.infcx();
1907 let cause = &obligation.cause;
1908 let param_env = obligation.param_env;
1910 let (cache_entry, _) = infcx.replace_bound_vars_with_fresh_vars(
1912 LateBoundRegionConversionTime::HigherRankedType,
1916 let cache_projection = cache_entry.projection_ty;
1917 let mut nested_obligations = Vec::new();
1918 let obligation_projection = obligation.predicate;
1919 let obligation_projection = ensure_sufficient_stack(|| {
1920 normalize_with_depth_to(
1922 obligation.param_env,
1923 obligation.cause.clone(),
1924 obligation.recursion_depth + 1,
1925 obligation_projection,
1926 &mut nested_obligations,
1929 let cache_projection = if potentially_unnormalized_candidate {
1930 ensure_sufficient_stack(|| {
1931 normalize_with_depth_to(
1933 obligation.param_env,
1934 obligation.cause.clone(),
1935 obligation.recursion_depth + 1,
1937 &mut nested_obligations,
1944 debug!(?cache_projection, ?obligation_projection);
1946 match infcx.at(cause, param_env).eq(cache_projection, obligation_projection) {
1947 Ok(InferOk { value: _, obligations }) => {
1948 nested_obligations.extend(obligations);
1949 assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations);
1950 // FIXME(associated_const_equality): Handle consts here as well? Maybe this progress type should just take
1952 Progress { term: cache_entry.term, obligations: nested_obligations }
1956 "Failed to unify obligation `{:?}` with poly_projection `{:?}`: {:?}",
1957 obligation, poly_cache_entry, e,
1959 debug!("confirm_param_env_candidate: {}", msg);
1960 let err = infcx.tcx.ty_error_with_message(obligation.cause.span, &msg);
1961 Progress { term: err.into(), obligations: vec![] }
1966 fn confirm_impl_candidate<'cx, 'tcx>(
1967 selcx: &mut SelectionContext<'cx, 'tcx>,
1968 obligation: &ProjectionTyObligation<'tcx>,
1969 impl_impl_source: ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>,
1970 ) -> Progress<'tcx> {
1971 let tcx = selcx.tcx();
1973 let ImplSourceUserDefinedData { impl_def_id, substs, mut nested } = impl_impl_source;
1974 let assoc_item_id = obligation.predicate.item_def_id;
1975 let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
1977 let param_env = obligation.param_env;
1978 let Ok(assoc_ty) = assoc_def(selcx, impl_def_id, assoc_item_id) else {
1979 return Progress { term: tcx.ty_error().into(), obligations: nested };
1982 if !assoc_ty.item.defaultness.has_value() {
1983 // This means that the impl is missing a definition for the
1984 // associated type. This error will be reported by the type
1985 // checker method `check_impl_items_against_trait`, so here we
1986 // just return Error.
1988 "confirm_impl_candidate: no associated type {:?} for {:?}",
1989 assoc_ty.item.name, obligation.predicate
1991 return Progress { term: tcx.ty_error().into(), obligations: nested };
1993 // If we're trying to normalize `<Vec<u32> as X>::A<S>` using
1994 //`impl<T> X for Vec<T> { type A<Y> = Box<Y>; }`, then:
1996 // * `obligation.predicate.substs` is `[Vec<u32>, S]`
1997 // * `substs` is `[u32]`
1998 // * `substs` ends up as `[u32, S]`
1999 let substs = obligation.predicate.substs.rebase_onto(tcx, trait_def_id, substs);
2001 translate_substs(selcx.infcx(), param_env, impl_def_id, substs, assoc_ty.defining_node);
2002 let ty = tcx.type_of(assoc_ty.item.def_id);
2003 let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst);
2004 let term: ty::Term<'tcx> = if is_const {
2005 let identity_substs =
2006 crate::traits::InternalSubsts::identity_for_item(tcx, assoc_ty.item.def_id);
2007 let did = ty::WithOptConstParam::unknown(assoc_ty.item.def_id);
2008 let val = ty::ConstKind::Unevaluated(ty::Unevaluated::new(did, identity_substs));
2009 tcx.mk_const(ty::ConstS { ty, val }).into()
2013 if substs.len() != tcx.generics_of(assoc_ty.item.def_id).count() {
2014 let err = tcx.ty_error_with_message(
2015 obligation.cause.span,
2016 "impl item and trait item have different parameter counts",
2018 Progress { term: err.into(), obligations: nested }
2020 assoc_ty_own_obligations(selcx, obligation, &mut nested);
2021 Progress { term: term.subst(tcx, substs), obligations: nested }
2025 // Get obligations corresponding to the predicates from the where-clause of the
2026 // associated type itself.
2027 // Note: `feature(generic_associated_types)` is required to write such
2028 // predicates, even for non-generic associated types.
2029 fn assoc_ty_own_obligations<'cx, 'tcx>(
2030 selcx: &mut SelectionContext<'cx, 'tcx>,
2031 obligation: &ProjectionTyObligation<'tcx>,
2032 nested: &mut Vec<PredicateObligation<'tcx>>,
2034 let tcx = selcx.tcx();
2035 for predicate in tcx
2036 .predicates_of(obligation.predicate.item_def_id)
2037 .instantiate_own(tcx, obligation.predicate.substs)
2040 let normalized = normalize_with_depth_to(
2042 obligation.param_env,
2043 obligation.cause.clone(),
2044 obligation.recursion_depth + 1,
2048 nested.push(Obligation::with_depth(
2049 obligation.cause.clone(),
2050 obligation.recursion_depth + 1,
2051 obligation.param_env,
2057 /// Locate the definition of an associated type in the specialization hierarchy,
2058 /// starting from the given impl.
2060 /// Based on the "projection mode", this lookup may in fact only examine the
2061 /// topmost impl. See the comments for `Reveal` for more details.
2063 selcx: &SelectionContext<'_, '_>,
2065 assoc_def_id: DefId,
2066 ) -> Result<specialization_graph::LeafDef, ErrorGuaranteed> {
2067 let tcx = selcx.tcx();
2068 let trait_def_id = tcx.impl_trait_ref(impl_def_id).unwrap().def_id;
2069 let trait_def = tcx.trait_def(trait_def_id);
2071 // This function may be called while we are still building the
2072 // specialization graph that is queried below (via TraitDef::ancestors()),
2073 // so, in order to avoid unnecessary infinite recursion, we manually look
2074 // for the associated item at the given impl.
2075 // If there is no such item in that impl, this function will fail with a
2076 // cycle error if the specialization graph is currently being built.
2077 if let Some(&impl_item_id) = tcx.impl_item_implementor_ids(impl_def_id).get(&assoc_def_id) {
2078 let item = tcx.associated_item(impl_item_id);
2079 let impl_node = specialization_graph::Node::Impl(impl_def_id);
2080 return Ok(specialization_graph::LeafDef {
2082 defining_node: impl_node,
2083 finalizing_node: if item.defaultness.is_default() { None } else { Some(impl_node) },
2087 let ancestors = trait_def.ancestors(tcx, impl_def_id)?;
2088 if let Some(assoc_item) = ancestors.leaf_def(tcx, assoc_def_id) {
2091 // This is saying that neither the trait nor
2092 // the impl contain a definition for this
2093 // associated type. Normally this situation
2094 // could only arise through a compiler bug --
2095 // if the user wrote a bad item name, it
2096 // should have failed in astconv.
2098 "No associated type `{}` for {}",
2099 tcx.item_name(assoc_def_id),
2100 tcx.def_path_str(impl_def_id)
2105 crate trait ProjectionCacheKeyExt<'cx, 'tcx>: Sized {
2106 fn from_poly_projection_predicate(
2107 selcx: &mut SelectionContext<'cx, 'tcx>,
2108 predicate: ty::PolyProjectionPredicate<'tcx>,
2112 impl<'cx, 'tcx> ProjectionCacheKeyExt<'cx, 'tcx> for ProjectionCacheKey<'tcx> {
2113 fn from_poly_projection_predicate(
2114 selcx: &mut SelectionContext<'cx, 'tcx>,
2115 predicate: ty::PolyProjectionPredicate<'tcx>,
2117 let infcx = selcx.infcx();
2118 // We don't do cross-snapshot caching of obligations with escaping regions,
2119 // so there's no cache key to use
2120 predicate.no_bound_vars().map(|predicate| {
2121 ProjectionCacheKey::new(
2122 // We don't attempt to match up with a specific type-variable state
2123 // from a specific call to `opt_normalize_projection_type` - if
2124 // there's no precise match, the original cache entry is "stranded"
2126 infcx.resolve_vars_if_possible(predicate.projection_ty),