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::query::evaluate_obligation::InferCtxtExt as _;
23 use crate::traits::select::ProjectionMatchesProjection;
24 use rustc_data_structures::sso::SsoHashSet;
25 use rustc_data_structures::stack::ensure_sufficient_stack;
26 use rustc_errors::ErrorGuaranteed;
27 use rustc_hir::def::DefKind;
28 use rustc_hir::def_id::DefId;
29 use rustc_hir::lang_items::LangItem;
30 use rustc_infer::infer::resolve::OpportunisticRegionResolver;
31 use rustc_middle::traits::select::OverflowError;
32 use rustc_middle::ty::fold::{MaxUniverse, TypeFoldable, TypeFolder};
33 use rustc_middle::ty::subst::Subst;
34 use rustc_middle::ty::{self, Term, ToPredicate, Ty, TyCtxt};
35 use rustc_span::symbol::sym;
37 use std::collections::BTreeMap;
39 pub use rustc_middle::traits::Reveal;
41 pub type PolyProjectionObligation<'tcx> = Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
43 pub type ProjectionObligation<'tcx> = Obligation<'tcx, ty::ProjectionPredicate<'tcx>>;
45 pub type ProjectionTyObligation<'tcx> = Obligation<'tcx, ty::ProjectionTy<'tcx>>;
47 pub(super) struct InProgress;
49 /// When attempting to resolve `<T as TraitRef>::Name` ...
51 pub enum ProjectionError<'tcx> {
52 /// ...we found multiple sources of information and couldn't resolve the ambiguity.
55 /// ...an error occurred matching `T : TraitRef`
56 TraitSelectionError(SelectionError<'tcx>),
59 #[derive(PartialEq, Eq, Debug)]
60 enum ProjectionCandidate<'tcx> {
61 /// From a where-clause in the env or object type
62 ParamEnv(ty::PolyProjectionPredicate<'tcx>),
64 /// From the definition of `Trait` when you have something like <<A as Trait>::B as Trait2>::C
65 TraitDef(ty::PolyProjectionPredicate<'tcx>),
67 /// Bounds specified on an object type
68 Object(ty::PolyProjectionPredicate<'tcx>),
70 /// From an "impl" (or a "pseudo-impl" returned by select)
71 Select(Selection<'tcx>),
74 enum ProjectionCandidateSet<'tcx> {
76 Single(ProjectionCandidate<'tcx>),
78 Error(SelectionError<'tcx>),
81 impl<'tcx> ProjectionCandidateSet<'tcx> {
82 fn mark_ambiguous(&mut self) {
83 *self = ProjectionCandidateSet::Ambiguous;
86 fn mark_error(&mut self, err: SelectionError<'tcx>) {
87 *self = ProjectionCandidateSet::Error(err);
90 // Returns true if the push was successful, or false if the candidate
91 // was discarded -- this could be because of ambiguity, or because
92 // a higher-priority candidate is already there.
93 fn push_candidate(&mut self, candidate: ProjectionCandidate<'tcx>) -> bool {
94 use self::ProjectionCandidate::*;
95 use self::ProjectionCandidateSet::*;
97 // This wacky variable is just used to try and
98 // make code readable and avoid confusing paths.
99 // It is assigned a "value" of `()` only on those
100 // paths in which we wish to convert `*self` to
101 // ambiguous (and return false, because the candidate
102 // was not used). On other paths, it is not assigned,
103 // and hence if those paths *could* reach the code that
104 // comes after the match, this fn would not compile.
105 let convert_to_ambiguous;
109 *self = Single(candidate);
114 // Duplicates can happen inside ParamEnv. In the case, we
115 // perform a lazy deduplication.
116 if current == &candidate {
120 // Prefer where-clauses. As in select, if there are multiple
121 // candidates, we prefer where-clause candidates over impls. This
122 // may seem a bit surprising, since impls are the source of
123 // "truth" in some sense, but in fact some of the impls that SEEM
124 // applicable are not, because of nested obligations. Where
125 // clauses are the safer choice. See the comment on
126 // `select::SelectionCandidate` and #21974 for more details.
127 match (current, candidate) {
128 (ParamEnv(..), ParamEnv(..)) => convert_to_ambiguous = (),
129 (ParamEnv(..), _) => return false,
130 (_, ParamEnv(..)) => unreachable!(),
131 (_, _) => convert_to_ambiguous = (),
135 Ambiguous | Error(..) => {
140 // We only ever get here when we moved from a single candidate
142 let () = convert_to_ambiguous;
148 /// Takes the place of a
150 /// Result<Option<Vec<PredicateObligation<'tcx>>>, InProgress>,
151 /// MismatchedProjectionTypes<'tcx>,
153 pub(super) enum ProjectAndUnifyResult<'tcx> {
154 Holds(Vec<PredicateObligation<'tcx>>),
157 MismatchedProjectionTypes(MismatchedProjectionTypes<'tcx>),
160 /// Evaluates constraints of the form:
162 /// for<...> <T as Trait>::U == V
164 /// If successful, this may result in additional obligations. Also returns
165 /// the projection cache key used to track these additional obligations.
169 /// - `Err(_)`: the projection can be normalized, but is not equal to the
171 /// - `Ok(Err(InProgress))`: this is called recursively while normalizing
172 /// the same projection.
173 /// - `Ok(Ok(None))`: The projection cannot be normalized due to ambiguity
174 /// (resolving some inference variables in the projection may fix this).
175 /// - `Ok(Ok(Some(obligations)))`: The projection bound holds subject to
176 /// the given obligations. If the projection cannot be normalized because
177 /// the required trait bound doesn't hold this returned with `obligations`
178 /// being a predicate that cannot be proven.
179 #[instrument(level = "debug", skip(selcx))]
180 pub(super) fn poly_project_and_unify_type<'cx, 'tcx>(
181 selcx: &mut SelectionContext<'cx, 'tcx>,
182 obligation: &PolyProjectionObligation<'tcx>,
183 ) -> ProjectAndUnifyResult<'tcx> {
184 let infcx = selcx.infcx();
185 let r = infcx.commit_if_ok(|_snapshot| {
186 let old_universe = infcx.universe();
187 let placeholder_predicate =
188 infcx.replace_bound_vars_with_placeholders(obligation.predicate);
189 let new_universe = infcx.universe();
191 let placeholder_obligation = obligation.with(placeholder_predicate);
192 match project_and_unify_type(selcx, &placeholder_obligation) {
193 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => Err(e),
194 ProjectAndUnifyResult::Holds(obligations)
195 if old_universe != new_universe
196 && selcx.tcx().features().generic_associated_types_extended =>
198 // If the `generic_associated_types_extended` feature is active, then we ignore any
199 // obligations references lifetimes from any universe greater than or equal to the
200 // universe just created. Otherwise, we can end up with something like `for<'a> I: 'a`,
201 // which isn't quite what we want. Ideally, we want either an implied
202 // `for<'a where I: 'a> I: 'a` or we want to "lazily" check these hold when we
203 // substitute concrete regions. There is design work to be done here; until then,
204 // however, this allows experimenting potential GAT features without running into
205 // well-formedness issues.
206 let new_obligations = obligations
208 .filter(|obligation| {
209 let mut visitor = MaxUniverse::new();
210 obligation.predicate.visit_with(&mut visitor);
211 visitor.max_universe() < new_universe
214 Ok(ProjectAndUnifyResult::Holds(new_obligations))
222 Err(err) => ProjectAndUnifyResult::MismatchedProjectionTypes(err),
226 /// Evaluates constraints of the form:
228 /// <T as Trait>::U == V
230 /// If successful, this may result in additional obligations.
232 /// See [poly_project_and_unify_type] for an explanation of the return value.
233 #[tracing::instrument(level = "debug", skip(selcx))]
234 fn project_and_unify_type<'cx, 'tcx>(
235 selcx: &mut SelectionContext<'cx, 'tcx>,
236 obligation: &ProjectionObligation<'tcx>,
237 ) -> ProjectAndUnifyResult<'tcx> {
238 let mut obligations = vec![];
240 let infcx = selcx.infcx();
241 let normalized = match opt_normalize_projection_type(
243 obligation.param_env,
244 obligation.predicate.projection_ty,
245 obligation.cause.clone(),
246 obligation.recursion_depth,
250 Ok(None) => return ProjectAndUnifyResult::FailedNormalization,
251 Err(InProgress) => return ProjectAndUnifyResult::Recursive,
253 debug!(?normalized, ?obligations, "project_and_unify_type result");
254 let actual = obligation.predicate.term;
255 // HACK: lazy TAIT would regress src/test/ui/impl-trait/nested-return-type2.rs, so we add
256 // a back-compat hack hat converts the RPITs into inference vars, just like they were before
258 // This does not affect TAITs in general, as tested in the nested-return-type-tait* tests.
259 let InferOk { value: actual, obligations: new } =
260 selcx.infcx().replace_opaque_types_with_inference_vars(
262 obligation.cause.body_id,
263 obligation.cause.span,
264 obligation.param_env,
266 obligations.extend(new);
268 match infcx.at(&obligation.cause, obligation.param_env).eq(normalized, actual) {
269 Ok(InferOk { obligations: inferred_obligations, value: () }) => {
270 obligations.extend(inferred_obligations);
271 ProjectAndUnifyResult::Holds(obligations)
274 debug!("equating types encountered error {:?}", err);
275 ProjectAndUnifyResult::MismatchedProjectionTypes(MismatchedProjectionTypes { err })
280 /// Normalizes any associated type projections in `value`, replacing
281 /// them with a fully resolved type where possible. The return value
282 /// combines the normalized result and any additional obligations that
283 /// were incurred as result.
284 pub fn normalize<'a, 'b, 'tcx, T>(
285 selcx: &'a mut SelectionContext<'b, 'tcx>,
286 param_env: ty::ParamEnv<'tcx>,
287 cause: ObligationCause<'tcx>,
289 ) -> Normalized<'tcx, T>
291 T: TypeFoldable<'tcx>,
293 let mut obligations = Vec::new();
294 let value = normalize_to(selcx, param_env, cause, value, &mut obligations);
295 Normalized { value, obligations }
298 pub fn normalize_to<'a, 'b, 'tcx, T>(
299 selcx: &'a mut SelectionContext<'b, 'tcx>,
300 param_env: ty::ParamEnv<'tcx>,
301 cause: ObligationCause<'tcx>,
303 obligations: &mut Vec<PredicateObligation<'tcx>>,
306 T: TypeFoldable<'tcx>,
308 normalize_with_depth_to(selcx, param_env, cause, 0, value, obligations)
311 /// As `normalize`, but with a custom depth.
312 pub fn normalize_with_depth<'a, 'b, 'tcx, T>(
313 selcx: &'a mut SelectionContext<'b, 'tcx>,
314 param_env: ty::ParamEnv<'tcx>,
315 cause: ObligationCause<'tcx>,
318 ) -> Normalized<'tcx, T>
320 T: TypeFoldable<'tcx>,
322 let mut obligations = Vec::new();
323 let value = normalize_with_depth_to(selcx, param_env, cause, depth, value, &mut obligations);
324 Normalized { value, obligations }
327 #[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
328 pub fn normalize_with_depth_to<'a, 'b, 'tcx, T>(
329 selcx: &'a mut SelectionContext<'b, 'tcx>,
330 param_env: ty::ParamEnv<'tcx>,
331 cause: ObligationCause<'tcx>,
334 obligations: &mut Vec<PredicateObligation<'tcx>>,
337 T: TypeFoldable<'tcx>,
339 debug!(obligations.len = obligations.len());
340 let mut normalizer = AssocTypeNormalizer::new(selcx, param_env, cause, depth, obligations);
341 let result = ensure_sufficient_stack(|| normalizer.fold(value));
342 debug!(?result, obligations.len = normalizer.obligations.len());
343 debug!(?normalizer.obligations,);
347 #[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
348 pub fn try_normalize_with_depth_to<'a, 'b, 'tcx, T>(
349 selcx: &'a mut SelectionContext<'b, 'tcx>,
350 param_env: ty::ParamEnv<'tcx>,
351 cause: ObligationCause<'tcx>,
354 obligations: &mut Vec<PredicateObligation<'tcx>>,
357 T: TypeFoldable<'tcx>,
359 debug!(obligations.len = obligations.len());
360 let mut normalizer = AssocTypeNormalizer::new_without_eager_inference_replacement(
367 let result = ensure_sufficient_stack(|| normalizer.fold(value));
368 debug!(?result, obligations.len = normalizer.obligations.len());
369 debug!(?normalizer.obligations,);
373 pub(crate) fn needs_normalization<'tcx, T: TypeFoldable<'tcx>>(value: &T, reveal: Reveal) -> bool {
375 Reveal::UserFacing => value
376 .has_type_flags(ty::TypeFlags::HAS_TY_PROJECTION | ty::TypeFlags::HAS_CT_PROJECTION),
377 Reveal::All => value.has_type_flags(
378 ty::TypeFlags::HAS_TY_PROJECTION
379 | ty::TypeFlags::HAS_TY_OPAQUE
380 | ty::TypeFlags::HAS_CT_PROJECTION,
385 struct AssocTypeNormalizer<'a, 'b, 'tcx> {
386 selcx: &'a mut SelectionContext<'b, 'tcx>,
387 param_env: ty::ParamEnv<'tcx>,
388 cause: ObligationCause<'tcx>,
389 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
391 universes: Vec<Option<ty::UniverseIndex>>,
392 /// If true, when a projection is unable to be completed, an inference
393 /// variable will be created and an obligation registered to project to that
394 /// inference variable. Also, constants will be eagerly evaluated.
395 eager_inference_replacement: bool,
398 impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
400 selcx: &'a mut SelectionContext<'b, 'tcx>,
401 param_env: ty::ParamEnv<'tcx>,
402 cause: ObligationCause<'tcx>,
404 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
405 ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
406 AssocTypeNormalizer {
413 eager_inference_replacement: true,
417 fn new_without_eager_inference_replacement(
418 selcx: &'a mut SelectionContext<'b, 'tcx>,
419 param_env: ty::ParamEnv<'tcx>,
420 cause: ObligationCause<'tcx>,
422 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
423 ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
424 AssocTypeNormalizer {
431 eager_inference_replacement: false,
435 fn fold<T: TypeFoldable<'tcx>>(&mut self, value: T) -> T {
436 let value = self.selcx.infcx().resolve_vars_if_possible(value);
440 !value.has_escaping_bound_vars(),
441 "Normalizing {:?} without wrapping in a `Binder`",
445 if !needs_normalization(&value, self.param_env.reveal()) {
448 value.fold_with(self)
453 impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
454 fn tcx<'c>(&'c self) -> TyCtxt<'tcx> {
458 fn fold_binder<T: TypeFoldable<'tcx>>(
460 t: ty::Binder<'tcx, T>,
461 ) -> ty::Binder<'tcx, T> {
462 self.universes.push(None);
463 let t = t.super_fold_with(self);
464 self.universes.pop();
468 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
469 if !needs_normalization(&ty, self.param_env.reveal()) {
473 // We try to be a little clever here as a performance optimization in
474 // cases where there are nested projections under binders.
477 // for<'a> fn(<T as Foo>::One<'a, Box<dyn Bar<'a, Item=<T as Foo>::Two<'a>>>>)
479 // We normalize the substs on the projection before the projecting, but
480 // if we're naive, we'll
481 // replace bound vars on inner, project inner, replace placeholders on inner,
482 // replace bound vars on outer, project outer, replace placeholders on outer
484 // However, if we're a bit more clever, we can replace the bound vars
485 // on the entire type before normalizing nested projections, meaning we
486 // replace bound vars on outer, project inner,
487 // project outer, replace placeholders on outer
489 // This is possible because the inner `'a` will already be a placeholder
490 // when we need to normalize the inner projection
492 // On the other hand, this does add a bit of complexity, since we only
493 // replace bound vars if the current type is a `Projection` and we need
494 // to make sure we don't forget to fold the substs regardless.
497 // This is really important. While we *can* handle this, this has
498 // severe performance implications for large opaque types with
499 // late-bound regions. See `issue-88862` benchmark.
500 ty::Opaque(def_id, substs) if !substs.has_escaping_bound_vars() => {
501 // Only normalize `impl Trait` outside of type inference, usually in codegen.
502 match self.param_env.reveal() {
503 Reveal::UserFacing => ty.super_fold_with(self),
506 let recursion_limit = self.tcx().recursion_limit();
507 if !recursion_limit.value_within_limit(self.depth) {
508 let obligation = Obligation::with_depth(
514 self.selcx.infcx().report_overflow_error(&obligation, true);
517 let substs = substs.super_fold_with(self);
518 let generic_ty = self.tcx().type_of(def_id);
519 let concrete_ty = generic_ty.subst(self.tcx(), substs);
521 let folded_ty = self.fold_ty(concrete_ty);
528 ty::Projection(data) if !data.has_escaping_bound_vars() => {
529 // This branch is *mostly* just an optimization: when we don't
530 // have escaping bound vars, we don't need to replace them with
531 // placeholders (see branch below). *Also*, we know that we can
532 // register an obligation to *later* project, since we know
533 // there won't be bound vars there.
535 let data = data.super_fold_with(self);
536 let normalized_ty = if self.eager_inference_replacement {
537 normalize_projection_type(
543 &mut self.obligations,
546 opt_normalize_projection_type(
552 &mut self.obligations,
556 .unwrap_or_else(|| ty::Term::Ty(ty.super_fold_with(self)))
562 obligations.len = ?self.obligations.len(),
563 "AssocTypeNormalizer: normalized type"
565 normalized_ty.ty().unwrap()
568 ty::Projection(data) => {
569 // If there are escaping bound vars, we temporarily replace the
570 // bound vars with placeholders. Note though, that in the case
571 // that we still can't project for whatever reason (e.g. self
572 // type isn't known enough), we *can't* register an obligation
573 // and return an inference variable (since then that obligation
574 // would have bound vars and that's a can of worms). Instead,
575 // we just give up and fall back to pretending like we never tried!
577 // Note: this isn't necessarily the final approach here; we may
578 // want to figure out how to register obligations with escaping vars
579 // or handle this some other way.
581 let infcx = self.selcx.infcx();
582 let (data, mapped_regions, mapped_types, mapped_consts) =
583 BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, data);
584 let data = data.super_fold_with(self);
585 let normalized_ty = opt_normalize_projection_type(
591 &mut self.obligations,
595 .map(|term| term.ty().unwrap())
596 .map(|normalized_ty| {
597 PlaceholderReplacer::replace_placeholders(
606 .unwrap_or_else(|| ty.super_fold_with(self));
612 obligations.len = ?self.obligations.len(),
613 "AssocTypeNormalizer: normalized type"
618 _ => ty.super_fold_with(self),
622 fn fold_const(&mut self, constant: ty::Const<'tcx>) -> ty::Const<'tcx> {
623 if self.selcx.tcx().lazy_normalization() || !self.eager_inference_replacement {
626 let constant = constant.super_fold_with(self);
627 constant.eval(self.selcx.tcx(), self.param_env)
632 pub struct BoundVarReplacer<'me, 'tcx> {
633 infcx: &'me InferCtxt<'me, 'tcx>,
634 // These three maps track the bound variable that were replaced by placeholders. It might be
635 // nice to remove these since we already have the `kind` in the placeholder; we really just need
636 // the `var` (but we *could* bring that into scope if we were to track them as we pass them).
637 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
638 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
639 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
640 // The current depth relative to *this* folding, *not* the entire normalization. In other words,
641 // the depth of binders we've passed here.
642 current_index: ty::DebruijnIndex,
643 // The `UniverseIndex` of the binding levels above us. These are optional, since we are lazy:
644 // we don't actually create a universe until we see a bound var we have to replace.
645 universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
648 impl<'me, 'tcx> BoundVarReplacer<'me, 'tcx> {
649 /// Returns `Some` if we *were* able to replace bound vars. If there are any bound vars that
650 /// use a binding level above `universe_indices.len()`, we fail.
651 pub fn replace_bound_vars<T: TypeFoldable<'tcx>>(
652 infcx: &'me InferCtxt<'me, 'tcx>,
653 universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
657 BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
658 BTreeMap<ty::PlaceholderType, ty::BoundTy>,
659 BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
661 let mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion> = BTreeMap::new();
662 let mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy> = BTreeMap::new();
663 let mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar> = BTreeMap::new();
665 let mut replacer = BoundVarReplacer {
670 current_index: ty::INNERMOST,
674 let value = value.super_fold_with(&mut replacer);
676 (value, replacer.mapped_regions, replacer.mapped_types, replacer.mapped_consts)
679 fn universe_for(&mut self, debruijn: ty::DebruijnIndex) -> ty::UniverseIndex {
680 let infcx = self.infcx;
682 self.universe_indices.len() + self.current_index.as_usize() - debruijn.as_usize() - 1;
683 let universe = self.universe_indices[index].unwrap_or_else(|| {
684 for i in self.universe_indices.iter_mut().take(index + 1) {
685 *i = i.or_else(|| Some(infcx.create_next_universe()))
687 self.universe_indices[index].unwrap()
693 impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
694 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
698 fn fold_binder<T: TypeFoldable<'tcx>>(
700 t: ty::Binder<'tcx, T>,
701 ) -> ty::Binder<'tcx, T> {
702 self.current_index.shift_in(1);
703 let t = t.super_fold_with(self);
704 self.current_index.shift_out(1);
708 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
710 ty::ReLateBound(debruijn, _)
711 if debruijn.as_usize() + 1
712 > self.current_index.as_usize() + self.universe_indices.len() =>
714 bug!("Bound vars outside of `self.universe_indices`");
716 ty::ReLateBound(debruijn, br) if debruijn >= self.current_index => {
717 let universe = self.universe_for(debruijn);
718 let p = ty::PlaceholderRegion { universe, name: br.kind };
719 self.mapped_regions.insert(p, br);
720 self.infcx.tcx.mk_region(ty::RePlaceholder(p))
726 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
728 ty::Bound(debruijn, _)
729 if debruijn.as_usize() + 1
730 > self.current_index.as_usize() + self.universe_indices.len() =>
732 bug!("Bound vars outside of `self.universe_indices`");
734 ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
735 let universe = self.universe_for(debruijn);
736 let p = ty::PlaceholderType { universe, name: bound_ty.var };
737 self.mapped_types.insert(p, bound_ty);
738 self.infcx.tcx.mk_ty(ty::Placeholder(p))
740 _ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
745 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
747 ty::ConstKind::Bound(debruijn, _)
748 if debruijn.as_usize() + 1
749 > self.current_index.as_usize() + self.universe_indices.len() =>
751 bug!("Bound vars outside of `self.universe_indices`");
753 ty::ConstKind::Bound(debruijn, bound_const) if debruijn >= self.current_index => {
754 let universe = self.universe_for(debruijn);
755 let p = ty::PlaceholderConst {
757 name: ty::BoundConst { var: bound_const, ty: ct.ty() },
759 self.mapped_consts.insert(p, bound_const);
762 .mk_const(ty::ConstS { val: ty::ConstKind::Placeholder(p), ty: ct.ty() })
764 _ if ct.has_vars_bound_at_or_above(self.current_index) => ct.super_fold_with(self),
770 // The inverse of `BoundVarReplacer`: replaces placeholders with the bound vars from which they came.
771 pub struct PlaceholderReplacer<'me, 'tcx> {
772 infcx: &'me InferCtxt<'me, 'tcx>,
773 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
774 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
775 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
776 universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
777 current_index: ty::DebruijnIndex,
780 impl<'me, 'tcx> PlaceholderReplacer<'me, 'tcx> {
781 pub fn replace_placeholders<T: TypeFoldable<'tcx>>(
782 infcx: &'me InferCtxt<'me, 'tcx>,
783 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
784 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
785 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
786 universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
789 let mut replacer = PlaceholderReplacer {
795 current_index: ty::INNERMOST,
797 value.super_fold_with(&mut replacer)
801 impl<'tcx> TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> {
802 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
806 fn fold_binder<T: TypeFoldable<'tcx>>(
808 t: ty::Binder<'tcx, T>,
809 ) -> ty::Binder<'tcx, T> {
810 if !t.has_placeholders() && !t.has_infer_regions() {
813 self.current_index.shift_in(1);
814 let t = t.super_fold_with(self);
815 self.current_index.shift_out(1);
819 fn fold_region(&mut self, r0: ty::Region<'tcx>) -> ty::Region<'tcx> {
825 .unwrap_region_constraints()
826 .opportunistic_resolve_region(self.infcx.tcx, r0),
831 ty::RePlaceholder(p) => {
832 let replace_var = self.mapped_regions.get(&p);
834 Some(replace_var) => {
838 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
839 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
840 let db = ty::DebruijnIndex::from_usize(
841 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
843 self.tcx().mk_region(ty::ReLateBound(db, *replace_var))
851 debug!(?r0, ?r1, ?r2, "fold_region");
856 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
858 ty::Placeholder(p) => {
859 let replace_var = self.mapped_types.get(&p);
861 Some(replace_var) => {
865 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
866 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
867 let db = ty::DebruijnIndex::from_usize(
868 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
870 self.tcx().mk_ty(ty::Bound(db, *replace_var))
876 _ if ty.has_placeholders() || ty.has_infer_regions() => ty.super_fold_with(self),
881 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
882 if let ty::ConstKind::Placeholder(p) = ct.val() {
883 let replace_var = self.mapped_consts.get(&p);
885 Some(replace_var) => {
889 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
890 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
891 let db = ty::DebruijnIndex::from_usize(
892 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
894 self.tcx().mk_const(ty::ConstS {
895 val: ty::ConstKind::Bound(db, *replace_var),
902 ct.super_fold_with(self)
907 /// The guts of `normalize`: normalize a specific projection like `<T
908 /// as Trait>::Item`. The result is always a type (and possibly
909 /// additional obligations). If ambiguity arises, which implies that
910 /// there are unresolved type variables in the projection, we will
911 /// substitute a fresh type variable `$X` and generate a new
912 /// obligation `<T as Trait>::Item == $X` for later.
913 pub fn normalize_projection_type<'a, 'b, 'tcx>(
914 selcx: &'a mut SelectionContext<'b, 'tcx>,
915 param_env: ty::ParamEnv<'tcx>,
916 projection_ty: ty::ProjectionTy<'tcx>,
917 cause: ObligationCause<'tcx>,
919 obligations: &mut Vec<PredicateObligation<'tcx>>,
921 opt_normalize_projection_type(
931 .unwrap_or_else(move || {
932 // if we bottom out in ambiguity, create a type variable
933 // and a deferred predicate to resolve this when more type
934 // information is available.
938 .infer_projection(param_env, projection_ty, cause, depth + 1, obligations)
943 /// The guts of `normalize`: normalize a specific projection like `<T
944 /// as Trait>::Item`. The result is always a type (and possibly
945 /// additional obligations). Returns `None` in the case of ambiguity,
946 /// which indicates that there are unbound type variables.
948 /// This function used to return `Option<NormalizedTy<'tcx>>`, which contains a
949 /// `Ty<'tcx>` and an obligations vector. But that obligation vector was very
950 /// often immediately appended to another obligations vector. So now this
951 /// function takes an obligations vector and appends to it directly, which is
952 /// slightly uglier but avoids the need for an extra short-lived allocation.
953 #[instrument(level = "debug", skip(selcx, param_env, cause, obligations))]
954 fn opt_normalize_projection_type<'a, 'b, 'tcx>(
955 selcx: &'a mut SelectionContext<'b, 'tcx>,
956 param_env: ty::ParamEnv<'tcx>,
957 projection_ty: ty::ProjectionTy<'tcx>,
958 cause: ObligationCause<'tcx>,
960 obligations: &mut Vec<PredicateObligation<'tcx>>,
961 ) -> Result<Option<Term<'tcx>>, InProgress> {
962 let infcx = selcx.infcx();
963 // Don't use the projection cache in intercrate mode -
964 // the `infcx` may be re-used between intercrate in non-intercrate
965 // mode, which could lead to using incorrect cache results.
966 let use_cache = !selcx.is_intercrate();
968 let projection_ty = infcx.resolve_vars_if_possible(projection_ty);
969 let cache_key = ProjectionCacheKey::new(projection_ty);
971 // FIXME(#20304) For now, I am caching here, which is good, but it
972 // means we don't capture the type variables that are created in
973 // the case of ambiguity. Which means we may create a large stream
974 // of such variables. OTOH, if we move the caching up a level, we
975 // would not benefit from caching when proving `T: Trait<U=Foo>`
976 // bounds. It might be the case that we want two distinct caches,
977 // or else another kind of cache entry.
979 let cache_result = if use_cache {
980 infcx.inner.borrow_mut().projection_cache().try_start(cache_key)
985 Ok(()) => debug!("no cache"),
986 Err(ProjectionCacheEntry::Ambiguous) => {
987 // If we found ambiguity the last time, that means we will continue
988 // to do so until some type in the key changes (and we know it
989 // hasn't, because we just fully resolved it).
990 debug!("found cache entry: ambiguous");
993 Err(ProjectionCacheEntry::InProgress) => {
994 // Under lazy normalization, this can arise when
995 // bootstrapping. That is, imagine an environment with a
996 // where-clause like `A::B == u32`. Now, if we are asked
997 // to normalize `A::B`, we will want to check the
998 // where-clauses in scope. So we will try to unify `A::B`
999 // with `A::B`, which can trigger a recursive
1002 debug!("found cache entry: in-progress");
1004 // Cache that normalizing this projection resulted in a cycle. This
1005 // should ensure that, unless this happens within a snapshot that's
1006 // rolled back, fulfillment or evaluation will notice the cycle.
1009 infcx.inner.borrow_mut().projection_cache().recur(cache_key);
1011 return Err(InProgress);
1013 Err(ProjectionCacheEntry::Recur) => {
1014 debug!("recur cache");
1015 return Err(InProgress);
1017 Err(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => {
1018 // This is the hottest path in this function.
1020 // If we find the value in the cache, then return it along
1021 // with the obligations that went along with it. Note
1022 // that, when using a fulfillment context, these
1023 // obligations could in principle be ignored: they have
1024 // already been registered when the cache entry was
1025 // created (and hence the new ones will quickly be
1026 // discarded as duplicated). But when doing trait
1027 // evaluation this is not the case, and dropping the trait
1028 // evaluations can causes ICEs (e.g., #43132).
1029 debug!(?ty, "found normalized ty");
1030 obligations.extend(ty.obligations);
1031 return Ok(Some(ty.value));
1033 Err(ProjectionCacheEntry::Error) => {
1034 debug!("opt_normalize_projection_type: found error");
1035 let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
1036 obligations.extend(result.obligations);
1037 return Ok(Some(result.value.into()));
1041 let obligation = Obligation::with_depth(cause.clone(), depth, param_env, projection_ty);
1043 match project(selcx, &obligation) {
1044 Ok(Projected::Progress(Progress {
1045 term: projected_term,
1046 obligations: mut projected_obligations,
1048 // if projection succeeded, then what we get out of this
1049 // is also non-normalized (consider: it was derived from
1050 // an impl, where-clause etc) and hence we must
1053 let projected_term = selcx.infcx().resolve_vars_if_possible(projected_term);
1055 let mut result = if projected_term.has_projections() {
1056 let mut normalizer = AssocTypeNormalizer::new(
1061 &mut projected_obligations,
1063 let normalized_ty = normalizer.fold(projected_term);
1065 Normalized { value: normalized_ty, obligations: projected_obligations }
1067 Normalized { value: projected_term, obligations: projected_obligations }
1070 let mut deduped: SsoHashSet<_> = Default::default();
1071 result.obligations.drain_filter(|projected_obligation| {
1072 if !deduped.insert(projected_obligation.clone()) {
1079 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
1081 obligations.extend(result.obligations);
1082 Ok(Some(result.value))
1084 Ok(Projected::NoProgress(projected_ty)) => {
1085 let result = Normalized { value: projected_ty, obligations: vec![] };
1087 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
1089 // No need to extend `obligations`.
1090 Ok(Some(result.value))
1092 Err(ProjectionError::TooManyCandidates) => {
1093 debug!("opt_normalize_projection_type: too many candidates");
1095 infcx.inner.borrow_mut().projection_cache().ambiguous(cache_key);
1099 Err(ProjectionError::TraitSelectionError(_)) => {
1100 debug!("opt_normalize_projection_type: ERROR");
1101 // if we got an error processing the `T as Trait` part,
1102 // just return `ty::err` but add the obligation `T :
1103 // Trait`, which when processed will cause the error to be
1107 infcx.inner.borrow_mut().projection_cache().error(cache_key);
1109 let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
1110 obligations.extend(result.obligations);
1111 Ok(Some(result.value.into()))
1116 /// If we are projecting `<T as Trait>::Item`, but `T: Trait` does not
1117 /// hold. In various error cases, we cannot generate a valid
1118 /// normalized projection. Therefore, we create an inference variable
1119 /// return an associated obligation that, when fulfilled, will lead to
1122 /// Note that we used to return `Error` here, but that was quite
1123 /// dubious -- the premise was that an error would *eventually* be
1124 /// reported, when the obligation was processed. But in general once
1125 /// you see an `Error` you are supposed to be able to assume that an
1126 /// error *has been* reported, so that you can take whatever heuristic
1127 /// paths you want to take. To make things worse, it was possible for
1128 /// cycles to arise, where you basically had a setup like `<MyType<$0>
1129 /// as Trait>::Foo == $0`. Here, normalizing `<MyType<$0> as
1130 /// Trait>::Foo> to `[type error]` would lead to an obligation of
1131 /// `<MyType<[type error]> as Trait>::Foo`. We are supposed to report
1132 /// an error for this obligation, but we legitimately should not,
1133 /// because it contains `[type error]`. Yuck! (See issue #29857 for
1134 /// one case where this arose.)
1135 fn normalize_to_error<'a, 'tcx>(
1136 selcx: &mut SelectionContext<'a, 'tcx>,
1137 param_env: ty::ParamEnv<'tcx>,
1138 projection_ty: ty::ProjectionTy<'tcx>,
1139 cause: ObligationCause<'tcx>,
1141 ) -> NormalizedTy<'tcx> {
1142 let trait_ref = ty::Binder::dummy(projection_ty.trait_ref(selcx.tcx()));
1143 let trait_obligation = Obligation {
1145 recursion_depth: depth,
1147 predicate: trait_ref.without_const().to_predicate(selcx.tcx()),
1149 let tcx = selcx.infcx().tcx;
1150 let def_id = projection_ty.item_def_id;
1151 let new_value = selcx.infcx().next_ty_var(TypeVariableOrigin {
1152 kind: TypeVariableOriginKind::NormalizeProjectionType,
1153 span: tcx.def_span(def_id),
1155 Normalized { value: new_value, obligations: vec![trait_obligation] }
1158 enum Projected<'tcx> {
1159 Progress(Progress<'tcx>),
1160 NoProgress(ty::Term<'tcx>),
1163 struct Progress<'tcx> {
1164 term: ty::Term<'tcx>,
1165 obligations: Vec<PredicateObligation<'tcx>>,
1168 impl<'tcx> Progress<'tcx> {
1169 fn error(tcx: TyCtxt<'tcx>) -> Self {
1170 Progress { term: tcx.ty_error().into(), obligations: vec![] }
1173 fn with_addl_obligations(mut self, mut obligations: Vec<PredicateObligation<'tcx>>) -> Self {
1174 self.obligations.append(&mut obligations);
1179 /// Computes the result of a projection type (if we can).
1182 /// - `obligation` must be fully normalized
1183 #[tracing::instrument(level = "info", skip(selcx))]
1184 fn project<'cx, 'tcx>(
1185 selcx: &mut SelectionContext<'cx, 'tcx>,
1186 obligation: &ProjectionTyObligation<'tcx>,
1187 ) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1188 if !selcx.tcx().recursion_limit().value_within_limit(obligation.recursion_depth) {
1189 // This should really be an immediate error, but some existing code
1190 // relies on being able to recover from this.
1191 return Err(ProjectionError::TraitSelectionError(SelectionError::Overflow(
1192 OverflowError::Canonical,
1196 if obligation.predicate.references_error() {
1197 return Ok(Projected::Progress(Progress::error(selcx.tcx())));
1200 let mut candidates = ProjectionCandidateSet::None;
1202 // Make sure that the following procedures are kept in order. ParamEnv
1203 // needs to be first because it has highest priority, and Select checks
1204 // the return value of push_candidate which assumes it's ran at last.
1205 assemble_candidates_from_param_env(selcx, obligation, &mut candidates);
1207 assemble_candidates_from_trait_def(selcx, obligation, &mut candidates);
1209 assemble_candidates_from_object_ty(selcx, obligation, &mut candidates);
1211 if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(_)) = candidates {
1212 // Avoid normalization cycle from selection (see
1213 // `assemble_candidates_from_object_ty`).
1214 // FIXME(lazy_normalization): Lazy normalization should save us from
1215 // having to special case this.
1217 assemble_candidates_from_impls(selcx, obligation, &mut candidates);
1221 ProjectionCandidateSet::Single(candidate) => {
1222 Ok(Projected::Progress(confirm_candidate(selcx, obligation, candidate)))
1224 ProjectionCandidateSet::None => Ok(Projected::NoProgress(
1225 // FIXME(associated_const_generics): this may need to change in the future?
1226 // need to investigate whether or not this is fine.
1229 .mk_projection(obligation.predicate.item_def_id, obligation.predicate.substs)
1232 // Error occurred while trying to processing impls.
1233 ProjectionCandidateSet::Error(e) => Err(ProjectionError::TraitSelectionError(e)),
1234 // Inherent ambiguity that prevents us from even enumerating the
1236 ProjectionCandidateSet::Ambiguous => Err(ProjectionError::TooManyCandidates),
1240 /// The first thing we have to do is scan through the parameter
1241 /// environment to see whether there are any projection predicates
1242 /// there that can answer this question.
1243 fn assemble_candidates_from_param_env<'cx, 'tcx>(
1244 selcx: &mut SelectionContext<'cx, 'tcx>,
1245 obligation: &ProjectionTyObligation<'tcx>,
1246 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1248 assemble_candidates_from_predicates(
1252 ProjectionCandidate::ParamEnv,
1253 obligation.param_env.caller_bounds().iter(),
1258 /// In the case of a nested projection like <<A as Foo>::FooT as Bar>::BarT, we may find
1259 /// that the definition of `Foo` has some clues:
1263 /// type FooT : Bar<BarT=i32>
1267 /// Here, for example, we could conclude that the result is `i32`.
1268 fn assemble_candidates_from_trait_def<'cx, 'tcx>(
1269 selcx: &mut SelectionContext<'cx, 'tcx>,
1270 obligation: &ProjectionTyObligation<'tcx>,
1271 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1273 debug!("assemble_candidates_from_trait_def(..)");
1275 let tcx = selcx.tcx();
1276 // Check whether the self-type is itself a projection.
1277 // If so, extract what we know from the trait and try to come up with a good answer.
1278 let bounds = match *obligation.predicate.self_ty().kind() {
1279 ty::Projection(ref data) => tcx.item_bounds(data.item_def_id).subst(tcx, data.substs),
1280 ty::Opaque(def_id, substs) => tcx.item_bounds(def_id).subst(tcx, substs),
1281 ty::Infer(ty::TyVar(_)) => {
1282 // If the self-type is an inference variable, then it MAY wind up
1283 // being a projected type, so induce an ambiguity.
1284 candidate_set.mark_ambiguous();
1290 assemble_candidates_from_predicates(
1294 ProjectionCandidate::TraitDef,
1300 /// In the case of a trait object like
1301 /// `<dyn Iterator<Item = ()> as Iterator>::Item` we can use the existential
1302 /// predicate in the trait object.
1304 /// We don't go through the select candidate for these bounds to avoid cycles:
1305 /// In the above case, `dyn Iterator<Item = ()>: Iterator` would create a
1306 /// nested obligation of `<dyn Iterator<Item = ()> as Iterator>::Item: Sized`,
1307 /// this then has to be normalized without having to prove
1308 /// `dyn Iterator<Item = ()>: Iterator` again.
1309 fn assemble_candidates_from_object_ty<'cx, 'tcx>(
1310 selcx: &mut SelectionContext<'cx, 'tcx>,
1311 obligation: &ProjectionTyObligation<'tcx>,
1312 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1314 debug!("assemble_candidates_from_object_ty(..)");
1316 let tcx = selcx.tcx();
1318 let self_ty = obligation.predicate.self_ty();
1319 let object_ty = selcx.infcx().shallow_resolve(self_ty);
1320 let data = match object_ty.kind() {
1321 ty::Dynamic(data, ..) => data,
1322 ty::Infer(ty::TyVar(_)) => {
1323 // If the self-type is an inference variable, then it MAY wind up
1324 // being an object type, so induce an ambiguity.
1325 candidate_set.mark_ambiguous();
1330 let env_predicates = data
1331 .projection_bounds()
1332 .filter(|bound| bound.item_def_id() == obligation.predicate.item_def_id)
1333 .map(|p| p.with_self_ty(tcx, object_ty).to_predicate(tcx));
1335 assemble_candidates_from_predicates(
1339 ProjectionCandidate::Object,
1345 #[tracing::instrument(
1347 skip(selcx, candidate_set, ctor, env_predicates, potentially_unnormalized_candidates)
1349 fn assemble_candidates_from_predicates<'cx, 'tcx>(
1350 selcx: &mut SelectionContext<'cx, 'tcx>,
1351 obligation: &ProjectionTyObligation<'tcx>,
1352 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1353 ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionCandidate<'tcx>,
1354 env_predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
1355 potentially_unnormalized_candidates: bool,
1357 let infcx = selcx.infcx();
1358 for predicate in env_predicates {
1359 let bound_predicate = predicate.kind();
1360 if let ty::PredicateKind::Projection(data) = predicate.kind().skip_binder() {
1361 let data = bound_predicate.rebind(data);
1362 if data.projection_def_id() != obligation.predicate.item_def_id {
1366 let is_match = infcx.probe(|_| {
1367 selcx.match_projection_projections(
1370 potentially_unnormalized_candidates,
1375 ProjectionMatchesProjection::Yes => {
1376 candidate_set.push_candidate(ctor(data));
1378 if potentially_unnormalized_candidates
1379 && !obligation.predicate.has_infer_types_or_consts()
1381 // HACK: Pick the first trait def candidate for a fully
1382 // inferred predicate. This is to allow duplicates that
1383 // differ only in normalization.
1387 ProjectionMatchesProjection::Ambiguous => {
1388 candidate_set.mark_ambiguous();
1390 ProjectionMatchesProjection::No => {}
1396 #[tracing::instrument(level = "debug", skip(selcx, obligation, candidate_set))]
1397 fn assemble_candidates_from_impls<'cx, 'tcx>(
1398 selcx: &mut SelectionContext<'cx, 'tcx>,
1399 obligation: &ProjectionTyObligation<'tcx>,
1400 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1402 // If we are resolving `<T as TraitRef<...>>::Item == Type`,
1403 // start out by selecting the predicate `T as TraitRef<...>`:
1404 let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx()));
1405 let trait_obligation = obligation.with(poly_trait_ref.to_poly_trait_predicate());
1406 let _ = selcx.infcx().commit_if_ok(|_| {
1407 let impl_source = match selcx.select(&trait_obligation) {
1408 Ok(Some(impl_source)) => impl_source,
1410 candidate_set.mark_ambiguous();
1414 debug!(error = ?e, "selection error");
1415 candidate_set.mark_error(e);
1420 let eligible = match &impl_source {
1421 super::ImplSource::Closure(_)
1422 | super::ImplSource::Generator(_)
1423 | super::ImplSource::FnPointer(_)
1424 | super::ImplSource::TraitAlias(_) => true,
1425 super::ImplSource::UserDefined(impl_data) => {
1426 // We have to be careful when projecting out of an
1427 // impl because of specialization. If we are not in
1428 // codegen (i.e., projection mode is not "any"), and the
1429 // impl's type is declared as default, then we disable
1430 // projection (even if the trait ref is fully
1431 // monomorphic). In the case where trait ref is not
1432 // fully monomorphic (i.e., includes type parameters),
1433 // this is because those type parameters may
1434 // ultimately be bound to types from other crates that
1435 // may have specialized impls we can't see. In the
1436 // case where the trait ref IS fully monomorphic, this
1437 // is a policy decision that we made in the RFC in
1438 // order to preserve flexibility for the crate that
1439 // defined the specializable impl to specialize later
1440 // for existing types.
1442 // In either case, we handle this by not adding a
1443 // candidate for an impl if it contains a `default`
1446 // NOTE: This should be kept in sync with the similar code in
1447 // `rustc_ty_utils::instance::resolve_associated_item()`.
1449 assoc_def(selcx, impl_data.impl_def_id, obligation.predicate.item_def_id)
1450 .map_err(|ErrorGuaranteed { .. }| ())?;
1452 if node_item.is_final() {
1453 // Non-specializable items are always projectable.
1456 // Only reveal a specializable default if we're past type-checking
1457 // and the obligation is monomorphic, otherwise passes such as
1458 // transmute checking and polymorphic MIR optimizations could
1459 // get a result which isn't correct for all monomorphizations.
1460 if obligation.param_env.reveal() == Reveal::All {
1461 // NOTE(eddyb) inference variables can resolve to parameters, so
1462 // assume `poly_trait_ref` isn't monomorphic, if it contains any.
1463 let poly_trait_ref = selcx.infcx().resolve_vars_if_possible(poly_trait_ref);
1464 !poly_trait_ref.still_further_specializable()
1467 assoc_ty = ?selcx.tcx().def_path_str(node_item.item.def_id),
1468 ?obligation.predicate,
1469 "assemble_candidates_from_impls: not eligible due to default",
1475 super::ImplSource::DiscriminantKind(..) => {
1476 // While `DiscriminantKind` is automatically implemented for every type,
1477 // the concrete discriminant may not be known yet.
1479 // Any type with multiple potential discriminant types is therefore not eligible.
1480 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1482 match self_ty.kind() {
1500 | ty::GeneratorWitness(..)
1503 // Integers and floats always have `u8` as their discriminant.
1504 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1510 | ty::Placeholder(..)
1512 | ty::Error(_) => false,
1515 super::ImplSource::Pointee(..) => {
1516 // While `Pointee` is automatically implemented for every type,
1517 // the concrete metadata type may not be known yet.
1519 // Any type with multiple potential metadata types is therefore not eligible.
1520 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1522 let tail = selcx.tcx().struct_tail_with_normalize(self_ty, |ty| {
1523 // We throw away any obligations we get from this, since we normalize
1524 // and confirm these obligations once again during confirmation
1525 normalize_with_depth(
1527 obligation.param_env,
1528 obligation.cause.clone(),
1529 obligation.recursion_depth + 1,
1551 | ty::GeneratorWitness(..)
1553 // Extern types have unit metadata, according to RFC 2850
1555 // If returned by `struct_tail_without_normalization` this is a unit struct
1556 // without any fields, or not a struct, and therefore is Sized.
1558 // If returned by `struct_tail_without_normalization` this is the empty tuple.
1560 // Integers and floats are always Sized, and so have unit type metadata.
1561 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1563 // type parameters, opaques, and unnormalized projections have pointer
1564 // metadata if they're known (e.g. by the param_env) to be sized
1565 ty::Param(_) | ty::Projection(..) | ty::Opaque(..)
1566 if selcx.infcx().predicate_must_hold_modulo_regions(
1568 ty::Binder::dummy(ty::TraitRef::new(
1569 selcx.tcx().require_lang_item(LangItem::Sized, None),
1570 selcx.tcx().mk_substs_trait(self_ty, &[]),
1573 .to_predicate(selcx.tcx()),
1580 // FIXME(compiler-errors): are Bound and Placeholder types ever known sized?
1582 | ty::Projection(..)
1585 | ty::Placeholder(..)
1588 if tail.has_infer_types() {
1589 candidate_set.mark_ambiguous();
1595 super::ImplSource::Param(..) => {
1596 // This case tell us nothing about the value of an
1597 // associated type. Consider:
1600 // trait SomeTrait { type Foo; }
1601 // fn foo<T:SomeTrait>(...) { }
1604 // If the user writes `<T as SomeTrait>::Foo`, then the `T
1605 // : SomeTrait` binding does not help us decide what the
1606 // type `Foo` is (at least, not more specifically than
1607 // what we already knew).
1609 // But wait, you say! What about an example like this:
1612 // fn bar<T:SomeTrait<Foo=usize>>(...) { ... }
1615 // Doesn't the `T : SomeTrait<Foo=usize>` predicate help
1616 // resolve `T::Foo`? And of course it does, but in fact
1617 // that single predicate is desugared into two predicates
1618 // in the compiler: a trait predicate (`T : SomeTrait`) and a
1619 // projection. And the projection where clause is handled
1620 // in `assemble_candidates_from_param_env`.
1623 super::ImplSource::Object(_) => {
1624 // Handled by the `Object` projection candidate. See
1625 // `assemble_candidates_from_object_ty` for an explanation of
1626 // why we special case object types.
1629 super::ImplSource::AutoImpl(..)
1630 | super::ImplSource::Builtin(..)
1631 | super::ImplSource::TraitUpcasting(_)
1632 | super::ImplSource::ConstDestruct(_) => {
1633 // These traits have no associated types.
1634 selcx.tcx().sess.delay_span_bug(
1635 obligation.cause.span,
1636 &format!("Cannot project an associated type from `{:?}`", impl_source),
1643 if candidate_set.push_candidate(ProjectionCandidate::Select(impl_source)) {
1654 fn confirm_candidate<'cx, 'tcx>(
1655 selcx: &mut SelectionContext<'cx, 'tcx>,
1656 obligation: &ProjectionTyObligation<'tcx>,
1657 candidate: ProjectionCandidate<'tcx>,
1658 ) -> Progress<'tcx> {
1659 debug!(?obligation, ?candidate, "confirm_candidate");
1660 let mut progress = match candidate {
1661 ProjectionCandidate::ParamEnv(poly_projection)
1662 | ProjectionCandidate::Object(poly_projection) => {
1663 confirm_param_env_candidate(selcx, obligation, poly_projection, false)
1666 ProjectionCandidate::TraitDef(poly_projection) => {
1667 confirm_param_env_candidate(selcx, obligation, poly_projection, true)
1670 ProjectionCandidate::Select(impl_source) => {
1671 confirm_select_candidate(selcx, obligation, impl_source)
1675 // When checking for cycle during evaluation, we compare predicates with
1676 // "syntactic" equality. Since normalization generally introduces a type
1677 // with new region variables, we need to resolve them to existing variables
1678 // when possible for this to work. See `auto-trait-projection-recursion.rs`
1679 // for a case where this matters.
1680 if progress.term.has_infer_regions() {
1682 progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx()));
1687 fn confirm_select_candidate<'cx, 'tcx>(
1688 selcx: &mut SelectionContext<'cx, 'tcx>,
1689 obligation: &ProjectionTyObligation<'tcx>,
1690 impl_source: Selection<'tcx>,
1691 ) -> Progress<'tcx> {
1693 super::ImplSource::UserDefined(data) => confirm_impl_candidate(selcx, obligation, data),
1694 super::ImplSource::Generator(data) => confirm_generator_candidate(selcx, obligation, data),
1695 super::ImplSource::Closure(data) => confirm_closure_candidate(selcx, obligation, data),
1696 super::ImplSource::FnPointer(data) => confirm_fn_pointer_candidate(selcx, obligation, data),
1697 super::ImplSource::DiscriminantKind(data) => {
1698 confirm_discriminant_kind_candidate(selcx, obligation, data)
1700 super::ImplSource::Pointee(data) => confirm_pointee_candidate(selcx, obligation, data),
1701 super::ImplSource::Object(_)
1702 | super::ImplSource::AutoImpl(..)
1703 | super::ImplSource::Param(..)
1704 | super::ImplSource::Builtin(..)
1705 | super::ImplSource::TraitUpcasting(_)
1706 | super::ImplSource::TraitAlias(..)
1707 | super::ImplSource::ConstDestruct(_) => {
1708 // we don't create Select candidates with this kind of resolution
1710 obligation.cause.span,
1711 "Cannot project an associated type from `{:?}`",
1718 fn confirm_generator_candidate<'cx, 'tcx>(
1719 selcx: &mut SelectionContext<'cx, 'tcx>,
1720 obligation: &ProjectionTyObligation<'tcx>,
1721 impl_source: ImplSourceGeneratorData<'tcx, PredicateObligation<'tcx>>,
1722 ) -> Progress<'tcx> {
1723 let gen_sig = impl_source.substs.as_generator().poly_sig();
1724 let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1726 obligation.param_env,
1727 obligation.cause.clone(),
1728 obligation.recursion_depth + 1,
1732 debug!(?obligation, ?gen_sig, ?obligations, "confirm_generator_candidate");
1734 let tcx = selcx.tcx();
1736 let gen_def_id = tcx.require_lang_item(LangItem::Generator, None);
1738 let predicate = super::util::generator_trait_ref_and_outputs(
1741 obligation.predicate.self_ty(),
1744 .map_bound(|(trait_ref, yield_ty, return_ty)| {
1745 let name = tcx.associated_item(obligation.predicate.item_def_id).name;
1746 let ty = if name == sym::Return {
1748 } else if name == sym::Yield {
1754 ty::ProjectionPredicate {
1755 projection_ty: ty::ProjectionTy {
1756 substs: trait_ref.substs,
1757 item_def_id: obligation.predicate.item_def_id,
1763 confirm_param_env_candidate(selcx, obligation, predicate, false)
1764 .with_addl_obligations(impl_source.nested)
1765 .with_addl_obligations(obligations)
1768 fn confirm_discriminant_kind_candidate<'cx, 'tcx>(
1769 selcx: &mut SelectionContext<'cx, 'tcx>,
1770 obligation: &ProjectionTyObligation<'tcx>,
1771 _: ImplSourceDiscriminantKindData,
1772 ) -> Progress<'tcx> {
1773 let tcx = selcx.tcx();
1775 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1776 // We get here from `poly_project_and_unify_type` which replaces bound vars
1777 // with placeholders
1778 debug_assert!(!self_ty.has_escaping_bound_vars());
1779 let substs = tcx.mk_substs([self_ty.into()].iter());
1781 let discriminant_def_id = tcx.require_lang_item(LangItem::Discriminant, None);
1783 let predicate = ty::ProjectionPredicate {
1784 projection_ty: ty::ProjectionTy { substs, item_def_id: discriminant_def_id },
1785 term: self_ty.discriminant_ty(tcx).into(),
1788 // We get here from `poly_project_and_unify_type` which replaces bound vars
1789 // with placeholders, so dummy is okay here.
1790 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1793 fn confirm_pointee_candidate<'cx, 'tcx>(
1794 selcx: &mut SelectionContext<'cx, 'tcx>,
1795 obligation: &ProjectionTyObligation<'tcx>,
1796 _: ImplSourcePointeeData,
1797 ) -> Progress<'tcx> {
1798 let tcx = selcx.tcx();
1799 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1801 let mut obligations = vec![];
1802 let (metadata_ty, check_is_sized) = self_ty.ptr_metadata_ty(tcx, |ty| {
1803 normalize_with_depth_to(
1805 obligation.param_env,
1806 obligation.cause.clone(),
1807 obligation.recursion_depth + 1,
1813 let sized_predicate = ty::Binder::dummy(ty::TraitRef::new(
1814 tcx.require_lang_item(LangItem::Sized, None),
1815 tcx.mk_substs_trait(self_ty, &[]),
1819 obligations.push(Obligation::new(
1820 obligation.cause.clone(),
1821 obligation.param_env,
1826 let substs = tcx.mk_substs([self_ty.into()].iter());
1827 let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
1829 let predicate = ty::ProjectionPredicate {
1830 projection_ty: ty::ProjectionTy { substs, item_def_id: metadata_def_id },
1831 term: metadata_ty.into(),
1834 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1835 .with_addl_obligations(obligations)
1838 fn confirm_fn_pointer_candidate<'cx, 'tcx>(
1839 selcx: &mut SelectionContext<'cx, 'tcx>,
1840 obligation: &ProjectionTyObligation<'tcx>,
1841 fn_pointer_impl_source: ImplSourceFnPointerData<'tcx, PredicateObligation<'tcx>>,
1842 ) -> Progress<'tcx> {
1843 let fn_type = selcx.infcx().shallow_resolve(fn_pointer_impl_source.fn_ty);
1844 let sig = fn_type.fn_sig(selcx.tcx());
1845 let Normalized { value: sig, obligations } = normalize_with_depth(
1847 obligation.param_env,
1848 obligation.cause.clone(),
1849 obligation.recursion_depth + 1,
1853 confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
1854 .with_addl_obligations(fn_pointer_impl_source.nested)
1855 .with_addl_obligations(obligations)
1858 fn confirm_closure_candidate<'cx, 'tcx>(
1859 selcx: &mut SelectionContext<'cx, 'tcx>,
1860 obligation: &ProjectionTyObligation<'tcx>,
1861 impl_source: ImplSourceClosureData<'tcx, PredicateObligation<'tcx>>,
1862 ) -> Progress<'tcx> {
1863 let closure_sig = impl_source.substs.as_closure().sig();
1864 let Normalized { value: closure_sig, obligations } = normalize_with_depth(
1866 obligation.param_env,
1867 obligation.cause.clone(),
1868 obligation.recursion_depth + 1,
1872 debug!(?obligation, ?closure_sig, ?obligations, "confirm_closure_candidate");
1874 confirm_callable_candidate(selcx, obligation, closure_sig, util::TupleArgumentsFlag::No)
1875 .with_addl_obligations(impl_source.nested)
1876 .with_addl_obligations(obligations)
1879 fn confirm_callable_candidate<'cx, 'tcx>(
1880 selcx: &mut SelectionContext<'cx, 'tcx>,
1881 obligation: &ProjectionTyObligation<'tcx>,
1882 fn_sig: ty::PolyFnSig<'tcx>,
1883 flag: util::TupleArgumentsFlag,
1884 ) -> Progress<'tcx> {
1885 let tcx = selcx.tcx();
1887 debug!(?obligation, ?fn_sig, "confirm_callable_candidate");
1889 let fn_once_def_id = tcx.require_lang_item(LangItem::FnOnce, None);
1890 let fn_once_output_def_id = tcx.require_lang_item(LangItem::FnOnceOutput, None);
1892 let predicate = super::util::closure_trait_ref_and_return_type(
1895 obligation.predicate.self_ty(),
1899 .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
1900 projection_ty: ty::ProjectionTy {
1901 substs: trait_ref.substs,
1902 item_def_id: fn_once_output_def_id,
1904 term: ret_type.into(),
1907 confirm_param_env_candidate(selcx, obligation, predicate, true)
1910 fn confirm_param_env_candidate<'cx, 'tcx>(
1911 selcx: &mut SelectionContext<'cx, 'tcx>,
1912 obligation: &ProjectionTyObligation<'tcx>,
1913 poly_cache_entry: ty::PolyProjectionPredicate<'tcx>,
1914 potentially_unnormalized_candidate: bool,
1915 ) -> Progress<'tcx> {
1916 let infcx = selcx.infcx();
1917 let cause = &obligation.cause;
1918 let param_env = obligation.param_env;
1920 let (cache_entry, _) = infcx.replace_bound_vars_with_fresh_vars(
1922 LateBoundRegionConversionTime::HigherRankedType,
1926 let cache_projection = cache_entry.projection_ty;
1927 let mut nested_obligations = Vec::new();
1928 let obligation_projection = obligation.predicate;
1929 let obligation_projection = ensure_sufficient_stack(|| {
1930 normalize_with_depth_to(
1932 obligation.param_env,
1933 obligation.cause.clone(),
1934 obligation.recursion_depth + 1,
1935 obligation_projection,
1936 &mut nested_obligations,
1939 let cache_projection = if potentially_unnormalized_candidate {
1940 ensure_sufficient_stack(|| {
1941 normalize_with_depth_to(
1943 obligation.param_env,
1944 obligation.cause.clone(),
1945 obligation.recursion_depth + 1,
1947 &mut nested_obligations,
1954 debug!(?cache_projection, ?obligation_projection);
1956 match infcx.at(cause, param_env).eq(cache_projection, obligation_projection) {
1957 Ok(InferOk { value: _, obligations }) => {
1958 nested_obligations.extend(obligations);
1959 assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations);
1960 // FIXME(associated_const_equality): Handle consts here as well? Maybe this progress type should just take
1962 Progress { term: cache_entry.term, obligations: nested_obligations }
1966 "Failed to unify obligation `{:?}` with poly_projection `{:?}`: {:?}",
1967 obligation, poly_cache_entry, e,
1969 debug!("confirm_param_env_candidate: {}", msg);
1970 let err = infcx.tcx.ty_error_with_message(obligation.cause.span, &msg);
1971 Progress { term: err.into(), obligations: vec![] }
1976 fn confirm_impl_candidate<'cx, 'tcx>(
1977 selcx: &mut SelectionContext<'cx, 'tcx>,
1978 obligation: &ProjectionTyObligation<'tcx>,
1979 impl_impl_source: ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>,
1980 ) -> Progress<'tcx> {
1981 let tcx = selcx.tcx();
1983 let ImplSourceUserDefinedData { impl_def_id, substs, mut nested } = impl_impl_source;
1984 let assoc_item_id = obligation.predicate.item_def_id;
1985 let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
1987 let param_env = obligation.param_env;
1988 let Ok(assoc_ty) = assoc_def(selcx, impl_def_id, assoc_item_id) else {
1989 return Progress { term: tcx.ty_error().into(), obligations: nested };
1992 if !assoc_ty.item.defaultness.has_value() {
1993 // This means that the impl is missing a definition for the
1994 // associated type. This error will be reported by the type
1995 // checker method `check_impl_items_against_trait`, so here we
1996 // just return Error.
1998 "confirm_impl_candidate: no associated type {:?} for {:?}",
1999 assoc_ty.item.name, obligation.predicate
2001 return Progress { term: tcx.ty_error().into(), obligations: nested };
2003 // If we're trying to normalize `<Vec<u32> as X>::A<S>` using
2004 //`impl<T> X for Vec<T> { type A<Y> = Box<Y>; }`, then:
2006 // * `obligation.predicate.substs` is `[Vec<u32>, S]`
2007 // * `substs` is `[u32]`
2008 // * `substs` ends up as `[u32, S]`
2009 let substs = obligation.predicate.substs.rebase_onto(tcx, trait_def_id, substs);
2011 translate_substs(selcx.infcx(), param_env, impl_def_id, substs, assoc_ty.defining_node);
2012 let ty = tcx.type_of(assoc_ty.item.def_id);
2013 let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst);
2014 let term: ty::Term<'tcx> = if is_const {
2015 let identity_substs =
2016 crate::traits::InternalSubsts::identity_for_item(tcx, assoc_ty.item.def_id);
2017 let did = ty::WithOptConstParam::unknown(assoc_ty.item.def_id);
2018 let val = ty::ConstKind::Unevaluated(ty::Unevaluated::new(did, identity_substs));
2019 tcx.mk_const(ty::ConstS { ty, val }).into()
2023 if substs.len() != tcx.generics_of(assoc_ty.item.def_id).count() {
2024 let err = tcx.ty_error_with_message(
2025 obligation.cause.span,
2026 "impl item and trait item have different parameter counts",
2028 Progress { term: err.into(), obligations: nested }
2030 assoc_ty_own_obligations(selcx, obligation, &mut nested);
2031 Progress { term: term.subst(tcx, substs), obligations: nested }
2035 // Get obligations corresponding to the predicates from the where-clause of the
2036 // associated type itself.
2037 // Note: `feature(generic_associated_types)` is required to write such
2038 // predicates, even for non-generic associated types.
2039 fn assoc_ty_own_obligations<'cx, 'tcx>(
2040 selcx: &mut SelectionContext<'cx, 'tcx>,
2041 obligation: &ProjectionTyObligation<'tcx>,
2042 nested: &mut Vec<PredicateObligation<'tcx>>,
2044 let tcx = selcx.tcx();
2045 for predicate in tcx
2046 .predicates_of(obligation.predicate.item_def_id)
2047 .instantiate_own(tcx, obligation.predicate.substs)
2050 let normalized = normalize_with_depth_to(
2052 obligation.param_env,
2053 obligation.cause.clone(),
2054 obligation.recursion_depth + 1,
2058 nested.push(Obligation::with_depth(
2059 obligation.cause.clone(),
2060 obligation.recursion_depth + 1,
2061 obligation.param_env,
2067 /// Locate the definition of an associated type in the specialization hierarchy,
2068 /// starting from the given impl.
2070 /// Based on the "projection mode", this lookup may in fact only examine the
2071 /// topmost impl. See the comments for `Reveal` for more details.
2073 selcx: &SelectionContext<'_, '_>,
2075 assoc_def_id: DefId,
2076 ) -> Result<specialization_graph::LeafDef, ErrorGuaranteed> {
2077 let tcx = selcx.tcx();
2078 let trait_def_id = tcx.impl_trait_ref(impl_def_id).unwrap().def_id;
2079 let trait_def = tcx.trait_def(trait_def_id);
2081 // This function may be called while we are still building the
2082 // specialization graph that is queried below (via TraitDef::ancestors()),
2083 // so, in order to avoid unnecessary infinite recursion, we manually look
2084 // for the associated item at the given impl.
2085 // If there is no such item in that impl, this function will fail with a
2086 // cycle error if the specialization graph is currently being built.
2087 if let Some(&impl_item_id) = tcx.impl_item_implementor_ids(impl_def_id).get(&assoc_def_id) {
2088 let item = tcx.associated_item(impl_item_id);
2089 let impl_node = specialization_graph::Node::Impl(impl_def_id);
2090 return Ok(specialization_graph::LeafDef {
2092 defining_node: impl_node,
2093 finalizing_node: if item.defaultness.is_default() { None } else { Some(impl_node) },
2097 let ancestors = trait_def.ancestors(tcx, impl_def_id)?;
2098 if let Some(assoc_item) = ancestors.leaf_def(tcx, assoc_def_id) {
2101 // This is saying that neither the trait nor
2102 // the impl contain a definition for this
2103 // associated type. Normally this situation
2104 // could only arise through a compiler bug --
2105 // if the user wrote a bad item name, it
2106 // should have failed in astconv.
2108 "No associated type `{}` for {}",
2109 tcx.item_name(assoc_def_id),
2110 tcx.def_path_str(impl_def_id)
2115 crate trait ProjectionCacheKeyExt<'cx, 'tcx>: Sized {
2116 fn from_poly_projection_predicate(
2117 selcx: &mut SelectionContext<'cx, 'tcx>,
2118 predicate: ty::PolyProjectionPredicate<'tcx>,
2122 impl<'cx, 'tcx> ProjectionCacheKeyExt<'cx, 'tcx> for ProjectionCacheKey<'tcx> {
2123 fn from_poly_projection_predicate(
2124 selcx: &mut SelectionContext<'cx, 'tcx>,
2125 predicate: ty::PolyProjectionPredicate<'tcx>,
2127 let infcx = selcx.infcx();
2128 // We don't do cross-snapshot caching of obligations with escaping regions,
2129 // so there's no cache key to use
2130 predicate.no_bound_vars().map(|predicate| {
2131 ProjectionCacheKey::new(
2132 // We don't attempt to match up with a specific type-variable state
2133 // from a specific call to `opt_normalize_projection_type` - if
2134 // there's no precise match, the original cache entry is "stranded"
2136 infcx.resolve_vars_if_possible(predicate.projection_ty),