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, ImplSourceFnPointerData, ImplSourceFutureData, ImplSourceGeneratorData,
15 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::TypeErrCtxtExt 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::lang_items::LangItem;
29 use rustc_infer::infer::at::At;
30 use rustc_infer::infer::resolve::OpportunisticRegionResolver;
31 use rustc_infer::traits::ImplSourceBuiltinData;
32 use rustc_middle::traits::select::OverflowError;
33 use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
34 use rustc_middle::ty::visit::{MaxUniverse, TypeVisitable};
35 use rustc_middle::ty::DefIdTree;
36 use rustc_middle::ty::{self, Term, ToPredicate, Ty, TyCtxt};
37 use rustc_span::symbol::sym;
39 use std::collections::BTreeMap;
41 pub use rustc_middle::traits::Reveal;
43 pub type PolyProjectionObligation<'tcx> = Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
45 pub type ProjectionObligation<'tcx> = Obligation<'tcx, ty::ProjectionPredicate<'tcx>>;
47 pub type ProjectionTyObligation<'tcx> = Obligation<'tcx, ty::AliasTy<'tcx>>;
49 pub(super) struct InProgress;
51 pub trait NormalizeExt<'tcx> {
52 /// Normalize a value using the `AssocTypeNormalizer`.
54 /// This normalization should be used when the type contains inference variables or the
55 /// projection may be fallible.
56 fn normalize<T: TypeFoldable<'tcx>>(&self, t: T) -> InferOk<'tcx, T>;
59 impl<'tcx> NormalizeExt<'tcx> for At<'_, 'tcx> {
60 fn normalize<T: TypeFoldable<'tcx>>(&self, value: T) -> InferOk<'tcx, T> {
61 let mut selcx = SelectionContext::new(self.infcx);
62 let Normalized { value, obligations } =
63 normalize_with_depth(&mut selcx, self.param_env, self.cause.clone(), 0, value);
64 InferOk { value, obligations }
68 /// When attempting to resolve `<T as TraitRef>::Name` ...
70 pub enum ProjectionError<'tcx> {
71 /// ...we found multiple sources of information and couldn't resolve the ambiguity.
74 /// ...an error occurred matching `T : TraitRef`
75 TraitSelectionError(SelectionError<'tcx>),
78 #[derive(PartialEq, Eq, Debug)]
79 enum ProjectionCandidate<'tcx> {
80 /// From a where-clause in the env or object type
81 ParamEnv(ty::PolyProjectionPredicate<'tcx>),
83 /// From the definition of `Trait` when you have something like
84 /// `<<A as Trait>::B as Trait2>::C`.
85 TraitDef(ty::PolyProjectionPredicate<'tcx>),
87 /// Bounds specified on an object type
88 Object(ty::PolyProjectionPredicate<'tcx>),
90 /// From an "impl" (or a "pseudo-impl" returned by select)
91 Select(Selection<'tcx>),
93 ImplTraitInTrait(ImplTraitInTraitCandidate<'tcx>),
96 #[derive(PartialEq, Eq, Debug)]
97 enum ImplTraitInTraitCandidate<'tcx> {
98 // The `impl Trait` from a trait function's default body
100 // A concrete type provided from a trait's `impl Trait` from an impl
101 Impl(ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>),
104 enum ProjectionCandidateSet<'tcx> {
106 Single(ProjectionCandidate<'tcx>),
108 Error(SelectionError<'tcx>),
111 impl<'tcx> ProjectionCandidateSet<'tcx> {
112 fn mark_ambiguous(&mut self) {
113 *self = ProjectionCandidateSet::Ambiguous;
116 fn mark_error(&mut self, err: SelectionError<'tcx>) {
117 *self = ProjectionCandidateSet::Error(err);
120 // Returns true if the push was successful, or false if the candidate
121 // was discarded -- this could be because of ambiguity, or because
122 // a higher-priority candidate is already there.
123 fn push_candidate(&mut self, candidate: ProjectionCandidate<'tcx>) -> bool {
124 use self::ProjectionCandidate::*;
125 use self::ProjectionCandidateSet::*;
127 // This wacky variable is just used to try and
128 // make code readable and avoid confusing paths.
129 // It is assigned a "value" of `()` only on those
130 // paths in which we wish to convert `*self` to
131 // ambiguous (and return false, because the candidate
132 // was not used). On other paths, it is not assigned,
133 // and hence if those paths *could* reach the code that
134 // comes after the match, this fn would not compile.
135 let convert_to_ambiguous;
139 *self = Single(candidate);
144 // Duplicates can happen inside ParamEnv. In the case, we
145 // perform a lazy deduplication.
146 if current == &candidate {
150 // Prefer where-clauses. As in select, if there are multiple
151 // candidates, we prefer where-clause candidates over impls. This
152 // may seem a bit surprising, since impls are the source of
153 // "truth" in some sense, but in fact some of the impls that SEEM
154 // applicable are not, because of nested obligations. Where
155 // clauses are the safer choice. See the comment on
156 // `select::SelectionCandidate` and #21974 for more details.
157 match (current, candidate) {
158 (ParamEnv(..), ParamEnv(..)) => convert_to_ambiguous = (),
159 (ParamEnv(..), _) => return false,
160 (_, ParamEnv(..)) => unreachable!(),
161 (_, _) => convert_to_ambiguous = (),
165 Ambiguous | Error(..) => {
170 // We only ever get here when we moved from a single candidate
172 let () = convert_to_ambiguous;
178 /// States returned from `poly_project_and_unify_type`. Takes the place
179 /// of the old return type, which was:
180 /// ```ignore (not-rust)
182 /// Result<Option<Vec<PredicateObligation<'tcx>>>, InProgress>,
183 /// MismatchedProjectionTypes<'tcx>,
186 pub(super) enum ProjectAndUnifyResult<'tcx> {
187 /// The projection bound holds subject to the given obligations. If the
188 /// projection cannot be normalized because the required trait bound does
189 /// not hold, this is returned, with `obligations` being a predicate that
190 /// cannot be proven.
191 Holds(Vec<PredicateObligation<'tcx>>),
192 /// The projection cannot be normalized due to ambiguity. Resolving some
193 /// inference variables in the projection may fix this.
195 /// The project cannot be normalized because `poly_project_and_unify_type`
196 /// is called recursively while normalizing the same projection.
198 // the projection can be normalized, but is not equal to the expected type.
199 // Returns the type error that arose from the mismatch.
200 MismatchedProjectionTypes(MismatchedProjectionTypes<'tcx>),
203 /// Evaluates constraints of the form:
204 /// ```ignore (not-rust)
205 /// for<...> <T as Trait>::U == V
207 /// If successful, this may result in additional obligations. Also returns
208 /// the projection cache key used to track these additional obligations.
209 #[instrument(level = "debug", skip(selcx))]
210 pub(super) fn poly_project_and_unify_type<'cx, 'tcx>(
211 selcx: &mut SelectionContext<'cx, 'tcx>,
212 obligation: &PolyProjectionObligation<'tcx>,
213 ) -> ProjectAndUnifyResult<'tcx> {
214 let infcx = selcx.infcx;
215 let r = infcx.commit_if_ok(|_snapshot| {
216 let old_universe = infcx.universe();
217 let placeholder_predicate =
218 infcx.replace_bound_vars_with_placeholders(obligation.predicate);
219 let new_universe = infcx.universe();
221 let placeholder_obligation = obligation.with(infcx.tcx, placeholder_predicate);
222 match project_and_unify_type(selcx, &placeholder_obligation) {
223 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => Err(e),
224 ProjectAndUnifyResult::Holds(obligations)
225 if old_universe != new_universe
226 && selcx.tcx().features().generic_associated_types_extended =>
228 // If the `generic_associated_types_extended` feature is active, then we ignore any
229 // obligations references lifetimes from any universe greater than or equal to the
230 // universe just created. Otherwise, we can end up with something like `for<'a> I: 'a`,
231 // which isn't quite what we want. Ideally, we want either an implied
232 // `for<'a where I: 'a> I: 'a` or we want to "lazily" check these hold when we
233 // substitute concrete regions. There is design work to be done here; until then,
234 // however, this allows experimenting potential GAT features without running into
235 // well-formedness issues.
236 let new_obligations = obligations
238 .filter(|obligation| {
239 let mut visitor = MaxUniverse::new();
240 obligation.predicate.visit_with(&mut visitor);
241 visitor.max_universe() < new_universe
244 Ok(ProjectAndUnifyResult::Holds(new_obligations))
252 Err(err) => ProjectAndUnifyResult::MismatchedProjectionTypes(err),
256 /// Evaluates constraints of the form:
257 /// ```ignore (not-rust)
258 /// <T as Trait>::U == V
260 /// If successful, this may result in additional obligations.
262 /// See [poly_project_and_unify_type] for an explanation of the return value.
263 #[instrument(level = "debug", skip(selcx))]
264 fn project_and_unify_type<'cx, 'tcx>(
265 selcx: &mut SelectionContext<'cx, 'tcx>,
266 obligation: &ProjectionObligation<'tcx>,
267 ) -> ProjectAndUnifyResult<'tcx> {
268 let mut obligations = vec![];
270 let infcx = selcx.infcx;
271 let normalized = match opt_normalize_projection_type(
273 obligation.param_env,
274 obligation.predicate.projection_ty,
275 obligation.cause.clone(),
276 obligation.recursion_depth,
280 Ok(None) => return ProjectAndUnifyResult::FailedNormalization,
281 Err(InProgress) => return ProjectAndUnifyResult::Recursive,
283 debug!(?normalized, ?obligations, "project_and_unify_type result");
284 let actual = obligation.predicate.term;
285 // For an example where this is necessary see src/test/ui/impl-trait/nested-return-type2.rs
286 // This allows users to omit re-mentioning all bounds on an associated type and just use an
287 // `impl Trait` for the assoc type to add more bounds.
288 let InferOk { value: actual, obligations: new } =
289 selcx.infcx.replace_opaque_types_with_inference_vars(
291 obligation.cause.body_id,
292 obligation.cause.span,
293 obligation.param_env,
295 obligations.extend(new);
297 match infcx.at(&obligation.cause, obligation.param_env).eq(normalized, actual) {
298 Ok(InferOk { obligations: inferred_obligations, value: () }) => {
299 obligations.extend(inferred_obligations);
300 ProjectAndUnifyResult::Holds(obligations)
303 debug!("equating types encountered error {:?}", err);
304 ProjectAndUnifyResult::MismatchedProjectionTypes(MismatchedProjectionTypes { err })
309 /// As `normalize`, but with a custom depth.
310 pub(crate) fn normalize_with_depth<'a, 'b, 'tcx, T>(
311 selcx: &'a mut SelectionContext<'b, 'tcx>,
312 param_env: ty::ParamEnv<'tcx>,
313 cause: ObligationCause<'tcx>,
316 ) -> Normalized<'tcx, T>
318 T: TypeFoldable<'tcx>,
320 let mut obligations = Vec::new();
321 let value = normalize_with_depth_to(selcx, param_env, cause, depth, value, &mut obligations);
322 Normalized { value, obligations }
325 #[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
326 pub(crate) fn normalize_with_depth_to<'a, 'b, 'tcx, T>(
327 selcx: &'a mut SelectionContext<'b, 'tcx>,
328 param_env: ty::ParamEnv<'tcx>,
329 cause: ObligationCause<'tcx>,
332 obligations: &mut Vec<PredicateObligation<'tcx>>,
335 T: TypeFoldable<'tcx>,
337 debug!(obligations.len = obligations.len());
338 let mut normalizer = AssocTypeNormalizer::new(selcx, param_env, cause, depth, obligations);
339 let result = ensure_sufficient_stack(|| normalizer.fold(value));
340 debug!(?result, obligations.len = normalizer.obligations.len());
341 debug!(?normalizer.obligations,);
345 #[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
346 pub(crate) fn try_normalize_with_depth_to<'a, 'b, 'tcx, T>(
347 selcx: &'a mut SelectionContext<'b, 'tcx>,
348 param_env: ty::ParamEnv<'tcx>,
349 cause: ObligationCause<'tcx>,
352 obligations: &mut Vec<PredicateObligation<'tcx>>,
355 T: TypeFoldable<'tcx>,
357 debug!(obligations.len = obligations.len());
358 let mut normalizer = AssocTypeNormalizer::new_without_eager_inference_replacement(
365 let result = ensure_sufficient_stack(|| normalizer.fold(value));
366 debug!(?result, obligations.len = normalizer.obligations.len());
367 debug!(?normalizer.obligations,);
371 pub(crate) fn needs_normalization<'tcx, T: TypeVisitable<'tcx>>(value: &T, reveal: Reveal) -> bool {
373 Reveal::UserFacing => value
374 .has_type_flags(ty::TypeFlags::HAS_TY_PROJECTION | ty::TypeFlags::HAS_CT_PROJECTION),
375 Reveal::All => value.has_type_flags(
376 ty::TypeFlags::HAS_TY_PROJECTION
377 | ty::TypeFlags::HAS_TY_OPAQUE
378 | ty::TypeFlags::HAS_CT_PROJECTION,
383 struct AssocTypeNormalizer<'a, 'b, 'tcx> {
384 selcx: &'a mut SelectionContext<'b, 'tcx>,
385 param_env: ty::ParamEnv<'tcx>,
386 cause: ObligationCause<'tcx>,
387 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
389 universes: Vec<Option<ty::UniverseIndex>>,
390 /// If true, when a projection is unable to be completed, an inference
391 /// variable will be created and an obligation registered to project to that
392 /// inference variable. Also, constants will be eagerly evaluated.
393 eager_inference_replacement: bool,
396 impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
398 selcx: &'a mut SelectionContext<'b, 'tcx>,
399 param_env: ty::ParamEnv<'tcx>,
400 cause: ObligationCause<'tcx>,
402 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
403 ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
404 AssocTypeNormalizer {
411 eager_inference_replacement: true,
415 fn new_without_eager_inference_replacement(
416 selcx: &'a mut SelectionContext<'b, 'tcx>,
417 param_env: ty::ParamEnv<'tcx>,
418 cause: ObligationCause<'tcx>,
420 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
421 ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
422 AssocTypeNormalizer {
429 eager_inference_replacement: false,
433 fn fold<T: TypeFoldable<'tcx>>(&mut self, value: T) -> T {
434 let value = self.selcx.infcx.resolve_vars_if_possible(value);
438 !value.has_escaping_bound_vars(),
439 "Normalizing {:?} without wrapping in a `Binder`",
443 if !needs_normalization(&value, self.param_env.reveal()) {
446 value.fold_with(self)
451 impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
452 fn tcx<'c>(&'c self) -> TyCtxt<'tcx> {
456 fn fold_binder<T: TypeFoldable<'tcx>>(
458 t: ty::Binder<'tcx, T>,
459 ) -> ty::Binder<'tcx, T> {
460 self.universes.push(None);
461 let t = t.super_fold_with(self);
462 self.universes.pop();
466 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
467 if !needs_normalization(&ty, self.param_env.reveal()) {
471 // We try to be a little clever here as a performance optimization in
472 // cases where there are nested projections under binders.
475 // for<'a> fn(<T as Foo>::One<'a, Box<dyn Bar<'a, Item=<T as Foo>::Two<'a>>>>)
477 // We normalize the substs on the projection before the projecting, but
478 // if we're naive, we'll
479 // replace bound vars on inner, project inner, replace placeholders on inner,
480 // replace bound vars on outer, project outer, replace placeholders on outer
482 // However, if we're a bit more clever, we can replace the bound vars
483 // on the entire type before normalizing nested projections, meaning we
484 // replace bound vars on outer, project inner,
485 // project outer, replace placeholders on outer
487 // This is possible because the inner `'a` will already be a placeholder
488 // when we need to normalize the inner projection
490 // On the other hand, this does add a bit of complexity, since we only
491 // replace bound vars if the current type is a `Projection` and we need
492 // to make sure we don't forget to fold the substs regardless.
495 // This is really important. While we *can* handle this, this has
496 // severe performance implications for large opaque types with
497 // late-bound regions. See `issue-88862` benchmark.
498 ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. })
499 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 self.selcx.infcx.err_ctxt().report_overflow_error(
516 let substs = substs.fold_with(self);
517 let generic_ty = self.tcx().bound_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::Alias(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.
533 let data = data.fold_with(self);
534 let normalized_ty = if self.eager_inference_replacement {
535 normalize_projection_type(
541 &mut self.obligations,
544 opt_normalize_projection_type(
550 &mut self.obligations,
554 .unwrap_or_else(|| ty.super_fold_with(self).into())
560 obligations.len = ?self.obligations.len(),
561 "AssocTypeNormalizer: normalized type"
563 normalized_ty.ty().unwrap()
566 ty::Alias(ty::Projection, data) => {
567 // If there are escaping bound vars, we temporarily replace the
568 // bound vars with placeholders. Note though, that in the case
569 // that we still can't project for whatever reason (e.g. self
570 // type isn't known enough), we *can't* register an obligation
571 // and return an inference variable (since then that obligation
572 // would have bound vars and that's a can of worms). Instead,
573 // we just give up and fall back to pretending like we never tried!
575 // Note: this isn't necessarily the final approach here; we may
576 // want to figure out how to register obligations with escaping vars
577 // or handle this some other way.
579 let infcx = self.selcx.infcx;
580 let (data, mapped_regions, mapped_types, mapped_consts) =
581 BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, data);
582 let data = data.fold_with(self);
583 let normalized_ty = opt_normalize_projection_type(
589 &mut self.obligations,
593 .map(|term| term.ty().unwrap())
594 .map(|normalized_ty| {
595 PlaceholderReplacer::replace_placeholders(
604 .unwrap_or_else(|| ty.super_fold_with(self));
610 obligations.len = ?self.obligations.len(),
611 "AssocTypeNormalizer: normalized type"
616 _ => ty.super_fold_with(self),
620 #[instrument(skip(self), level = "debug")]
621 fn fold_const(&mut self, constant: ty::Const<'tcx>) -> ty::Const<'tcx> {
622 let tcx = self.selcx.tcx();
623 if tcx.lazy_normalization() || !needs_normalization(&constant, self.param_env.reveal()) {
626 let constant = constant.super_fold_with(self);
627 debug!(?constant, ?self.param_env);
628 with_replaced_escaping_bound_vars(
632 |constant| constant.eval(tcx, self.param_env),
638 fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
639 if p.allow_normalization() && needs_normalization(&p, self.param_env.reveal()) {
640 p.super_fold_with(self)
647 pub struct BoundVarReplacer<'me, 'tcx> {
648 infcx: &'me InferCtxt<'tcx>,
649 // These three maps track the bound variable that were replaced by placeholders. It might be
650 // nice to remove these since we already have the `kind` in the placeholder; we really just need
651 // the `var` (but we *could* bring that into scope if we were to track them as we pass them).
652 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
653 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
654 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
655 // The current depth relative to *this* folding, *not* the entire normalization. In other words,
656 // the depth of binders we've passed here.
657 current_index: ty::DebruijnIndex,
658 // The `UniverseIndex` of the binding levels above us. These are optional, since we are lazy:
659 // we don't actually create a universe until we see a bound var we have to replace.
660 universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
663 /// Executes `f` on `value` after replacing all escaping bound variables with placeholders
664 /// and then replaces these placeholders with the original bound variables in the result.
666 /// In most places, bound variables should be replaced right when entering a binder, making
667 /// this function unnecessary. However, normalization currently does not do that, so we have
668 /// to do this lazily.
670 /// You should not add any additional uses of this function, at least not without first
671 /// discussing it with t-types.
673 /// FIXME(@lcnr): We may even consider experimenting with eagerly replacing bound vars during
674 /// normalization as well, at which point this function will be unnecessary and can be removed.
675 pub fn with_replaced_escaping_bound_vars<'a, 'tcx, T: TypeFoldable<'tcx>, R: TypeFoldable<'tcx>>(
676 infcx: &'a InferCtxt<'tcx>,
677 universe_indices: &'a mut Vec<Option<ty::UniverseIndex>>,
679 f: impl FnOnce(T) -> R,
681 if value.has_escaping_bound_vars() {
682 let (value, mapped_regions, mapped_types, mapped_consts) =
683 BoundVarReplacer::replace_bound_vars(infcx, universe_indices, value);
684 let result = f(value);
685 PlaceholderReplacer::replace_placeholders(
698 impl<'me, 'tcx> BoundVarReplacer<'me, 'tcx> {
699 /// Returns `Some` if we *were* able to replace bound vars. If there are any bound vars that
700 /// use a binding level above `universe_indices.len()`, we fail.
701 pub fn replace_bound_vars<T: TypeFoldable<'tcx>>(
702 infcx: &'me InferCtxt<'tcx>,
703 universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
707 BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
708 BTreeMap<ty::PlaceholderType, ty::BoundTy>,
709 BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
711 let mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion> = BTreeMap::new();
712 let mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy> = BTreeMap::new();
713 let mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar> = BTreeMap::new();
715 let mut replacer = BoundVarReplacer {
720 current_index: ty::INNERMOST,
724 let value = value.fold_with(&mut replacer);
726 (value, replacer.mapped_regions, replacer.mapped_types, replacer.mapped_consts)
729 fn universe_for(&mut self, debruijn: ty::DebruijnIndex) -> ty::UniverseIndex {
730 let infcx = self.infcx;
732 self.universe_indices.len() + self.current_index.as_usize() - debruijn.as_usize() - 1;
733 let universe = self.universe_indices[index].unwrap_or_else(|| {
734 for i in self.universe_indices.iter_mut().take(index + 1) {
735 *i = i.or_else(|| Some(infcx.create_next_universe()))
737 self.universe_indices[index].unwrap()
743 impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
744 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
748 fn fold_binder<T: TypeFoldable<'tcx>>(
750 t: ty::Binder<'tcx, T>,
751 ) -> ty::Binder<'tcx, T> {
752 self.current_index.shift_in(1);
753 let t = t.super_fold_with(self);
754 self.current_index.shift_out(1);
758 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
760 ty::ReLateBound(debruijn, _)
761 if debruijn.as_usize() + 1
762 > self.current_index.as_usize() + self.universe_indices.len() =>
764 bug!("Bound vars outside of `self.universe_indices`");
766 ty::ReLateBound(debruijn, br) if debruijn >= self.current_index => {
767 let universe = self.universe_for(debruijn);
768 let p = ty::PlaceholderRegion { universe, name: br.kind };
769 self.mapped_regions.insert(p, br);
770 self.infcx.tcx.mk_region(ty::RePlaceholder(p))
776 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
778 ty::Bound(debruijn, _)
779 if debruijn.as_usize() + 1
780 > self.current_index.as_usize() + self.universe_indices.len() =>
782 bug!("Bound vars outside of `self.universe_indices`");
784 ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
785 let universe = self.universe_for(debruijn);
786 let p = ty::PlaceholderType { universe, name: bound_ty.var };
787 self.mapped_types.insert(p, bound_ty);
788 self.infcx.tcx.mk_ty(ty::Placeholder(p))
790 _ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
795 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
797 ty::ConstKind::Bound(debruijn, _)
798 if debruijn.as_usize() + 1
799 > self.current_index.as_usize() + self.universe_indices.len() =>
801 bug!("Bound vars outside of `self.universe_indices`");
803 ty::ConstKind::Bound(debruijn, bound_const) if debruijn >= self.current_index => {
804 let universe = self.universe_for(debruijn);
805 let p = ty::PlaceholderConst { universe, name: bound_const };
806 self.mapped_consts.insert(p, bound_const);
807 self.infcx.tcx.mk_const(p, ct.ty())
809 _ => ct.super_fold_with(self),
813 fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
814 if p.has_vars_bound_at_or_above(self.current_index) { p.super_fold_with(self) } else { p }
818 /// The inverse of [`BoundVarReplacer`]: replaces placeholders with the bound vars from which they came.
819 pub struct PlaceholderReplacer<'me, 'tcx> {
820 infcx: &'me InferCtxt<'tcx>,
821 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
822 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
823 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
824 universe_indices: &'me [Option<ty::UniverseIndex>],
825 current_index: ty::DebruijnIndex,
828 impl<'me, 'tcx> PlaceholderReplacer<'me, 'tcx> {
829 pub fn replace_placeholders<T: TypeFoldable<'tcx>>(
830 infcx: &'me InferCtxt<'tcx>,
831 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
832 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
833 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
834 universe_indices: &'me [Option<ty::UniverseIndex>],
837 let mut replacer = PlaceholderReplacer {
843 current_index: ty::INNERMOST,
845 value.fold_with(&mut replacer)
849 impl<'tcx> TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> {
850 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
854 fn fold_binder<T: TypeFoldable<'tcx>>(
856 t: ty::Binder<'tcx, T>,
857 ) -> ty::Binder<'tcx, T> {
858 if !t.has_placeholders() && !t.has_infer_regions() {
861 self.current_index.shift_in(1);
862 let t = t.super_fold_with(self);
863 self.current_index.shift_out(1);
867 fn fold_region(&mut self, r0: ty::Region<'tcx>) -> ty::Region<'tcx> {
873 .unwrap_region_constraints()
874 .opportunistic_resolve_region(self.infcx.tcx, r0),
879 ty::RePlaceholder(p) => {
880 let replace_var = self.mapped_regions.get(&p);
882 Some(replace_var) => {
886 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
887 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
888 let db = ty::DebruijnIndex::from_usize(
889 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
891 self.tcx().mk_region(ty::ReLateBound(db, *replace_var))
899 debug!(?r0, ?r1, ?r2, "fold_region");
904 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
906 ty::Placeholder(p) => {
907 let replace_var = self.mapped_types.get(&p);
909 Some(replace_var) => {
913 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
914 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
915 let db = ty::DebruijnIndex::from_usize(
916 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
918 self.tcx().mk_ty(ty::Bound(db, *replace_var))
924 _ if ty.has_placeholders() || ty.has_infer_regions() => ty.super_fold_with(self),
929 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
930 if let ty::ConstKind::Placeholder(p) = ct.kind() {
931 let replace_var = self.mapped_consts.get(&p);
933 Some(replace_var) => {
937 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
938 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
939 let db = ty::DebruijnIndex::from_usize(
940 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
942 self.tcx().mk_const(ty::ConstKind::Bound(db, *replace_var), ct.ty())
947 ct.super_fold_with(self)
952 /// The guts of `normalize`: normalize a specific projection like `<T
953 /// as Trait>::Item`. The result is always a type (and possibly
954 /// additional obligations). If ambiguity arises, which implies that
955 /// there are unresolved type variables in the projection, we will
956 /// substitute a fresh type variable `$X` and generate a new
957 /// obligation `<T as Trait>::Item == $X` for later.
958 pub fn normalize_projection_type<'a, 'b, 'tcx>(
959 selcx: &'a mut SelectionContext<'b, 'tcx>,
960 param_env: ty::ParamEnv<'tcx>,
961 projection_ty: ty::AliasTy<'tcx>,
962 cause: ObligationCause<'tcx>,
964 obligations: &mut Vec<PredicateObligation<'tcx>>,
966 opt_normalize_projection_type(
976 .unwrap_or_else(move || {
977 // if we bottom out in ambiguity, create a type variable
978 // and a deferred predicate to resolve this when more type
979 // information is available.
981 selcx.infcx.infer_projection(param_env, projection_ty, cause, depth + 1, obligations).into()
985 /// The guts of `normalize`: normalize a specific projection like `<T
986 /// as Trait>::Item`. The result is always a type (and possibly
987 /// additional obligations). Returns `None` in the case of ambiguity,
988 /// which indicates that there are unbound type variables.
990 /// This function used to return `Option<NormalizedTy<'tcx>>`, which contains a
991 /// `Ty<'tcx>` and an obligations vector. But that obligation vector was very
992 /// often immediately appended to another obligations vector. So now this
993 /// function takes an obligations vector and appends to it directly, which is
994 /// slightly uglier but avoids the need for an extra short-lived allocation.
995 #[instrument(level = "debug", skip(selcx, param_env, cause, obligations))]
996 fn opt_normalize_projection_type<'a, 'b, 'tcx>(
997 selcx: &'a mut SelectionContext<'b, 'tcx>,
998 param_env: ty::ParamEnv<'tcx>,
999 projection_ty: ty::AliasTy<'tcx>,
1000 cause: ObligationCause<'tcx>,
1002 obligations: &mut Vec<PredicateObligation<'tcx>>,
1003 ) -> Result<Option<Term<'tcx>>, InProgress> {
1004 let infcx = selcx.infcx;
1005 // Don't use the projection cache in intercrate mode -
1006 // the `infcx` may be re-used between intercrate in non-intercrate
1007 // mode, which could lead to using incorrect cache results.
1008 let use_cache = !selcx.is_intercrate();
1010 let projection_ty = infcx.resolve_vars_if_possible(projection_ty);
1011 let cache_key = ProjectionCacheKey::new(projection_ty);
1013 // FIXME(#20304) For now, I am caching here, which is good, but it
1014 // means we don't capture the type variables that are created in
1015 // the case of ambiguity. Which means we may create a large stream
1016 // of such variables. OTOH, if we move the caching up a level, we
1017 // would not benefit from caching when proving `T: Trait<U=Foo>`
1018 // bounds. It might be the case that we want two distinct caches,
1019 // or else another kind of cache entry.
1021 let cache_result = if use_cache {
1022 infcx.inner.borrow_mut().projection_cache().try_start(cache_key)
1026 match cache_result {
1027 Ok(()) => debug!("no cache"),
1028 Err(ProjectionCacheEntry::Ambiguous) => {
1029 // If we found ambiguity the last time, that means we will continue
1030 // to do so until some type in the key changes (and we know it
1031 // hasn't, because we just fully resolved it).
1032 debug!("found cache entry: ambiguous");
1035 Err(ProjectionCacheEntry::InProgress) => {
1036 // Under lazy normalization, this can arise when
1037 // bootstrapping. That is, imagine an environment with a
1038 // where-clause like `A::B == u32`. Now, if we are asked
1039 // to normalize `A::B`, we will want to check the
1040 // where-clauses in scope. So we will try to unify `A::B`
1041 // with `A::B`, which can trigger a recursive
1044 debug!("found cache entry: in-progress");
1046 // Cache that normalizing this projection resulted in a cycle. This
1047 // should ensure that, unless this happens within a snapshot that's
1048 // rolled back, fulfillment or evaluation will notice the cycle.
1051 infcx.inner.borrow_mut().projection_cache().recur(cache_key);
1053 return Err(InProgress);
1055 Err(ProjectionCacheEntry::Recur) => {
1056 debug!("recur cache");
1057 return Err(InProgress);
1059 Err(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => {
1060 // This is the hottest path in this function.
1062 // If we find the value in the cache, then return it along
1063 // with the obligations that went along with it. Note
1064 // that, when using a fulfillment context, these
1065 // obligations could in principle be ignored: they have
1066 // already been registered when the cache entry was
1067 // created (and hence the new ones will quickly be
1068 // discarded as duplicated). But when doing trait
1069 // evaluation this is not the case, and dropping the trait
1070 // evaluations can causes ICEs (e.g., #43132).
1071 debug!(?ty, "found normalized ty");
1072 obligations.extend(ty.obligations);
1073 return Ok(Some(ty.value));
1075 Err(ProjectionCacheEntry::Error) => {
1076 debug!("opt_normalize_projection_type: found error");
1077 let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
1078 obligations.extend(result.obligations);
1079 return Ok(Some(result.value.into()));
1084 Obligation::with_depth(selcx.tcx(), cause.clone(), depth, param_env, projection_ty);
1086 match project(selcx, &obligation) {
1087 Ok(Projected::Progress(Progress {
1088 term: projected_term,
1089 obligations: mut projected_obligations,
1091 // if projection succeeded, then what we get out of this
1092 // is also non-normalized (consider: it was derived from
1093 // an impl, where-clause etc) and hence we must
1096 let projected_term = selcx.infcx.resolve_vars_if_possible(projected_term);
1098 let mut result = if projected_term.has_projections() {
1099 let mut normalizer = AssocTypeNormalizer::new(
1104 &mut projected_obligations,
1106 let normalized_ty = normalizer.fold(projected_term);
1108 Normalized { value: normalized_ty, obligations: projected_obligations }
1110 Normalized { value: projected_term, obligations: projected_obligations }
1113 let mut deduped: SsoHashSet<_> = Default::default();
1114 result.obligations.drain_filter(|projected_obligation| {
1115 if !deduped.insert(projected_obligation.clone()) {
1122 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
1124 obligations.extend(result.obligations);
1125 Ok(Some(result.value))
1127 Ok(Projected::NoProgress(projected_ty)) => {
1128 let result = Normalized { value: projected_ty, obligations: vec![] };
1130 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
1132 // No need to extend `obligations`.
1133 Ok(Some(result.value))
1135 Err(ProjectionError::TooManyCandidates) => {
1136 debug!("opt_normalize_projection_type: too many candidates");
1138 infcx.inner.borrow_mut().projection_cache().ambiguous(cache_key);
1142 Err(ProjectionError::TraitSelectionError(_)) => {
1143 debug!("opt_normalize_projection_type: ERROR");
1144 // if we got an error processing the `T as Trait` part,
1145 // just return `ty::err` but add the obligation `T :
1146 // Trait`, which when processed will cause the error to be
1150 infcx.inner.borrow_mut().projection_cache().error(cache_key);
1152 let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
1153 obligations.extend(result.obligations);
1154 Ok(Some(result.value.into()))
1159 /// If we are projecting `<T as Trait>::Item`, but `T: Trait` does not
1160 /// hold. In various error cases, we cannot generate a valid
1161 /// normalized projection. Therefore, we create an inference variable
1162 /// return an associated obligation that, when fulfilled, will lead to
1165 /// Note that we used to return `Error` here, but that was quite
1166 /// dubious -- the premise was that an error would *eventually* be
1167 /// reported, when the obligation was processed. But in general once
1168 /// you see an `Error` you are supposed to be able to assume that an
1169 /// error *has been* reported, so that you can take whatever heuristic
1170 /// paths you want to take. To make things worse, it was possible for
1171 /// cycles to arise, where you basically had a setup like `<MyType<$0>
1172 /// as Trait>::Foo == $0`. Here, normalizing `<MyType<$0> as
1173 /// Trait>::Foo> to `[type error]` would lead to an obligation of
1174 /// `<MyType<[type error]> as Trait>::Foo`. We are supposed to report
1175 /// an error for this obligation, but we legitimately should not,
1176 /// because it contains `[type error]`. Yuck! (See issue #29857 for
1177 /// one case where this arose.)
1178 fn normalize_to_error<'a, 'tcx>(
1179 selcx: &mut SelectionContext<'a, 'tcx>,
1180 param_env: ty::ParamEnv<'tcx>,
1181 projection_ty: ty::AliasTy<'tcx>,
1182 cause: ObligationCause<'tcx>,
1184 ) -> NormalizedTy<'tcx> {
1185 let trait_ref = ty::Binder::dummy(projection_ty.trait_ref(selcx.tcx()));
1186 let trait_obligation = Obligation {
1188 recursion_depth: depth,
1190 predicate: trait_ref.without_const().to_predicate(selcx.tcx()),
1192 let tcx = selcx.infcx.tcx;
1193 let new_value = selcx.infcx.next_ty_var(TypeVariableOrigin {
1194 kind: TypeVariableOriginKind::NormalizeProjectionType,
1195 span: tcx.def_span(projection_ty.def_id),
1197 Normalized { value: new_value, obligations: vec![trait_obligation] }
1200 enum Projected<'tcx> {
1201 Progress(Progress<'tcx>),
1202 NoProgress(ty::Term<'tcx>),
1205 struct Progress<'tcx> {
1206 term: ty::Term<'tcx>,
1207 obligations: Vec<PredicateObligation<'tcx>>,
1210 impl<'tcx> Progress<'tcx> {
1211 fn error(tcx: TyCtxt<'tcx>) -> Self {
1212 Progress { term: tcx.ty_error().into(), obligations: vec![] }
1215 fn with_addl_obligations(mut self, mut obligations: Vec<PredicateObligation<'tcx>>) -> Self {
1216 self.obligations.append(&mut obligations);
1221 /// Computes the result of a projection type (if we can).
1224 /// - `obligation` must be fully normalized
1225 #[instrument(level = "info", skip(selcx))]
1226 fn project<'cx, 'tcx>(
1227 selcx: &mut SelectionContext<'cx, 'tcx>,
1228 obligation: &ProjectionTyObligation<'tcx>,
1229 ) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1230 if !selcx.tcx().recursion_limit().value_within_limit(obligation.recursion_depth) {
1231 // This should really be an immediate error, but some existing code
1232 // relies on being able to recover from this.
1233 return Err(ProjectionError::TraitSelectionError(SelectionError::Overflow(
1234 OverflowError::Canonical,
1238 if obligation.predicate.references_error() {
1239 return Ok(Projected::Progress(Progress::error(selcx.tcx())));
1242 let mut candidates = ProjectionCandidateSet::None;
1244 assemble_candidate_for_impl_trait_in_trait(selcx, obligation, &mut candidates);
1246 // Make sure that the following procedures are kept in order. ParamEnv
1247 // needs to be first because it has highest priority, and Select checks
1248 // the return value of push_candidate which assumes it's ran at last.
1249 assemble_candidates_from_param_env(selcx, obligation, &mut candidates);
1251 assemble_candidates_from_trait_def(selcx, obligation, &mut candidates);
1253 assemble_candidates_from_object_ty(selcx, obligation, &mut candidates);
1255 if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(_)) = candidates {
1256 // Avoid normalization cycle from selection (see
1257 // `assemble_candidates_from_object_ty`).
1258 // FIXME(lazy_normalization): Lazy normalization should save us from
1259 // having to special case this.
1261 assemble_candidates_from_impls(selcx, obligation, &mut candidates);
1265 ProjectionCandidateSet::Single(candidate) => {
1266 Ok(Projected::Progress(confirm_candidate(selcx, obligation, candidate)))
1268 ProjectionCandidateSet::None => Ok(Projected::NoProgress(
1269 // FIXME(associated_const_generics): this may need to change in the future?
1270 // need to investigate whether or not this is fine.
1273 .mk_projection(obligation.predicate.def_id, obligation.predicate.substs)
1276 // Error occurred while trying to processing impls.
1277 ProjectionCandidateSet::Error(e) => Err(ProjectionError::TraitSelectionError(e)),
1278 // Inherent ambiguity that prevents us from even enumerating the
1280 ProjectionCandidateSet::Ambiguous => Err(ProjectionError::TooManyCandidates),
1284 /// If the predicate's item is an `ImplTraitPlaceholder`, we do a select on the
1285 /// corresponding trait ref. If this yields an `impl`, then we're able to project
1286 /// to a concrete type, since we have an `impl`'s method to provide the RPITIT.
1287 fn assemble_candidate_for_impl_trait_in_trait<'cx, 'tcx>(
1288 selcx: &mut SelectionContext<'cx, 'tcx>,
1289 obligation: &ProjectionTyObligation<'tcx>,
1290 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1292 let tcx = selcx.tcx();
1293 if tcx.def_kind(obligation.predicate.def_id) == DefKind::ImplTraitPlaceholder {
1294 let trait_fn_def_id = tcx.impl_trait_in_trait_parent(obligation.predicate.def_id);
1295 // If we are trying to project an RPITIT with trait's default `Self` parameter,
1296 // then we must be within a default trait body.
1297 if obligation.predicate.self_ty()
1298 == ty::InternalSubsts::identity_for_item(tcx, obligation.predicate.def_id).type_at(0)
1299 && tcx.associated_item(trait_fn_def_id).defaultness(tcx).has_value()
1301 candidate_set.push_candidate(ProjectionCandidate::ImplTraitInTrait(
1302 ImplTraitInTraitCandidate::Trait,
1307 let trait_def_id = tcx.parent(trait_fn_def_id);
1309 obligation.predicate.substs.truncate_to(tcx, tcx.generics_of(trait_def_id));
1310 // FIXME(named-returns): Binders
1311 let trait_predicate = ty::Binder::dummy(tcx.mk_trait_ref(trait_def_id, trait_substs));
1313 let _ = selcx.infcx.commit_if_ok(|_| {
1314 match selcx.select(&obligation.with(tcx, trait_predicate)) {
1315 Ok(Some(super::ImplSource::UserDefined(data))) => {
1316 candidate_set.push_candidate(ProjectionCandidate::ImplTraitInTrait(
1317 ImplTraitInTraitCandidate::Impl(data),
1322 candidate_set.mark_ambiguous();
1326 // Don't know enough about the impl to provide a useful signature
1330 debug!(error = ?e, "selection error");
1331 candidate_set.mark_error(e);
1339 /// The first thing we have to do is scan through the parameter
1340 /// environment to see whether there are any projection predicates
1341 /// there that can answer this question.
1342 fn assemble_candidates_from_param_env<'cx, 'tcx>(
1343 selcx: &mut SelectionContext<'cx, 'tcx>,
1344 obligation: &ProjectionTyObligation<'tcx>,
1345 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1347 assemble_candidates_from_predicates(
1351 ProjectionCandidate::ParamEnv,
1352 obligation.param_env.caller_bounds().iter(),
1357 /// In the case of a nested projection like `<<A as Foo>::FooT as Bar>::BarT`, we may find
1358 /// that the definition of `Foo` has some clues:
1360 /// ```ignore (illustrative)
1362 /// type FooT : Bar<BarT=i32>
1366 /// Here, for example, we could conclude that the result is `i32`.
1367 fn assemble_candidates_from_trait_def<'cx, 'tcx>(
1368 selcx: &mut SelectionContext<'cx, 'tcx>,
1369 obligation: &ProjectionTyObligation<'tcx>,
1370 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1372 debug!("assemble_candidates_from_trait_def(..)");
1374 let tcx = selcx.tcx();
1375 // Check whether the self-type is itself a projection.
1376 // If so, extract what we know from the trait and try to come up with a good answer.
1377 let bounds = match *obligation.predicate.self_ty().kind() {
1378 ty::Alias(_, ref data) => tcx.bound_item_bounds(data.def_id).subst(tcx, data.substs),
1379 ty::Infer(ty::TyVar(_)) => {
1380 // If the self-type is an inference variable, then it MAY wind up
1381 // being a projected type, so induce an ambiguity.
1382 candidate_set.mark_ambiguous();
1388 assemble_candidates_from_predicates(
1392 ProjectionCandidate::TraitDef,
1398 /// In the case of a trait object like
1399 /// `<dyn Iterator<Item = ()> as Iterator>::Item` we can use the existential
1400 /// predicate in the trait object.
1402 /// We don't go through the select candidate for these bounds to avoid cycles:
1403 /// In the above case, `dyn Iterator<Item = ()>: Iterator` would create a
1404 /// nested obligation of `<dyn Iterator<Item = ()> as Iterator>::Item: Sized`,
1405 /// this then has to be normalized without having to prove
1406 /// `dyn Iterator<Item = ()>: Iterator` again.
1407 fn assemble_candidates_from_object_ty<'cx, 'tcx>(
1408 selcx: &mut SelectionContext<'cx, 'tcx>,
1409 obligation: &ProjectionTyObligation<'tcx>,
1410 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1412 debug!("assemble_candidates_from_object_ty(..)");
1414 let tcx = selcx.tcx();
1416 let self_ty = obligation.predicate.self_ty();
1417 let object_ty = selcx.infcx.shallow_resolve(self_ty);
1418 let data = match object_ty.kind() {
1419 ty::Dynamic(data, ..) => data,
1420 ty::Infer(ty::TyVar(_)) => {
1421 // If the self-type is an inference variable, then it MAY wind up
1422 // being an object type, so induce an ambiguity.
1423 candidate_set.mark_ambiguous();
1428 let env_predicates = data
1429 .projection_bounds()
1430 .filter(|bound| bound.item_def_id() == obligation.predicate.def_id)
1431 .map(|p| p.with_self_ty(tcx, object_ty).to_predicate(tcx));
1433 assemble_candidates_from_predicates(
1437 ProjectionCandidate::Object,
1445 skip(selcx, candidate_set, ctor, env_predicates, potentially_unnormalized_candidates)
1447 fn assemble_candidates_from_predicates<'cx, 'tcx>(
1448 selcx: &mut SelectionContext<'cx, 'tcx>,
1449 obligation: &ProjectionTyObligation<'tcx>,
1450 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1451 ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionCandidate<'tcx>,
1452 env_predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
1453 potentially_unnormalized_candidates: bool,
1455 let infcx = selcx.infcx;
1456 for predicate in env_predicates {
1457 let bound_predicate = predicate.kind();
1458 if let ty::PredicateKind::Clause(ty::Clause::Projection(data)) =
1459 predicate.kind().skip_binder()
1461 let data = bound_predicate.rebind(data);
1462 if data.projection_def_id() != obligation.predicate.def_id {
1466 let is_match = infcx.probe(|_| {
1467 selcx.match_projection_projections(
1470 potentially_unnormalized_candidates,
1475 ProjectionMatchesProjection::Yes => {
1476 candidate_set.push_candidate(ctor(data));
1478 if potentially_unnormalized_candidates
1479 && !obligation.predicate.has_non_region_infer()
1481 // HACK: Pick the first trait def candidate for a fully
1482 // inferred predicate. This is to allow duplicates that
1483 // differ only in normalization.
1487 ProjectionMatchesProjection::Ambiguous => {
1488 candidate_set.mark_ambiguous();
1490 ProjectionMatchesProjection::No => {}
1496 #[instrument(level = "debug", skip(selcx, obligation, candidate_set))]
1497 fn assemble_candidates_from_impls<'cx, 'tcx>(
1498 selcx: &mut SelectionContext<'cx, 'tcx>,
1499 obligation: &ProjectionTyObligation<'tcx>,
1500 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1502 // Can't assemble candidate from impl for RPITIT
1503 if selcx.tcx().def_kind(obligation.predicate.def_id) == DefKind::ImplTraitPlaceholder {
1507 // If we are resolving `<T as TraitRef<...>>::Item == Type`,
1508 // start out by selecting the predicate `T as TraitRef<...>`:
1509 let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx()));
1510 let trait_obligation = obligation.with(selcx.tcx(), poly_trait_ref);
1511 let _ = selcx.infcx.commit_if_ok(|_| {
1512 let impl_source = match selcx.select(&trait_obligation) {
1513 Ok(Some(impl_source)) => impl_source,
1515 candidate_set.mark_ambiguous();
1519 debug!(error = ?e, "selection error");
1520 candidate_set.mark_error(e);
1525 let eligible = match &impl_source {
1526 super::ImplSource::Closure(_)
1527 | super::ImplSource::Generator(_)
1528 | super::ImplSource::Future(_)
1529 | super::ImplSource::FnPointer(_)
1530 | super::ImplSource::TraitAlias(_) => true,
1531 super::ImplSource::UserDefined(impl_data) => {
1532 // We have to be careful when projecting out of an
1533 // impl because of specialization. If we are not in
1534 // codegen (i.e., projection mode is not "any"), and the
1535 // impl's type is declared as default, then we disable
1536 // projection (even if the trait ref is fully
1537 // monomorphic). In the case where trait ref is not
1538 // fully monomorphic (i.e., includes type parameters),
1539 // this is because those type parameters may
1540 // ultimately be bound to types from other crates that
1541 // may have specialized impls we can't see. In the
1542 // case where the trait ref IS fully monomorphic, this
1543 // is a policy decision that we made in the RFC in
1544 // order to preserve flexibility for the crate that
1545 // defined the specializable impl to specialize later
1546 // for existing types.
1548 // In either case, we handle this by not adding a
1549 // candidate for an impl if it contains a `default`
1552 // NOTE: This should be kept in sync with the similar code in
1553 // `rustc_ty_utils::instance::resolve_associated_item()`.
1555 specialization_graph::assoc_def(selcx.tcx(), impl_data.impl_def_id, obligation.predicate.def_id)
1556 .map_err(|ErrorGuaranteed { .. }| ())?;
1558 if node_item.is_final() {
1559 // Non-specializable items are always projectable.
1562 // Only reveal a specializable default if we're past type-checking
1563 // and the obligation is monomorphic, otherwise passes such as
1564 // transmute checking and polymorphic MIR optimizations could
1565 // get a result which isn't correct for all monomorphizations.
1566 if obligation.param_env.reveal() == Reveal::All {
1567 // NOTE(eddyb) inference variables can resolve to parameters, so
1568 // assume `poly_trait_ref` isn't monomorphic, if it contains any.
1569 let poly_trait_ref = selcx.infcx.resolve_vars_if_possible(poly_trait_ref);
1570 !poly_trait_ref.still_further_specializable()
1573 assoc_ty = ?selcx.tcx().def_path_str(node_item.item.def_id),
1574 ?obligation.predicate,
1575 "assemble_candidates_from_impls: not eligible due to default",
1581 super::ImplSource::Builtin(..) => {
1582 // While a builtin impl may be known to exist, the associated type may not yet
1583 // be known. Any type with multiple potential associated types is therefore
1585 let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1587 let lang_items = selcx.tcx().lang_items();
1588 if lang_items.discriminant_kind_trait() == Some(poly_trait_ref.def_id()) {
1589 match self_ty.kind() {
1607 | ty::GeneratorWitness(..)
1610 // Integers and floats always have `u8` as their discriminant.
1611 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1613 // type parameters, opaques, and unnormalized projections have pointer
1614 // metadata if they're known (e.g. by the param_env) to be sized
1618 | ty::Placeholder(..)
1620 | ty::Error(_) => false,
1622 } else if lang_items.pointee_trait() == Some(poly_trait_ref.def_id()) {
1623 let tail = selcx.tcx().struct_tail_with_normalize(
1626 // We throw away any obligations we get from this, since we normalize
1627 // and confirm these obligations once again during confirmation
1628 normalize_with_depth(
1630 obligation.param_env,
1631 obligation.cause.clone(),
1632 obligation.recursion_depth + 1,
1656 | ty::GeneratorWitness(..)
1658 // Extern types have unit metadata, according to RFC 2850
1660 // If returned by `struct_tail_without_normalization` this is a unit struct
1661 // without any fields, or not a struct, and therefore is Sized.
1663 // If returned by `struct_tail_without_normalization` this is the empty tuple.
1665 // Integers and floats are always Sized, and so have unit type metadata.
1666 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1668 // type parameters, opaques, and unnormalized projections have pointer
1669 // metadata if they're known (e.g. by the param_env) to be sized
1670 ty::Param(_) | ty::Alias(..)
1671 if selcx.infcx.predicate_must_hold_modulo_regions(
1675 selcx.tcx().at(obligation.cause.span()).mk_trait_ref(LangItem::Sized, [self_ty]),
1684 // FIXME(compiler-errors): are Bound and Placeholder types ever known sized?
1688 | ty::Placeholder(..)
1691 if tail.has_infer_types() {
1692 candidate_set.mark_ambiguous();
1698 bug!("unexpected builtin trait with associated type: {poly_trait_ref:?}")
1701 super::ImplSource::Param(..) => {
1702 // This case tell us nothing about the value of an
1703 // associated type. Consider:
1706 // trait SomeTrait { type Foo; }
1707 // fn foo<T:SomeTrait>(...) { }
1710 // If the user writes `<T as SomeTrait>::Foo`, then the `T
1711 // : SomeTrait` binding does not help us decide what the
1712 // type `Foo` is (at least, not more specifically than
1713 // what we already knew).
1715 // But wait, you say! What about an example like this:
1718 // fn bar<T:SomeTrait<Foo=usize>>(...) { ... }
1721 // Doesn't the `T : SomeTrait<Foo=usize>` predicate help
1722 // resolve `T::Foo`? And of course it does, but in fact
1723 // that single predicate is desugared into two predicates
1724 // in the compiler: a trait predicate (`T : SomeTrait`) and a
1725 // projection. And the projection where clause is handled
1726 // in `assemble_candidates_from_param_env`.
1729 super::ImplSource::Object(_) => {
1730 // Handled by the `Object` projection candidate. See
1731 // `assemble_candidates_from_object_ty` for an explanation of
1732 // why we special case object types.
1735 super::ImplSource::AutoImpl(..)
1736 | super::ImplSource::TraitUpcasting(_)
1737 | super::ImplSource::ConstDestruct(_) => {
1738 // These traits have no associated types.
1739 selcx.tcx().sess.delay_span_bug(
1740 obligation.cause.span,
1741 &format!("Cannot project an associated type from `{:?}`", impl_source),
1748 if candidate_set.push_candidate(ProjectionCandidate::Select(impl_source)) {
1759 fn confirm_candidate<'cx, 'tcx>(
1760 selcx: &mut SelectionContext<'cx, 'tcx>,
1761 obligation: &ProjectionTyObligation<'tcx>,
1762 candidate: ProjectionCandidate<'tcx>,
1763 ) -> Progress<'tcx> {
1764 debug!(?obligation, ?candidate, "confirm_candidate");
1765 let mut progress = match candidate {
1766 ProjectionCandidate::ParamEnv(poly_projection)
1767 | ProjectionCandidate::Object(poly_projection) => {
1768 confirm_param_env_candidate(selcx, obligation, poly_projection, false)
1771 ProjectionCandidate::TraitDef(poly_projection) => {
1772 confirm_param_env_candidate(selcx, obligation, poly_projection, true)
1775 ProjectionCandidate::Select(impl_source) => {
1776 confirm_select_candidate(selcx, obligation, impl_source)
1778 ProjectionCandidate::ImplTraitInTrait(ImplTraitInTraitCandidate::Impl(data)) => {
1779 confirm_impl_trait_in_trait_candidate(selcx, obligation, data)
1781 // If we're projecting an RPITIT for a default trait body, that's just
1782 // the same def-id, but as an opaque type (with regular RPIT semantics).
1783 ProjectionCandidate::ImplTraitInTrait(ImplTraitInTraitCandidate::Trait) => Progress {
1786 .mk_opaque(obligation.predicate.def_id, obligation.predicate.substs)
1788 obligations: vec![],
1792 // When checking for cycle during evaluation, we compare predicates with
1793 // "syntactic" equality. Since normalization generally introduces a type
1794 // with new region variables, we need to resolve them to existing variables
1795 // when possible for this to work. See `auto-trait-projection-recursion.rs`
1796 // for a case where this matters.
1797 if progress.term.has_infer_regions() {
1798 progress.term = progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx));
1803 fn confirm_select_candidate<'cx, 'tcx>(
1804 selcx: &mut SelectionContext<'cx, 'tcx>,
1805 obligation: &ProjectionTyObligation<'tcx>,
1806 impl_source: Selection<'tcx>,
1807 ) -> Progress<'tcx> {
1809 super::ImplSource::UserDefined(data) => confirm_impl_candidate(selcx, obligation, data),
1810 super::ImplSource::Generator(data) => confirm_generator_candidate(selcx, obligation, data),
1811 super::ImplSource::Future(data) => confirm_future_candidate(selcx, obligation, data),
1812 super::ImplSource::Closure(data) => confirm_closure_candidate(selcx, obligation, data),
1813 super::ImplSource::FnPointer(data) => confirm_fn_pointer_candidate(selcx, obligation, data),
1814 super::ImplSource::Builtin(data) => confirm_builtin_candidate(selcx, obligation, data),
1815 super::ImplSource::Object(_)
1816 | super::ImplSource::AutoImpl(..)
1817 | super::ImplSource::Param(..)
1818 | super::ImplSource::TraitUpcasting(_)
1819 | super::ImplSource::TraitAlias(..)
1820 | super::ImplSource::ConstDestruct(_) => {
1821 // we don't create Select candidates with this kind of resolution
1823 obligation.cause.span,
1824 "Cannot project an associated type from `{:?}`",
1831 fn confirm_generator_candidate<'cx, 'tcx>(
1832 selcx: &mut SelectionContext<'cx, 'tcx>,
1833 obligation: &ProjectionTyObligation<'tcx>,
1834 impl_source: ImplSourceGeneratorData<'tcx, PredicateObligation<'tcx>>,
1835 ) -> Progress<'tcx> {
1836 let gen_sig = impl_source.substs.as_generator().poly_sig();
1837 let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1839 obligation.param_env,
1840 obligation.cause.clone(),
1841 obligation.recursion_depth + 1,
1845 debug!(?obligation, ?gen_sig, ?obligations, "confirm_generator_candidate");
1847 let tcx = selcx.tcx();
1849 let gen_def_id = tcx.require_lang_item(LangItem::Generator, None);
1851 let predicate = super::util::generator_trait_ref_and_outputs(
1854 obligation.predicate.self_ty(),
1857 .map_bound(|(trait_ref, yield_ty, return_ty)| {
1858 let name = tcx.associated_item(obligation.predicate.def_id).name;
1859 let ty = if name == sym::Return {
1861 } else if name == sym::Yield {
1867 ty::ProjectionPredicate {
1868 projection_ty: tcx.mk_alias_ty(obligation.predicate.def_id, trait_ref.substs),
1873 confirm_param_env_candidate(selcx, obligation, predicate, false)
1874 .with_addl_obligations(impl_source.nested)
1875 .with_addl_obligations(obligations)
1878 fn confirm_future_candidate<'cx, 'tcx>(
1879 selcx: &mut SelectionContext<'cx, 'tcx>,
1880 obligation: &ProjectionTyObligation<'tcx>,
1881 impl_source: ImplSourceFutureData<'tcx, PredicateObligation<'tcx>>,
1882 ) -> Progress<'tcx> {
1883 let gen_sig = impl_source.substs.as_generator().poly_sig();
1884 let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1886 obligation.param_env,
1887 obligation.cause.clone(),
1888 obligation.recursion_depth + 1,
1892 debug!(?obligation, ?gen_sig, ?obligations, "confirm_future_candidate");
1894 let tcx = selcx.tcx();
1895 let fut_def_id = tcx.require_lang_item(LangItem::Future, None);
1897 let predicate = super::util::future_trait_ref_and_outputs(
1900 obligation.predicate.self_ty(),
1903 .map_bound(|(trait_ref, return_ty)| {
1904 debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Output);
1906 ty::ProjectionPredicate {
1907 projection_ty: tcx.mk_alias_ty(obligation.predicate.def_id, trait_ref.substs),
1908 term: return_ty.into(),
1912 confirm_param_env_candidate(selcx, obligation, predicate, false)
1913 .with_addl_obligations(impl_source.nested)
1914 .with_addl_obligations(obligations)
1917 fn confirm_builtin_candidate<'cx, 'tcx>(
1918 selcx: &mut SelectionContext<'cx, 'tcx>,
1919 obligation: &ProjectionTyObligation<'tcx>,
1920 data: ImplSourceBuiltinData<PredicateObligation<'tcx>>,
1921 ) -> Progress<'tcx> {
1922 let tcx = selcx.tcx();
1923 let self_ty = obligation.predicate.self_ty();
1924 let substs = tcx.mk_substs([self_ty.into()].iter());
1925 let lang_items = tcx.lang_items();
1926 let item_def_id = obligation.predicate.def_id;
1927 let trait_def_id = tcx.trait_of_item(item_def_id).unwrap();
1928 let (term, obligations) = if lang_items.discriminant_kind_trait() == Some(trait_def_id) {
1929 let discriminant_def_id = tcx.require_lang_item(LangItem::Discriminant, None);
1930 assert_eq!(discriminant_def_id, item_def_id);
1932 (self_ty.discriminant_ty(tcx).into(), Vec::new())
1933 } else if lang_items.pointee_trait() == Some(trait_def_id) {
1934 let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
1935 assert_eq!(metadata_def_id, item_def_id);
1937 let mut obligations = Vec::new();
1938 let (metadata_ty, check_is_sized) = self_ty.ptr_metadata_ty(tcx, |ty| {
1939 normalize_with_depth_to(
1941 obligation.param_env,
1942 obligation.cause.clone(),
1943 obligation.recursion_depth + 1,
1949 let sized_predicate = ty::Binder::dummy(
1950 tcx.at(obligation.cause.span()).mk_trait_ref(LangItem::Sized, [self_ty]),
1953 obligations.push(obligation.with(tcx, sized_predicate));
1955 (metadata_ty.into(), obligations)
1957 bug!("unexpected builtin trait with associated type: {:?}", obligation.predicate);
1961 ty::ProjectionPredicate { projection_ty: tcx.mk_alias_ty(item_def_id, substs), term };
1963 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1964 .with_addl_obligations(obligations)
1965 .with_addl_obligations(data.nested)
1968 fn confirm_fn_pointer_candidate<'cx, 'tcx>(
1969 selcx: &mut SelectionContext<'cx, 'tcx>,
1970 obligation: &ProjectionTyObligation<'tcx>,
1971 fn_pointer_impl_source: ImplSourceFnPointerData<'tcx, PredicateObligation<'tcx>>,
1972 ) -> Progress<'tcx> {
1973 let fn_type = selcx.infcx.shallow_resolve(fn_pointer_impl_source.fn_ty);
1974 let sig = fn_type.fn_sig(selcx.tcx());
1975 let Normalized { value: sig, obligations } = normalize_with_depth(
1977 obligation.param_env,
1978 obligation.cause.clone(),
1979 obligation.recursion_depth + 1,
1983 confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
1984 .with_addl_obligations(fn_pointer_impl_source.nested)
1985 .with_addl_obligations(obligations)
1988 fn confirm_closure_candidate<'cx, 'tcx>(
1989 selcx: &mut SelectionContext<'cx, 'tcx>,
1990 obligation: &ProjectionTyObligation<'tcx>,
1991 impl_source: ImplSourceClosureData<'tcx, PredicateObligation<'tcx>>,
1992 ) -> Progress<'tcx> {
1993 let closure_sig = impl_source.substs.as_closure().sig();
1994 let Normalized { value: closure_sig, obligations } = normalize_with_depth(
1996 obligation.param_env,
1997 obligation.cause.clone(),
1998 obligation.recursion_depth + 1,
2002 debug!(?obligation, ?closure_sig, ?obligations, "confirm_closure_candidate");
2004 confirm_callable_candidate(selcx, obligation, closure_sig, util::TupleArgumentsFlag::No)
2005 .with_addl_obligations(impl_source.nested)
2006 .with_addl_obligations(obligations)
2009 fn confirm_callable_candidate<'cx, 'tcx>(
2010 selcx: &mut SelectionContext<'cx, 'tcx>,
2011 obligation: &ProjectionTyObligation<'tcx>,
2012 fn_sig: ty::PolyFnSig<'tcx>,
2013 flag: util::TupleArgumentsFlag,
2014 ) -> Progress<'tcx> {
2015 let tcx = selcx.tcx();
2017 debug!(?obligation, ?fn_sig, "confirm_callable_candidate");
2019 let fn_once_def_id = tcx.require_lang_item(LangItem::FnOnce, None);
2020 let fn_once_output_def_id = tcx.require_lang_item(LangItem::FnOnceOutput, None);
2022 let predicate = super::util::closure_trait_ref_and_return_type(
2025 obligation.predicate.self_ty(),
2029 .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
2030 projection_ty: tcx.mk_alias_ty(fn_once_output_def_id, trait_ref.substs),
2031 term: ret_type.into(),
2034 confirm_param_env_candidate(selcx, obligation, predicate, true)
2037 fn confirm_param_env_candidate<'cx, 'tcx>(
2038 selcx: &mut SelectionContext<'cx, 'tcx>,
2039 obligation: &ProjectionTyObligation<'tcx>,
2040 poly_cache_entry: ty::PolyProjectionPredicate<'tcx>,
2041 potentially_unnormalized_candidate: bool,
2042 ) -> Progress<'tcx> {
2043 let infcx = selcx.infcx;
2044 let cause = &obligation.cause;
2045 let param_env = obligation.param_env;
2047 let cache_entry = infcx.replace_bound_vars_with_fresh_vars(
2049 LateBoundRegionConversionTime::HigherRankedType,
2053 let cache_projection = cache_entry.projection_ty;
2054 let mut nested_obligations = Vec::new();
2055 let obligation_projection = obligation.predicate;
2056 let obligation_projection = ensure_sufficient_stack(|| {
2057 normalize_with_depth_to(
2059 obligation.param_env,
2060 obligation.cause.clone(),
2061 obligation.recursion_depth + 1,
2062 obligation_projection,
2063 &mut nested_obligations,
2066 let cache_projection = if potentially_unnormalized_candidate {
2067 ensure_sufficient_stack(|| {
2068 normalize_with_depth_to(
2070 obligation.param_env,
2071 obligation.cause.clone(),
2072 obligation.recursion_depth + 1,
2074 &mut nested_obligations,
2081 debug!(?cache_projection, ?obligation_projection);
2083 match infcx.at(cause, param_env).eq(cache_projection, obligation_projection) {
2084 Ok(InferOk { value: _, obligations }) => {
2085 nested_obligations.extend(obligations);
2086 assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations);
2087 // FIXME(associated_const_equality): Handle consts here as well? Maybe this progress type should just take
2089 Progress { term: cache_entry.term, obligations: nested_obligations }
2093 "Failed to unify obligation `{:?}` with poly_projection `{:?}`: {:?}",
2094 obligation, poly_cache_entry, e,
2096 debug!("confirm_param_env_candidate: {}", msg);
2097 let err = infcx.tcx.ty_error_with_message(obligation.cause.span, &msg);
2098 Progress { term: err.into(), obligations: vec![] }
2103 fn confirm_impl_candidate<'cx, 'tcx>(
2104 selcx: &mut SelectionContext<'cx, 'tcx>,
2105 obligation: &ProjectionTyObligation<'tcx>,
2106 impl_impl_source: ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>,
2107 ) -> Progress<'tcx> {
2108 let tcx = selcx.tcx();
2110 let ImplSourceUserDefinedData { impl_def_id, substs, mut nested } = impl_impl_source;
2111 let assoc_item_id = obligation.predicate.def_id;
2112 let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
2114 let param_env = obligation.param_env;
2115 let Ok(assoc_ty) = specialization_graph::assoc_def(tcx, impl_def_id, assoc_item_id) else {
2116 return Progress { term: tcx.ty_error().into(), obligations: nested };
2119 if !assoc_ty.item.defaultness(tcx).has_value() {
2120 // This means that the impl is missing a definition for the
2121 // associated type. This error will be reported by the type
2122 // checker method `check_impl_items_against_trait`, so here we
2123 // just return Error.
2125 "confirm_impl_candidate: no associated type {:?} for {:?}",
2126 assoc_ty.item.name, obligation.predicate
2128 return Progress { term: tcx.ty_error().into(), obligations: nested };
2130 // If we're trying to normalize `<Vec<u32> as X>::A<S>` using
2131 //`impl<T> X for Vec<T> { type A<Y> = Box<Y>; }`, then:
2133 // * `obligation.predicate.substs` is `[Vec<u32>, S]`
2134 // * `substs` is `[u32]`
2135 // * `substs` ends up as `[u32, S]`
2136 let substs = obligation.predicate.substs.rebase_onto(tcx, trait_def_id, substs);
2138 translate_substs(selcx.infcx, param_env, impl_def_id, substs, assoc_ty.defining_node);
2139 let ty = tcx.bound_type_of(assoc_ty.item.def_id);
2140 let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst);
2141 let term: ty::EarlyBinder<ty::Term<'tcx>> = if is_const {
2142 let identity_substs =
2143 crate::traits::InternalSubsts::identity_for_item(tcx, assoc_ty.item.def_id);
2144 let did = ty::WithOptConstParam::unknown(assoc_ty.item.def_id);
2145 let kind = ty::ConstKind::Unevaluated(ty::UnevaluatedConst::new(did, identity_substs));
2146 ty.map_bound(|ty| tcx.mk_const(kind, ty).into())
2148 ty.map_bound(|ty| ty.into())
2150 if !check_substs_compatible(tcx, &assoc_ty.item, substs) {
2151 let err = tcx.ty_error_with_message(
2152 obligation.cause.span,
2153 "impl item and trait item have different parameters",
2155 Progress { term: err.into(), obligations: nested }
2157 assoc_ty_own_obligations(selcx, obligation, &mut nested);
2158 Progress { term: term.subst(tcx, substs), obligations: nested }
2162 // Verify that the trait item and its implementation have compatible substs lists
2163 fn check_substs_compatible<'tcx>(
2165 assoc_item: &ty::AssocItem,
2166 substs: ty::SubstsRef<'tcx>,
2168 fn check_substs_compatible_inner<'tcx>(
2170 generics: &'tcx ty::Generics,
2171 args: &'tcx [ty::GenericArg<'tcx>],
2173 if generics.count() != args.len() {
2177 let (parent_args, own_args) = args.split_at(generics.parent_count);
2179 if let Some(parent) = generics.parent
2180 && let parent_generics = tcx.generics_of(parent)
2181 && !check_substs_compatible_inner(tcx, parent_generics, parent_args) {
2185 for (param, arg) in std::iter::zip(&generics.params, own_args) {
2186 match (¶m.kind, arg.unpack()) {
2187 (ty::GenericParamDefKind::Type { .. }, ty::GenericArgKind::Type(_))
2188 | (ty::GenericParamDefKind::Lifetime, ty::GenericArgKind::Lifetime(_))
2189 | (ty::GenericParamDefKind::Const { .. }, ty::GenericArgKind::Const(_)) => {}
2197 let generics = tcx.generics_of(assoc_item.def_id);
2198 // Chop off any additional substs (RPITIT) substs
2199 let substs = &substs[0..generics.count().min(substs.len())];
2200 check_substs_compatible_inner(tcx, generics, substs)
2203 fn confirm_impl_trait_in_trait_candidate<'tcx>(
2204 selcx: &mut SelectionContext<'_, 'tcx>,
2205 obligation: &ProjectionTyObligation<'tcx>,
2206 data: ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>,
2207 ) -> Progress<'tcx> {
2208 let tcx = selcx.tcx();
2209 let mut obligations = data.nested;
2211 let trait_fn_def_id = tcx.impl_trait_in_trait_parent(obligation.predicate.def_id);
2212 let Ok(leaf_def) = specialization_graph::assoc_def(tcx, data.impl_def_id, trait_fn_def_id) else {
2213 return Progress { term: tcx.ty_error().into(), obligations };
2215 if !leaf_def.item.defaultness(tcx).has_value() {
2216 return Progress { term: tcx.ty_error().into(), obligations };
2219 // Use the default `impl Trait` for the trait, e.g., for a default trait body
2220 if leaf_def.item.container == ty::AssocItemContainer::TraitContainer {
2222 term: tcx.mk_opaque(obligation.predicate.def_id, obligation.predicate.substs).into(),
2227 // Rebase from {trait}::{fn}::{opaque} to {impl}::{fn}::{opaque},
2228 // since `data.substs` are the impl substs.
2229 let impl_fn_substs =
2230 obligation.predicate.substs.rebase_onto(tcx, tcx.parent(trait_fn_def_id), data.substs);
2231 let impl_fn_substs = translate_substs(
2233 obligation.param_env,
2236 leaf_def.defining_node,
2239 if !check_substs_compatible(tcx, &leaf_def.item, impl_fn_substs) {
2240 let err = tcx.ty_error_with_message(
2241 obligation.cause.span,
2242 "impl method and trait method have different parameters",
2244 return Progress { term: err.into(), obligations };
2247 let impl_fn_def_id = leaf_def.item.def_id;
2249 let cause = ObligationCause::new(
2250 obligation.cause.span,
2251 obligation.cause.body_id,
2252 super::ItemObligation(impl_fn_def_id),
2254 let predicates = normalize_with_depth_to(
2256 obligation.param_env,
2258 obligation.recursion_depth + 1,
2259 tcx.predicates_of(impl_fn_def_id).instantiate(tcx, impl_fn_substs),
2262 obligations.extend(std::iter::zip(predicates.predicates, predicates.spans).map(
2264 Obligation::with_depth(
2266 ObligationCause::new(
2267 obligation.cause.span,
2268 obligation.cause.body_id,
2269 if span.is_dummy() {
2270 super::ItemObligation(impl_fn_def_id)
2272 super::BindingObligation(impl_fn_def_id, span)
2275 obligation.recursion_depth + 1,
2276 obligation.param_env,
2282 let ty = normalize_with_depth_to(
2284 obligation.param_env,
2286 obligation.recursion_depth + 1,
2287 tcx.bound_trait_impl_trait_tys(impl_fn_def_id)
2289 tys.map_or_else(|_| tcx.ty_error(), |tys| tys[&obligation.predicate.def_id])
2291 .subst(tcx, impl_fn_substs),
2295 Progress { term: ty.into(), obligations }
2298 // Get obligations corresponding to the predicates from the where-clause of the
2299 // associated type itself.
2300 fn assoc_ty_own_obligations<'cx, 'tcx>(
2301 selcx: &mut SelectionContext<'cx, 'tcx>,
2302 obligation: &ProjectionTyObligation<'tcx>,
2303 nested: &mut Vec<PredicateObligation<'tcx>>,
2305 let tcx = selcx.tcx();
2307 .predicates_of(obligation.predicate.def_id)
2308 .instantiate_own(tcx, obligation.predicate.substs);
2309 for (predicate, span) in std::iter::zip(own.predicates, own.spans) {
2310 let normalized = normalize_with_depth_to(
2312 obligation.param_env,
2313 obligation.cause.clone(),
2314 obligation.recursion_depth + 1,
2319 let nested_cause = if matches!(
2320 obligation.cause.code(),
2321 super::CompareImplItemObligation { .. }
2322 | super::CheckAssociatedTypeBounds { .. }
2323 | super::AscribeUserTypeProvePredicate(..)
2325 obligation.cause.clone()
2326 } else if span.is_dummy() {
2327 ObligationCause::new(
2328 obligation.cause.span,
2329 obligation.cause.body_id,
2330 super::ItemObligation(obligation.predicate.def_id),
2333 ObligationCause::new(
2334 obligation.cause.span,
2335 obligation.cause.body_id,
2336 super::BindingObligation(obligation.predicate.def_id, span),
2339 nested.push(Obligation::with_depth(
2342 obligation.recursion_depth + 1,
2343 obligation.param_env,
2349 pub(crate) trait ProjectionCacheKeyExt<'cx, 'tcx>: Sized {
2350 fn from_poly_projection_predicate(
2351 selcx: &mut SelectionContext<'cx, 'tcx>,
2352 predicate: ty::PolyProjectionPredicate<'tcx>,
2356 impl<'cx, 'tcx> ProjectionCacheKeyExt<'cx, 'tcx> for ProjectionCacheKey<'tcx> {
2357 fn from_poly_projection_predicate(
2358 selcx: &mut SelectionContext<'cx, 'tcx>,
2359 predicate: ty::PolyProjectionPredicate<'tcx>,
2361 let infcx = selcx.infcx;
2362 // We don't do cross-snapshot caching of obligations with escaping regions,
2363 // so there's no cache key to use
2364 predicate.no_bound_vars().map(|predicate| {
2365 ProjectionCacheKey::new(
2366 // We don't attempt to match up with a specific type-variable state
2367 // from a specific call to `opt_normalize_projection_type` - if
2368 // there's no precise match, the original cache entry is "stranded"
2370 infcx.resolve_vars_if_possible(predicate.projection_ty),