]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_trait_selection/src/traits/project.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / compiler / rustc_trait_selection / src / traits / project.rs
CommitLineData
ba9703b0
XL
1//! Code for projecting associated types out of trait references.
2
ba9703b0
XL
3use super::specialization_graph;
4use super::translate_substs;
5use super::util;
6use super::MismatchedProjectionTypes;
7use super::Obligation;
8use super::ObligationCause;
9use super::PredicateObligation;
10use super::Selection;
11use super::SelectionContext;
12use super::SelectionError;
f9f354fc 13use super::{
f035d41b 14 ImplSourceClosureData, ImplSourceDiscriminantKindData, ImplSourceFnPointerData,
6a06907d 15 ImplSourceGeneratorData, ImplSourcePointeeData, ImplSourceUserDefinedData,
f9f354fc 16};
f035d41b 17use super::{Normalized, NormalizedTy, ProjectionCacheEntry, ProjectionCacheKey};
ba9703b0
XL
18
19use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
20use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
94222f64 21use crate::traits::error_reporting::InferCtxtExt as _;
5099ac24 22use crate::traits::select::ProjectionMatchesProjection;
3c0e092e 23use rustc_data_structures::sso::SsoHashSet;
f9f354fc 24use rustc_data_structures::stack::ensure_sufficient_stack;
ee023bcb 25use rustc_errors::ErrorGuaranteed;
5099ac24 26use rustc_hir::def::DefKind;
ba9703b0 27use rustc_hir::def_id::DefId;
3dfed10e 28use rustc_hir::lang_items::LangItem;
f035d41b 29use rustc_infer::infer::resolve::OpportunisticRegionResolver;
ee023bcb
FG
30use rustc_middle::traits::select::OverflowError;
31use rustc_middle::ty::fold::{MaxUniverse, TypeFoldable, TypeFolder};
f035d41b 32use rustc_middle::ty::subst::Subst;
5099ac24 33use rustc_middle::ty::{self, Term, ToPredicate, Ty, TyCtxt};
f035d41b 34use rustc_span::symbol::sym;
ba9703b0 35
136023e0
XL
36use std::collections::BTreeMap;
37
ba9703b0
XL
38pub use rustc_middle::traits::Reveal;
39
40pub type PolyProjectionObligation<'tcx> = Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
41
42pub type ProjectionObligation<'tcx> = Obligation<'tcx, ty::ProjectionPredicate<'tcx>>;
43
44pub type ProjectionTyObligation<'tcx> = Obligation<'tcx, ty::ProjectionTy<'tcx>>;
45
f9652781
XL
46pub(super) struct InProgress;
47
ba9703b0
XL
48/// When attempting to resolve `<T as TraitRef>::Name` ...
49#[derive(Debug)]
5099ac24 50pub enum ProjectionError<'tcx> {
ba9703b0
XL
51 /// ...we found multiple sources of information and couldn't resolve the ambiguity.
52 TooManyCandidates,
53
54 /// ...an error occurred matching `T : TraitRef`
55 TraitSelectionError(SelectionError<'tcx>),
56}
57
58#[derive(PartialEq, Eq, Debug)]
5099ac24 59enum ProjectionCandidate<'tcx> {
29967ef6 60 /// From a where-clause in the env or object type
ba9703b0
XL
61 ParamEnv(ty::PolyProjectionPredicate<'tcx>),
62
29967ef6 63 /// From the definition of `Trait` when you have something like <<A as Trait>::B as Trait2>::C
ba9703b0
XL
64 TraitDef(ty::PolyProjectionPredicate<'tcx>),
65
29967ef6
XL
66 /// Bounds specified on an object type
67 Object(ty::PolyProjectionPredicate<'tcx>),
68
94222f64 69 /// From an "impl" (or a "pseudo-impl" returned by select)
ba9703b0
XL
70 Select(Selection<'tcx>),
71}
72
5099ac24 73enum ProjectionCandidateSet<'tcx> {
ba9703b0 74 None,
5099ac24 75 Single(ProjectionCandidate<'tcx>),
ba9703b0
XL
76 Ambiguous,
77 Error(SelectionError<'tcx>),
78}
79
5099ac24 80impl<'tcx> ProjectionCandidateSet<'tcx> {
ba9703b0 81 fn mark_ambiguous(&mut self) {
5099ac24 82 *self = ProjectionCandidateSet::Ambiguous;
ba9703b0
XL
83 }
84
85 fn mark_error(&mut self, err: SelectionError<'tcx>) {
5099ac24 86 *self = ProjectionCandidateSet::Error(err);
ba9703b0
XL
87 }
88
89 // Returns true if the push was successful, or false if the candidate
90 // was discarded -- this could be because of ambiguity, or because
91 // a higher-priority candidate is already there.
5099ac24
FG
92 fn push_candidate(&mut self, candidate: ProjectionCandidate<'tcx>) -> bool {
93 use self::ProjectionCandidate::*;
94 use self::ProjectionCandidateSet::*;
ba9703b0
XL
95
96 // This wacky variable is just used to try and
97 // make code readable and avoid confusing paths.
98 // It is assigned a "value" of `()` only on those
99 // paths in which we wish to convert `*self` to
100 // ambiguous (and return false, because the candidate
101 // was not used). On other paths, it is not assigned,
102 // and hence if those paths *could* reach the code that
103 // comes after the match, this fn would not compile.
104 let convert_to_ambiguous;
105
106 match self {
107 None => {
108 *self = Single(candidate);
109 return true;
110 }
111
112 Single(current) => {
113 // Duplicates can happen inside ParamEnv. In the case, we
114 // perform a lazy deduplication.
115 if current == &candidate {
116 return false;
117 }
118
119 // Prefer where-clauses. As in select, if there are multiple
120 // candidates, we prefer where-clause candidates over impls. This
121 // may seem a bit surprising, since impls are the source of
122 // "truth" in some sense, but in fact some of the impls that SEEM
123 // applicable are not, because of nested obligations. Where
124 // clauses are the safer choice. See the comment on
125 // `select::SelectionCandidate` and #21974 for more details.
126 match (current, candidate) {
127 (ParamEnv(..), ParamEnv(..)) => convert_to_ambiguous = (),
128 (ParamEnv(..), _) => return false,
129 (_, ParamEnv(..)) => unreachable!(),
130 (_, _) => convert_to_ambiguous = (),
131 }
132 }
133
134 Ambiguous | Error(..) => {
135 return false;
136 }
137 }
138
139 // We only ever get here when we moved from a single candidate
140 // to ambiguous.
141 let () = convert_to_ambiguous;
142 *self = Ambiguous;
143 false
144 }
145}
146
ee023bcb
FG
147/// Takes the place of a
148/// Result<
149/// Result<Option<Vec<PredicateObligation<'tcx>>>, InProgress>,
150/// MismatchedProjectionTypes<'tcx>,
151/// >
152pub(super) enum ProjectAndUnifyResult<'tcx> {
153 Holds(Vec<PredicateObligation<'tcx>>),
154 FailedNormalization,
155 Recursive,
156 MismatchedProjectionTypes(MismatchedProjectionTypes<'tcx>),
157}
158
ba9703b0
XL
159/// Evaluates constraints of the form:
160///
161/// for<...> <T as Trait>::U == V
162///
163/// If successful, this may result in additional obligations. Also returns
164/// the projection cache key used to track these additional obligations.
f9652781
XL
165///
166/// ## Returns
167///
168/// - `Err(_)`: the projection can be normalized, but is not equal to the
169/// expected type.
170/// - `Ok(Err(InProgress))`: this is called recursively while normalizing
171/// the same projection.
172/// - `Ok(Ok(None))`: The projection cannot be normalized due to ambiguity
173/// (resolving some inference variables in the projection may fix this).
174/// - `Ok(Ok(Some(obligations)))`: The projection bound holds subject to
175/// the given obligations. If the projection cannot be normalized because
176/// the required trait bound doesn't hold this returned with `obligations`
177/// being a predicate that cannot be proven.
29967ef6 178#[instrument(level = "debug", skip(selcx))]
f9652781 179pub(super) fn poly_project_and_unify_type<'cx, 'tcx>(
ba9703b0
XL
180 selcx: &mut SelectionContext<'cx, 'tcx>,
181 obligation: &PolyProjectionObligation<'tcx>,
ee023bcb 182) -> ProjectAndUnifyResult<'tcx> {
ba9703b0 183 let infcx = selcx.infcx();
ee023bcb
FG
184 let r = infcx.commit_if_ok(|_snapshot| {
185 let old_universe = infcx.universe();
29967ef6 186 let placeholder_predicate =
fc512014 187 infcx.replace_bound_vars_with_placeholders(obligation.predicate);
ee023bcb 188 let new_universe = infcx.universe();
ba9703b0
XL
189
190 let placeholder_obligation = obligation.with(placeholder_predicate);
ee023bcb
FG
191 match project_and_unify_type(selcx, &placeholder_obligation) {
192 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => Err(e),
193 ProjectAndUnifyResult::Holds(obligations)
194 if old_universe != new_universe
195 && selcx.tcx().features().generic_associated_types_extended =>
196 {
197 // If the `generic_associated_types_extended` feature is active, then we ignore any
198 // obligations references lifetimes from any universe greater than or equal to the
199 // universe just created. Otherwise, we can end up with something like `for<'a> I: 'a`,
200 // which isn't quite what we want. Ideally, we want either an implied
201 // `for<'a where I: 'a> I: 'a` or we want to "lazily" check these hold when we
202 // substitute concrete regions. There is design work to be done here; until then,
203 // however, this allows experimenting potential GAT features without running into
204 // well-formedness issues.
205 let new_obligations = obligations
206 .into_iter()
207 .filter(|obligation| {
208 let mut visitor = MaxUniverse::new();
209 obligation.predicate.visit_with(&mut visitor);
210 visitor.max_universe() < new_universe
211 })
212 .collect();
213 Ok(ProjectAndUnifyResult::Holds(new_obligations))
214 }
215 other => Ok(other),
216 }
217 });
218
219 match r {
220 Ok(inner) => inner,
221 Err(err) => ProjectAndUnifyResult::MismatchedProjectionTypes(err),
222 }
ba9703b0
XL
223}
224
225/// Evaluates constraints of the form:
226///
227/// <T as Trait>::U == V
228///
229/// If successful, this may result in additional obligations.
f9652781
XL
230///
231/// See [poly_project_and_unify_type] for an explanation of the return value.
ee023bcb 232#[tracing::instrument(level = "debug", skip(selcx))]
ba9703b0
XL
233fn project_and_unify_type<'cx, 'tcx>(
234 selcx: &mut SelectionContext<'cx, 'tcx>,
235 obligation: &ProjectionObligation<'tcx>,
ee023bcb 236) -> ProjectAndUnifyResult<'tcx> {
ba9703b0 237 let mut obligations = vec![];
5099ac24
FG
238
239 let infcx = selcx.infcx();
240 let normalized = match opt_normalize_projection_type(
ba9703b0
XL
241 selcx,
242 obligation.param_env,
243 obligation.predicate.projection_ty,
244 obligation.cause.clone(),
245 obligation.recursion_depth,
246 &mut obligations,
247 ) {
f9652781 248 Ok(Some(n)) => n,
ee023bcb
FG
249 Ok(None) => return ProjectAndUnifyResult::FailedNormalization,
250 Err(InProgress) => return ProjectAndUnifyResult::Recursive,
ba9703b0 251 };
5099ac24 252 debug!(?normalized, ?obligations, "project_and_unify_type result");
ee023bcb
FG
253 let actual = obligation.predicate.term;
254 // HACK: lazy TAIT would regress src/test/ui/impl-trait/nested-return-type2.rs, so we add
255 // a back-compat hack hat converts the RPITs into inference vars, just like they were before
256 // lazy TAIT.
257 // This does not affect TAITs in general, as tested in the nested-return-type-tait* tests.
258 let InferOk { value: actual, obligations: new } =
259 selcx.infcx().replace_opaque_types_with_inference_vars(
260 actual,
261 obligation.cause.body_id,
262 obligation.cause.span,
263 obligation.param_env,
264 );
265 obligations.extend(new);
266
267 match infcx.at(&obligation.cause, obligation.param_env).eq(normalized, actual) {
ba9703b0
XL
268 Ok(InferOk { obligations: inferred_obligations, value: () }) => {
269 obligations.extend(inferred_obligations);
ee023bcb 270 ProjectAndUnifyResult::Holds(obligations)
ba9703b0
XL
271 }
272 Err(err) => {
ee023bcb
FG
273 debug!("equating types encountered error {:?}", err);
274 ProjectAndUnifyResult::MismatchedProjectionTypes(MismatchedProjectionTypes { err })
ba9703b0
XL
275 }
276 }
277}
278
279/// Normalizes any associated type projections in `value`, replacing
280/// them with a fully resolved type where possible. The return value
281/// combines the normalized result and any additional obligations that
282/// were incurred as result.
283pub fn normalize<'a, 'b, 'tcx, T>(
284 selcx: &'a mut SelectionContext<'b, 'tcx>,
285 param_env: ty::ParamEnv<'tcx>,
286 cause: ObligationCause<'tcx>,
fc512014 287 value: T,
ba9703b0
XL
288) -> Normalized<'tcx, T>
289where
290 T: TypeFoldable<'tcx>,
291{
292 let mut obligations = Vec::new();
293 let value = normalize_to(selcx, param_env, cause, value, &mut obligations);
294 Normalized { value, obligations }
295}
296
297pub fn normalize_to<'a, 'b, 'tcx, T>(
298 selcx: &'a mut SelectionContext<'b, 'tcx>,
299 param_env: ty::ParamEnv<'tcx>,
300 cause: ObligationCause<'tcx>,
fc512014 301 value: T,
ba9703b0
XL
302 obligations: &mut Vec<PredicateObligation<'tcx>>,
303) -> T
304where
305 T: TypeFoldable<'tcx>,
306{
307 normalize_with_depth_to(selcx, param_env, cause, 0, value, obligations)
308}
309
310/// As `normalize`, but with a custom depth.
311pub fn normalize_with_depth<'a, 'b, 'tcx, T>(
312 selcx: &'a mut SelectionContext<'b, 'tcx>,
313 param_env: ty::ParamEnv<'tcx>,
314 cause: ObligationCause<'tcx>,
315 depth: usize,
fc512014 316 value: T,
ba9703b0
XL
317) -> Normalized<'tcx, T>
318where
319 T: TypeFoldable<'tcx>,
320{
321 let mut obligations = Vec::new();
322 let value = normalize_with_depth_to(selcx, param_env, cause, depth, value, &mut obligations);
323 Normalized { value, obligations }
324}
325
136023e0 326#[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
ba9703b0
XL
327pub fn normalize_with_depth_to<'a, 'b, 'tcx, T>(
328 selcx: &'a mut SelectionContext<'b, 'tcx>,
329 param_env: ty::ParamEnv<'tcx>,
330 cause: ObligationCause<'tcx>,
331 depth: usize,
fc512014 332 value: T,
ba9703b0
XL
333 obligations: &mut Vec<PredicateObligation<'tcx>>,
334) -> T
335where
336 T: TypeFoldable<'tcx>,
337{
136023e0 338 debug!(obligations.len = obligations.len());
ba9703b0 339 let mut normalizer = AssocTypeNormalizer::new(selcx, param_env, cause, depth, obligations);
f9f354fc 340 let result = ensure_sufficient_stack(|| normalizer.fold(value));
29967ef6
XL
341 debug!(?result, obligations.len = normalizer.obligations.len());
342 debug!(?normalizer.obligations,);
ba9703b0
XL
343 result
344}
345
ee023bcb
FG
346#[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
347pub fn try_normalize_with_depth_to<'a, 'b, 'tcx, T>(
348 selcx: &'a mut SelectionContext<'b, 'tcx>,
349 param_env: ty::ParamEnv<'tcx>,
350 cause: ObligationCause<'tcx>,
351 depth: usize,
352 value: T,
353 obligations: &mut Vec<PredicateObligation<'tcx>>,
354) -> T
355where
356 T: TypeFoldable<'tcx>,
357{
358 debug!(obligations.len = obligations.len());
359 let mut normalizer = AssocTypeNormalizer::new_without_eager_inference_replacement(
360 selcx,
361 param_env,
362 cause,
363 depth,
364 obligations,
365 );
366 let result = ensure_sufficient_stack(|| normalizer.fold(value));
367 debug!(?result, obligations.len = normalizer.obligations.len());
368 debug!(?normalizer.obligations,);
369 result
370}
371
136023e0
XL
372pub(crate) fn needs_normalization<'tcx, T: TypeFoldable<'tcx>>(value: &T, reveal: Reveal) -> bool {
373 match reveal {
374 Reveal::UserFacing => value
375 .has_type_flags(ty::TypeFlags::HAS_TY_PROJECTION | ty::TypeFlags::HAS_CT_PROJECTION),
376 Reveal::All => value.has_type_flags(
377 ty::TypeFlags::HAS_TY_PROJECTION
378 | ty::TypeFlags::HAS_TY_OPAQUE
379 | ty::TypeFlags::HAS_CT_PROJECTION,
380 ),
381 }
382}
383
ba9703b0
XL
384struct AssocTypeNormalizer<'a, 'b, 'tcx> {
385 selcx: &'a mut SelectionContext<'b, 'tcx>,
386 param_env: ty::ParamEnv<'tcx>,
387 cause: ObligationCause<'tcx>,
388 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
389 depth: usize,
136023e0 390 universes: Vec<Option<ty::UniverseIndex>>,
ee023bcb
FG
391 /// If true, when a projection is unable to be completed, an inference
392 /// variable will be created and an obligation registered to project to that
393 /// inference variable. Also, constants will be eagerly evaluated.
394 eager_inference_replacement: bool,
ba9703b0
XL
395}
396
397impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
398 fn new(
399 selcx: &'a mut SelectionContext<'b, 'tcx>,
400 param_env: ty::ParamEnv<'tcx>,
401 cause: ObligationCause<'tcx>,
402 depth: usize,
403 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
404 ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
ee023bcb
FG
405 AssocTypeNormalizer {
406 selcx,
407 param_env,
408 cause,
409 obligations,
410 depth,
411 universes: vec![],
412 eager_inference_replacement: true,
413 }
414 }
415
416 fn new_without_eager_inference_replacement(
417 selcx: &'a mut SelectionContext<'b, 'tcx>,
418 param_env: ty::ParamEnv<'tcx>,
419 cause: ObligationCause<'tcx>,
420 depth: usize,
421 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
422 ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
423 AssocTypeNormalizer {
424 selcx,
425 param_env,
426 cause,
427 obligations,
428 depth,
429 universes: vec![],
430 eager_inference_replacement: false,
431 }
ba9703b0
XL
432 }
433
fc512014 434 fn fold<T: TypeFoldable<'tcx>>(&mut self, value: T) -> T {
ba9703b0 435 let value = self.selcx.infcx().resolve_vars_if_possible(value);
136023e0
XL
436 debug!(?value);
437
438 assert!(
439 !value.has_escaping_bound_vars(),
440 "Normalizing {:?} without wrapping in a `Binder`",
441 value
442 );
ba9703b0 443
136023e0
XL
444 if !needs_normalization(&value, self.param_env.reveal()) {
445 value
446 } else {
447 value.fold_with(self)
448 }
ba9703b0
XL
449 }
450}
451
452impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
453 fn tcx<'c>(&'c self) -> TyCtxt<'tcx> {
454 self.selcx.tcx()
455 }
456
136023e0
XL
457 fn fold_binder<T: TypeFoldable<'tcx>>(
458 &mut self,
459 t: ty::Binder<'tcx, T>,
460 ) -> ty::Binder<'tcx, T> {
461 self.universes.push(None);
462 let t = t.super_fold_with(self);
463 self.universes.pop();
464 t
465 }
466
ba9703b0 467 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
136023e0 468 if !needs_normalization(&ty, self.param_env.reveal()) {
ba9703b0
XL
469 return ty;
470 }
94222f64
XL
471
472 // We try to be a little clever here as a performance optimization in
473 // cases where there are nested projections under binders.
474 // For example:
475 // ```
476 // for<'a> fn(<T as Foo>::One<'a, Box<dyn Bar<'a, Item=<T as Foo>::Two<'a>>>>)
477 // ```
478 // We normalize the substs on the projection before the projecting, but
479 // if we're naive, we'll
480 // replace bound vars on inner, project inner, replace placeholders on inner,
481 // replace bound vars on outer, project outer, replace placeholders on outer
ba9703b0 482 //
94222f64
XL
483 // However, if we're a bit more clever, we can replace the bound vars
484 // on the entire type before normalizing nested projections, meaning we
485 // replace bound vars on outer, project inner,
486 // project outer, replace placeholders on outer
ba9703b0 487 //
94222f64
XL
488 // This is possible because the inner `'a` will already be a placeholder
489 // when we need to normalize the inner projection
ba9703b0 490 //
94222f64
XL
491 // On the other hand, this does add a bit of complexity, since we only
492 // replace bound vars if the current type is a `Projection` and we need
493 // to make sure we don't forget to fold the substs regardless.
ba9703b0 494
1b1a35ee 495 match *ty.kind() {
94222f64
XL
496 // This is really important. While we *can* handle this, this has
497 // severe performance implications for large opaque types with
498 // late-bound regions. See `issue-88862` benchmark.
1b1a35ee 499 ty::Opaque(def_id, substs) if !substs.has_escaping_bound_vars() => {
5099ac24 500 // Only normalize `impl Trait` outside of type inference, usually in codegen.
f035d41b 501 match self.param_env.reveal() {
94222f64 502 Reveal::UserFacing => ty.super_fold_with(self),
ba9703b0
XL
503
504 Reveal::All => {
136023e0 505 let recursion_limit = self.tcx().recursion_limit();
f9f354fc 506 if !recursion_limit.value_within_limit(self.depth) {
ba9703b0
XL
507 let obligation = Obligation::with_depth(
508 self.cause.clone(),
f9f354fc 509 recursion_limit.0,
ba9703b0
XL
510 self.param_env,
511 ty,
512 );
513 self.selcx.infcx().report_overflow_error(&obligation, true);
514 }
515
94222f64 516 let substs = substs.super_fold_with(self);
ba9703b0
XL
517 let generic_ty = self.tcx().type_of(def_id);
518 let concrete_ty = generic_ty.subst(self.tcx(), substs);
519 self.depth += 1;
520 let folded_ty = self.fold_ty(concrete_ty);
521 self.depth -= 1;
522 folded_ty
523 }
524 }
525 }
526
fc512014 527 ty::Projection(data) if !data.has_escaping_bound_vars() => {
94222f64
XL
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.
ba9703b0 533
94222f64 534 let data = data.super_fold_with(self);
ee023bcb
FG
535 let normalized_ty = if self.eager_inference_replacement {
536 normalize_projection_type(
537 self.selcx,
538 self.param_env,
539 data,
540 self.cause.clone(),
541 self.depth,
542 &mut self.obligations,
543 )
544 } else {
545 opt_normalize_projection_type(
546 self.selcx,
547 self.param_env,
548 data,
549 self.cause.clone(),
550 self.depth,
551 &mut self.obligations,
552 )
553 .ok()
554 .flatten()
555 .unwrap_or_else(|| ty::Term::Ty(ty.super_fold_with(self)))
556 };
ba9703b0 557 debug!(
29967ef6
XL
558 ?self.depth,
559 ?ty,
560 ?normalized_ty,
561 obligations.len = ?self.obligations.len(),
562 "AssocTypeNormalizer: normalized type"
ba9703b0 563 );
5099ac24 564 normalized_ty.ty().unwrap()
ba9703b0
XL
565 }
566
94222f64
XL
567 ty::Projection(data) => {
568 // If there are escaping bound vars, we temporarily replace the
569 // bound vars with placeholders. Note though, that in the case
570 // that we still can't project for whatever reason (e.g. self
571 // type isn't known enough), we *can't* register an obligation
572 // and return an inference variable (since then that obligation
573 // would have bound vars and that's a can of worms). Instead,
574 // we just give up and fall back to pretending like we never tried!
575 //
576 // Note: this isn't necessarily the final approach here; we may
577 // want to figure out how to register obligations with escaping vars
578 // or handle this some other way.
136023e0
XL
579
580 let infcx = self.selcx.infcx();
581 let (data, mapped_regions, mapped_types, mapped_consts) =
582 BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, data);
94222f64 583 let data = data.super_fold_with(self);
136023e0
XL
584 let normalized_ty = opt_normalize_projection_type(
585 self.selcx,
586 self.param_env,
587 data,
588 self.cause.clone(),
589 self.depth,
590 &mut self.obligations,
591 )
592 .ok()
593 .flatten()
5099ac24 594 .map(|term| term.ty().unwrap())
94222f64
XL
595 .map(|normalized_ty| {
596 PlaceholderReplacer::replace_placeholders(
597 infcx,
598 mapped_regions,
599 mapped_types,
600 mapped_consts,
601 &self.universes,
602 normalized_ty,
603 )
604 })
605 .unwrap_or_else(|| ty.super_fold_with(self));
606
136023e0
XL
607 debug!(
608 ?self.depth,
609 ?ty,
610 ?normalized_ty,
611 obligations.len = ?self.obligations.len(),
612 "AssocTypeNormalizer: normalized type"
613 );
614 normalized_ty
615 }
616
94222f64 617 _ => ty.super_fold_with(self),
ba9703b0
XL
618 }
619 }
620
5099ac24 621 fn fold_const(&mut self, constant: ty::Const<'tcx>) -> ty::Const<'tcx> {
ee023bcb 622 if self.selcx.tcx().lazy_normalization() || !self.eager_inference_replacement {
f9f354fc
XL
623 constant
624 } else {
625 let constant = constant.super_fold_with(self);
626 constant.eval(self.selcx.tcx(), self.param_env)
627 }
ba9703b0
XL
628 }
629}
630
136023e0
XL
631pub struct BoundVarReplacer<'me, 'tcx> {
632 infcx: &'me InferCtxt<'me, 'tcx>,
633 // These three maps track the bound variable that were replaced by placeholders. It might be
634 // nice to remove these since we already have the `kind` in the placeholder; we really just need
635 // the `var` (but we *could* bring that into scope if we were to track them as we pass them).
636 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
637 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
638 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
639 // The current depth relative to *this* folding, *not* the entire normalization. In other words,
640 // the depth of binders we've passed here.
641 current_index: ty::DebruijnIndex,
642 // The `UniverseIndex` of the binding levels above us. These are optional, since we are lazy:
643 // we don't actually create a universe until we see a bound var we have to replace.
644 universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
645}
646
647impl<'me, 'tcx> BoundVarReplacer<'me, 'tcx> {
648 /// Returns `Some` if we *were* able to replace bound vars. If there are any bound vars that
649 /// use a binding level above `universe_indices.len()`, we fail.
650 pub fn replace_bound_vars<T: TypeFoldable<'tcx>>(
651 infcx: &'me InferCtxt<'me, 'tcx>,
652 universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
653 value: T,
654 ) -> (
655 T,
656 BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
657 BTreeMap<ty::PlaceholderType, ty::BoundTy>,
658 BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
659 ) {
660 let mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion> = BTreeMap::new();
661 let mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy> = BTreeMap::new();
662 let mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar> = BTreeMap::new();
663
664 let mut replacer = BoundVarReplacer {
665 infcx,
666 mapped_regions,
667 mapped_types,
668 mapped_consts,
669 current_index: ty::INNERMOST,
670 universe_indices,
671 };
672
673 let value = value.super_fold_with(&mut replacer);
674
675 (value, replacer.mapped_regions, replacer.mapped_types, replacer.mapped_consts)
676 }
677
678 fn universe_for(&mut self, debruijn: ty::DebruijnIndex) -> ty::UniverseIndex {
679 let infcx = self.infcx;
680 let index =
c295e0f8 681 self.universe_indices.len() + self.current_index.as_usize() - debruijn.as_usize() - 1;
136023e0
XL
682 let universe = self.universe_indices[index].unwrap_or_else(|| {
683 for i in self.universe_indices.iter_mut().take(index + 1) {
684 *i = i.or_else(|| Some(infcx.create_next_universe()))
685 }
686 self.universe_indices[index].unwrap()
687 });
688 universe
689 }
690}
691
a2a8927a 692impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
136023e0
XL
693 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
694 self.infcx.tcx
695 }
696
697 fn fold_binder<T: TypeFoldable<'tcx>>(
698 &mut self,
699 t: ty::Binder<'tcx, T>,
700 ) -> ty::Binder<'tcx, T> {
701 self.current_index.shift_in(1);
702 let t = t.super_fold_with(self);
703 self.current_index.shift_out(1);
704 t
705 }
706
707 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
708 match *r {
709 ty::ReLateBound(debruijn, _)
710 if debruijn.as_usize() + 1
711 > self.current_index.as_usize() + self.universe_indices.len() =>
712 {
713 bug!("Bound vars outside of `self.universe_indices`");
714 }
715 ty::ReLateBound(debruijn, br) if debruijn >= self.current_index => {
716 let universe = self.universe_for(debruijn);
717 let p = ty::PlaceholderRegion { universe, name: br.kind };
c295e0f8 718 self.mapped_regions.insert(p, br);
136023e0
XL
719 self.infcx.tcx.mk_region(ty::RePlaceholder(p))
720 }
721 _ => r,
722 }
723 }
724
725 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
726 match *t.kind() {
727 ty::Bound(debruijn, _)
728 if debruijn.as_usize() + 1
729 > self.current_index.as_usize() + self.universe_indices.len() =>
730 {
731 bug!("Bound vars outside of `self.universe_indices`");
732 }
733 ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
734 let universe = self.universe_for(debruijn);
735 let p = ty::PlaceholderType { universe, name: bound_ty.var };
c295e0f8 736 self.mapped_types.insert(p, bound_ty);
136023e0
XL
737 self.infcx.tcx.mk_ty(ty::Placeholder(p))
738 }
739 _ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
740 _ => t,
741 }
742 }
743
5099ac24
FG
744 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
745 match ct.val() {
746 ty::ConstKind::Bound(debruijn, _)
136023e0
XL
747 if debruijn.as_usize() + 1
748 > self.current_index.as_usize() + self.universe_indices.len() =>
749 {
750 bug!("Bound vars outside of `self.universe_indices`");
751 }
5099ac24 752 ty::ConstKind::Bound(debruijn, bound_const) if debruijn >= self.current_index => {
136023e0
XL
753 let universe = self.universe_for(debruijn);
754 let p = ty::PlaceholderConst {
755 universe,
5099ac24 756 name: ty::BoundConst { var: bound_const, ty: ct.ty() },
136023e0 757 };
c295e0f8 758 self.mapped_consts.insert(p, bound_const);
5099ac24
FG
759 self.infcx
760 .tcx
761 .mk_const(ty::ConstS { val: ty::ConstKind::Placeholder(p), ty: ct.ty() })
136023e0
XL
762 }
763 _ if ct.has_vars_bound_at_or_above(self.current_index) => ct.super_fold_with(self),
764 _ => ct,
765 }
766 }
767}
768
769// The inverse of `BoundVarReplacer`: replaces placeholders with the bound vars from which they came.
770pub struct PlaceholderReplacer<'me, 'tcx> {
771 infcx: &'me InferCtxt<'me, 'tcx>,
772 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
773 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
774 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
775 universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
776 current_index: ty::DebruijnIndex,
777}
778
779impl<'me, 'tcx> PlaceholderReplacer<'me, 'tcx> {
780 pub fn replace_placeholders<T: TypeFoldable<'tcx>>(
781 infcx: &'me InferCtxt<'me, 'tcx>,
782 mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
783 mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
784 mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
785 universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
786 value: T,
787 ) -> T {
788 let mut replacer = PlaceholderReplacer {
789 infcx,
790 mapped_regions,
791 mapped_types,
792 mapped_consts,
793 universe_indices,
794 current_index: ty::INNERMOST,
795 };
796 value.super_fold_with(&mut replacer)
797 }
798}
799
a2a8927a 800impl<'tcx> TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> {
136023e0
XL
801 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
802 self.infcx.tcx
803 }
804
805 fn fold_binder<T: TypeFoldable<'tcx>>(
806 &mut self,
807 t: ty::Binder<'tcx, T>,
808 ) -> ty::Binder<'tcx, T> {
809 if !t.has_placeholders() && !t.has_infer_regions() {
810 return t;
811 }
812 self.current_index.shift_in(1);
813 let t = t.super_fold_with(self);
814 self.current_index.shift_out(1);
815 t
816 }
817
818 fn fold_region(&mut self, r0: ty::Region<'tcx>) -> ty::Region<'tcx> {
5099ac24 819 let r1 = match *r0 {
136023e0
XL
820 ty::ReVar(_) => self
821 .infcx
822 .inner
823 .borrow_mut()
824 .unwrap_region_constraints()
825 .opportunistic_resolve_region(self.infcx.tcx, r0),
826 _ => r0,
827 };
828
829 let r2 = match *r1 {
830 ty::RePlaceholder(p) => {
831 let replace_var = self.mapped_regions.get(&p);
832 match replace_var {
833 Some(replace_var) => {
834 let index = self
835 .universe_indices
836 .iter()
837 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
838 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
839 let db = ty::DebruijnIndex::from_usize(
840 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
841 );
842 self.tcx().mk_region(ty::ReLateBound(db, *replace_var))
843 }
844 None => r1,
845 }
846 }
847 _ => r1,
848 };
849
850 debug!(?r0, ?r1, ?r2, "fold_region");
851
852 r2
853 }
854
855 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
856 match *ty.kind() {
857 ty::Placeholder(p) => {
858 let replace_var = self.mapped_types.get(&p);
859 match replace_var {
860 Some(replace_var) => {
861 let index = self
862 .universe_indices
863 .iter()
864 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
865 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
866 let db = ty::DebruijnIndex::from_usize(
867 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
868 );
869 self.tcx().mk_ty(ty::Bound(db, *replace_var))
870 }
871 None => ty,
872 }
873 }
874
875 _ if ty.has_placeholders() || ty.has_infer_regions() => ty.super_fold_with(self),
876 _ => ty,
877 }
878 }
879
5099ac24
FG
880 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
881 if let ty::ConstKind::Placeholder(p) = ct.val() {
136023e0
XL
882 let replace_var = self.mapped_consts.get(&p);
883 match replace_var {
884 Some(replace_var) => {
885 let index = self
886 .universe_indices
887 .iter()
888 .position(|u| matches!(u, Some(pu) if *pu == p.universe))
889 .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
890 let db = ty::DebruijnIndex::from_usize(
891 self.universe_indices.len() - index + self.current_index.as_usize() - 1,
892 );
5099ac24
FG
893 self.tcx().mk_const(ty::ConstS {
894 val: ty::ConstKind::Bound(db, *replace_var),
895 ty: ct.ty(),
896 })
136023e0
XL
897 }
898 None => ct,
899 }
900 } else {
901 ct.super_fold_with(self)
902 }
903 }
904}
905
ba9703b0
XL
906/// The guts of `normalize`: normalize a specific projection like `<T
907/// as Trait>::Item`. The result is always a type (and possibly
908/// additional obligations). If ambiguity arises, which implies that
909/// there are unresolved type variables in the projection, we will
910/// substitute a fresh type variable `$X` and generate a new
911/// obligation `<T as Trait>::Item == $X` for later.
912pub fn normalize_projection_type<'a, 'b, 'tcx>(
913 selcx: &'a mut SelectionContext<'b, 'tcx>,
914 param_env: ty::ParamEnv<'tcx>,
915 projection_ty: ty::ProjectionTy<'tcx>,
916 cause: ObligationCause<'tcx>,
917 depth: usize,
918 obligations: &mut Vec<PredicateObligation<'tcx>>,
5099ac24 919) -> Term<'tcx> {
ba9703b0
XL
920 opt_normalize_projection_type(
921 selcx,
922 param_env,
923 projection_ty,
924 cause.clone(),
925 depth,
926 obligations,
927 )
f9652781
XL
928 .ok()
929 .flatten()
ba9703b0
XL
930 .unwrap_or_else(move || {
931 // if we bottom out in ambiguity, create a type variable
932 // and a deferred predicate to resolve this when more type
933 // information is available.
934
5099ac24
FG
935 selcx
936 .infcx()
937 .infer_projection(param_env, projection_ty, cause, depth + 1, obligations)
938 .into()
ba9703b0
XL
939 })
940}
941
942/// The guts of `normalize`: normalize a specific projection like `<T
943/// as Trait>::Item`. The result is always a type (and possibly
944/// additional obligations). Returns `None` in the case of ambiguity,
945/// which indicates that there are unbound type variables.
946///
947/// This function used to return `Option<NormalizedTy<'tcx>>`, which contains a
948/// `Ty<'tcx>` and an obligations vector. But that obligation vector was very
949/// often immediately appended to another obligations vector. So now this
950/// function takes an obligations vector and appends to it directly, which is
951/// slightly uglier but avoids the need for an extra short-lived allocation.
29967ef6 952#[instrument(level = "debug", skip(selcx, param_env, cause, obligations))]
ba9703b0
XL
953fn opt_normalize_projection_type<'a, 'b, 'tcx>(
954 selcx: &'a mut SelectionContext<'b, 'tcx>,
955 param_env: ty::ParamEnv<'tcx>,
956 projection_ty: ty::ProjectionTy<'tcx>,
957 cause: ObligationCause<'tcx>,
958 depth: usize,
959 obligations: &mut Vec<PredicateObligation<'tcx>>,
5099ac24 960) -> Result<Option<Term<'tcx>>, InProgress> {
ba9703b0 961 let infcx = selcx.infcx();
94222f64
XL
962 // Don't use the projection cache in intercrate mode -
963 // the `infcx` may be re-used between intercrate in non-intercrate
964 // mode, which could lead to using incorrect cache results.
965 let use_cache = !selcx.is_intercrate();
ba9703b0 966
fc512014 967 let projection_ty = infcx.resolve_vars_if_possible(projection_ty);
ba9703b0
XL
968 let cache_key = ProjectionCacheKey::new(projection_ty);
969
ba9703b0
XL
970 // FIXME(#20304) For now, I am caching here, which is good, but it
971 // means we don't capture the type variables that are created in
972 // the case of ambiguity. Which means we may create a large stream
973 // of such variables. OTOH, if we move the caching up a level, we
974 // would not benefit from caching when proving `T: Trait<U=Foo>`
975 // bounds. It might be the case that we want two distinct caches,
976 // or else another kind of cache entry.
977
94222f64
XL
978 let cache_result = if use_cache {
979 infcx.inner.borrow_mut().projection_cache().try_start(cache_key)
980 } else {
981 Ok(())
982 };
ba9703b0 983 match cache_result {
136023e0 984 Ok(()) => debug!("no cache"),
ba9703b0
XL
985 Err(ProjectionCacheEntry::Ambiguous) => {
986 // If we found ambiguity the last time, that means we will continue
987 // to do so until some type in the key changes (and we know it
988 // hasn't, because we just fully resolved it).
29967ef6 989 debug!("found cache entry: ambiguous");
f9652781 990 return Ok(None);
ba9703b0
XL
991 }
992 Err(ProjectionCacheEntry::InProgress) => {
ba9703b0
XL
993 // Under lazy normalization, this can arise when
994 // bootstrapping. That is, imagine an environment with a
995 // where-clause like `A::B == u32`. Now, if we are asked
996 // to normalize `A::B`, we will want to check the
997 // where-clauses in scope. So we will try to unify `A::B`
998 // with `A::B`, which can trigger a recursive
f9652781 999 // normalization.
ba9703b0 1000
29967ef6 1001 debug!("found cache entry: in-progress");
ba9703b0 1002
b9856134
XL
1003 // Cache that normalizing this projection resulted in a cycle. This
1004 // should ensure that, unless this happens within a snapshot that's
1005 // rolled back, fulfillment or evaluation will notice the cycle.
1006
94222f64
XL
1007 if use_cache {
1008 infcx.inner.borrow_mut().projection_cache().recur(cache_key);
1009 }
b9856134
XL
1010 return Err(InProgress);
1011 }
1012 Err(ProjectionCacheEntry::Recur) => {
136023e0 1013 debug!("recur cache");
f9652781 1014 return Err(InProgress);
ba9703b0 1015 }
a2a8927a 1016 Err(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => {
ba9703b0
XL
1017 // This is the hottest path in this function.
1018 //
1019 // If we find the value in the cache, then return it along
1020 // with the obligations that went along with it. Note
1021 // that, when using a fulfillment context, these
1022 // obligations could in principle be ignored: they have
1023 // already been registered when the cache entry was
1024 // created (and hence the new ones will quickly be
1025 // discarded as duplicated). But when doing trait
1026 // evaluation this is not the case, and dropping the trait
1027 // evaluations can causes ICEs (e.g., #43132).
29967ef6 1028 debug!(?ty, "found normalized ty");
17df50a5 1029 obligations.extend(ty.obligations);
f9652781 1030 return Ok(Some(ty.value));
ba9703b0
XL
1031 }
1032 Err(ProjectionCacheEntry::Error) => {
29967ef6 1033 debug!("opt_normalize_projection_type: found error");
ba9703b0
XL
1034 let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
1035 obligations.extend(result.obligations);
5099ac24 1036 return Ok(Some(result.value.into()));
ba9703b0
XL
1037 }
1038 }
1039
1040 let obligation = Obligation::with_depth(cause.clone(), depth, param_env, projection_ty);
94222f64 1041
5099ac24
FG
1042 match project(selcx, &obligation) {
1043 Ok(Projected::Progress(Progress {
1044 term: projected_term,
ba9703b0
XL
1045 obligations: mut projected_obligations,
1046 })) => {
1047 // if projection succeeded, then what we get out of this
1048 // is also non-normalized (consider: it was derived from
1049 // an impl, where-clause etc) and hence we must
1050 // re-normalize it
1051
5099ac24 1052 let projected_term = selcx.infcx().resolve_vars_if_possible(projected_term);
ba9703b0 1053
5099ac24 1054 let mut result = if projected_term.has_projections() {
ba9703b0
XL
1055 let mut normalizer = AssocTypeNormalizer::new(
1056 selcx,
1057 param_env,
1058 cause,
1059 depth + 1,
1060 &mut projected_obligations,
1061 );
5099ac24 1062 let normalized_ty = normalizer.fold(projected_term);
ba9703b0
XL
1063
1064 Normalized { value: normalized_ty, obligations: projected_obligations }
1065 } else {
5099ac24 1066 Normalized { value: projected_term, obligations: projected_obligations }
ba9703b0
XL
1067 };
1068
3c0e092e 1069 let mut deduped: SsoHashSet<_> = Default::default();
94222f64 1070 result.obligations.drain_filter(|projected_obligation| {
3c0e092e
XL
1071 if !deduped.insert(projected_obligation.clone()) {
1072 return true;
1073 }
a2a8927a 1074 false
94222f64
XL
1075 });
1076
1077 if use_cache {
5099ac24 1078 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
94222f64 1079 }
ba9703b0 1080 obligations.extend(result.obligations);
f9652781 1081 Ok(Some(result.value))
ba9703b0 1082 }
5099ac24 1083 Ok(Projected::NoProgress(projected_ty)) => {
ba9703b0 1084 let result = Normalized { value: projected_ty, obligations: vec![] };
94222f64 1085 if use_cache {
5099ac24 1086 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
94222f64 1087 }
ba9703b0 1088 // No need to extend `obligations`.
f9652781 1089 Ok(Some(result.value))
ba9703b0 1090 }
5099ac24 1091 Err(ProjectionError::TooManyCandidates) => {
29967ef6 1092 debug!("opt_normalize_projection_type: too many candidates");
94222f64
XL
1093 if use_cache {
1094 infcx.inner.borrow_mut().projection_cache().ambiguous(cache_key);
1095 }
f9652781 1096 Ok(None)
ba9703b0 1097 }
5099ac24 1098 Err(ProjectionError::TraitSelectionError(_)) => {
ba9703b0
XL
1099 debug!("opt_normalize_projection_type: ERROR");
1100 // if we got an error processing the `T as Trait` part,
1101 // just return `ty::err` but add the obligation `T :
1102 // Trait`, which when processed will cause the error to be
1103 // reported later
1104
94222f64
XL
1105 if use_cache {
1106 infcx.inner.borrow_mut().projection_cache().error(cache_key);
1107 }
ba9703b0
XL
1108 let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
1109 obligations.extend(result.obligations);
5099ac24 1110 Ok(Some(result.value.into()))
ba9703b0
XL
1111 }
1112 }
1113}
1114
ba9703b0
XL
1115/// If we are projecting `<T as Trait>::Item`, but `T: Trait` does not
1116/// hold. In various error cases, we cannot generate a valid
1117/// normalized projection. Therefore, we create an inference variable
1118/// return an associated obligation that, when fulfilled, will lead to
1119/// an error.
1120///
1121/// Note that we used to return `Error` here, but that was quite
1122/// dubious -- the premise was that an error would *eventually* be
1123/// reported, when the obligation was processed. But in general once
94222f64 1124/// you see an `Error` you are supposed to be able to assume that an
ba9703b0
XL
1125/// error *has been* reported, so that you can take whatever heuristic
1126/// paths you want to take. To make things worse, it was possible for
1127/// cycles to arise, where you basically had a setup like `<MyType<$0>
1128/// as Trait>::Foo == $0`. Here, normalizing `<MyType<$0> as
1129/// Trait>::Foo> to `[type error]` would lead to an obligation of
1130/// `<MyType<[type error]> as Trait>::Foo`. We are supposed to report
1131/// an error for this obligation, but we legitimately should not,
1132/// because it contains `[type error]`. Yuck! (See issue #29857 for
1133/// one case where this arose.)
1134fn normalize_to_error<'a, 'tcx>(
1135 selcx: &mut SelectionContext<'a, 'tcx>,
1136 param_env: ty::ParamEnv<'tcx>,
1137 projection_ty: ty::ProjectionTy<'tcx>,
1138 cause: ObligationCause<'tcx>,
1139 depth: usize,
1140) -> NormalizedTy<'tcx> {
c295e0f8 1141 let trait_ref = ty::Binder::dummy(projection_ty.trait_ref(selcx.tcx()));
ba9703b0
XL
1142 let trait_obligation = Obligation {
1143 cause,
1144 recursion_depth: depth,
1145 param_env,
f9f354fc 1146 predicate: trait_ref.without_const().to_predicate(selcx.tcx()),
ba9703b0
XL
1147 };
1148 let tcx = selcx.infcx().tcx;
1149 let def_id = projection_ty.item_def_id;
1150 let new_value = selcx.infcx().next_ty_var(TypeVariableOrigin {
1151 kind: TypeVariableOriginKind::NormalizeProjectionType,
1152 span: tcx.def_span(def_id),
1153 });
1154 Normalized { value: new_value, obligations: vec![trait_obligation] }
1155}
1156
5099ac24 1157enum Projected<'tcx> {
ba9703b0 1158 Progress(Progress<'tcx>),
5099ac24 1159 NoProgress(ty::Term<'tcx>),
ba9703b0
XL
1160}
1161
1162struct Progress<'tcx> {
5099ac24 1163 term: ty::Term<'tcx>,
ba9703b0
XL
1164 obligations: Vec<PredicateObligation<'tcx>>,
1165}
1166
1167impl<'tcx> Progress<'tcx> {
1168 fn error(tcx: TyCtxt<'tcx>) -> Self {
5099ac24 1169 Progress { term: tcx.ty_error().into(), obligations: vec![] }
ba9703b0
XL
1170 }
1171
1172 fn with_addl_obligations(mut self, mut obligations: Vec<PredicateObligation<'tcx>>) -> Self {
ba9703b0
XL
1173 self.obligations.append(&mut obligations);
1174 self
1175 }
1176}
1177
1178/// Computes the result of a projection type (if we can).
1179///
1180/// IMPORTANT:
1181/// - `obligation` must be fully normalized
136023e0 1182#[tracing::instrument(level = "info", skip(selcx))]
5099ac24 1183fn project<'cx, 'tcx>(
ba9703b0
XL
1184 selcx: &mut SelectionContext<'cx, 'tcx>,
1185 obligation: &ProjectionTyObligation<'tcx>,
5099ac24 1186) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
136023e0 1187 if !selcx.tcx().recursion_limit().value_within_limit(obligation.recursion_depth) {
fc512014
XL
1188 // This should really be an immediate error, but some existing code
1189 // relies on being able to recover from this.
ee023bcb
FG
1190 return Err(ProjectionError::TraitSelectionError(SelectionError::Overflow(
1191 OverflowError::Canonical,
1192 )));
ba9703b0
XL
1193 }
1194
6a06907d 1195 if obligation.predicate.references_error() {
5099ac24 1196 return Ok(Projected::Progress(Progress::error(selcx.tcx())));
ba9703b0
XL
1197 }
1198
5099ac24 1199 let mut candidates = ProjectionCandidateSet::None;
ba9703b0
XL
1200
1201 // Make sure that the following procedures are kept in order. ParamEnv
1202 // needs to be first because it has highest priority, and Select checks
1203 // the return value of push_candidate which assumes it's ran at last.
6a06907d 1204 assemble_candidates_from_param_env(selcx, obligation, &mut candidates);
ba9703b0 1205
6a06907d 1206 assemble_candidates_from_trait_def(selcx, obligation, &mut candidates);
ba9703b0 1207
6a06907d 1208 assemble_candidates_from_object_ty(selcx, obligation, &mut candidates);
29967ef6 1209
5099ac24 1210 if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(_)) = candidates {
29967ef6
XL
1211 // Avoid normalization cycle from selection (see
1212 // `assemble_candidates_from_object_ty`).
1213 // FIXME(lazy_normalization): Lazy normalization should save us from
6a06907d 1214 // having to special case this.
29967ef6 1215 } else {
6a06907d 1216 assemble_candidates_from_impls(selcx, obligation, &mut candidates);
29967ef6 1217 };
ba9703b0
XL
1218
1219 match candidates {
5099ac24
FG
1220 ProjectionCandidateSet::Single(candidate) => {
1221 Ok(Projected::Progress(confirm_candidate(selcx, obligation, candidate)))
29967ef6 1222 }
5099ac24
FG
1223 ProjectionCandidateSet::None => Ok(Projected::NoProgress(
1224 // FIXME(associated_const_generics): this may need to change in the future?
1225 // need to investigate whether or not this is fine.
ba9703b0
XL
1226 selcx
1227 .tcx()
5099ac24
FG
1228 .mk_projection(obligation.predicate.item_def_id, obligation.predicate.substs)
1229 .into(),
ba9703b0
XL
1230 )),
1231 // Error occurred while trying to processing impls.
5099ac24 1232 ProjectionCandidateSet::Error(e) => Err(ProjectionError::TraitSelectionError(e)),
ba9703b0
XL
1233 // Inherent ambiguity that prevents us from even enumerating the
1234 // candidates.
5099ac24 1235 ProjectionCandidateSet::Ambiguous => Err(ProjectionError::TooManyCandidates),
ba9703b0
XL
1236 }
1237}
1238
1239/// The first thing we have to do is scan through the parameter
1240/// environment to see whether there are any projection predicates
1241/// there that can answer this question.
1242fn assemble_candidates_from_param_env<'cx, 'tcx>(
1243 selcx: &mut SelectionContext<'cx, 'tcx>,
1244 obligation: &ProjectionTyObligation<'tcx>,
5099ac24 1245 candidate_set: &mut ProjectionCandidateSet<'tcx>,
ba9703b0 1246) {
ba9703b0
XL
1247 assemble_candidates_from_predicates(
1248 selcx,
1249 obligation,
ba9703b0 1250 candidate_set,
5099ac24 1251 ProjectionCandidate::ParamEnv,
f035d41b 1252 obligation.param_env.caller_bounds().iter(),
29967ef6 1253 false,
ba9703b0
XL
1254 );
1255}
1256
1257/// In the case of a nested projection like <<A as Foo>::FooT as Bar>::BarT, we may find
1258/// that the definition of `Foo` has some clues:
1259///
1260/// ```
1261/// trait Foo {
1262/// type FooT : Bar<BarT=i32>
1263/// }
1264/// ```
1265///
1266/// Here, for example, we could conclude that the result is `i32`.
1267fn assemble_candidates_from_trait_def<'cx, 'tcx>(
1268 selcx: &mut SelectionContext<'cx, 'tcx>,
1269 obligation: &ProjectionTyObligation<'tcx>,
5099ac24 1270 candidate_set: &mut ProjectionCandidateSet<'tcx>,
ba9703b0
XL
1271) {
1272 debug!("assemble_candidates_from_trait_def(..)");
1273
1274 let tcx = selcx.tcx();
1275 // Check whether the self-type is itself a projection.
f035d41b 1276 // If so, extract what we know from the trait and try to come up with a good answer.
6a06907d 1277 let bounds = match *obligation.predicate.self_ty().kind() {
29967ef6
XL
1278 ty::Projection(ref data) => tcx.item_bounds(data.item_def_id).subst(tcx, data.substs),
1279 ty::Opaque(def_id, substs) => tcx.item_bounds(def_id).subst(tcx, substs),
ba9703b0
XL
1280 ty::Infer(ty::TyVar(_)) => {
1281 // If the self-type is an inference variable, then it MAY wind up
1282 // being a projected type, so induce an ambiguity.
1283 candidate_set.mark_ambiguous();
1284 return;
1285 }
1286 _ => return,
1287 };
1288
ba9703b0
XL
1289 assemble_candidates_from_predicates(
1290 selcx,
1291 obligation,
ba9703b0 1292 candidate_set,
5099ac24 1293 ProjectionCandidate::TraitDef,
f035d41b 1294 bounds.iter(),
29967ef6 1295 true,
5099ac24 1296 );
ba9703b0
XL
1297}
1298
29967ef6
XL
1299/// In the case of a trait object like
1300/// `<dyn Iterator<Item = ()> as Iterator>::Item` we can use the existential
1301/// predicate in the trait object.
1302///
1303/// We don't go through the select candidate for these bounds to avoid cycles:
1304/// In the above case, `dyn Iterator<Item = ()>: Iterator` would create a
1305/// nested obligation of `<dyn Iterator<Item = ()> as Iterator>::Item: Sized`,
1306/// this then has to be normalized without having to prove
1307/// `dyn Iterator<Item = ()>: Iterator` again.
1308fn assemble_candidates_from_object_ty<'cx, 'tcx>(
1309 selcx: &mut SelectionContext<'cx, 'tcx>,
1310 obligation: &ProjectionTyObligation<'tcx>,
5099ac24 1311 candidate_set: &mut ProjectionCandidateSet<'tcx>,
29967ef6
XL
1312) {
1313 debug!("assemble_candidates_from_object_ty(..)");
1314
1315 let tcx = selcx.tcx();
1316
6a06907d 1317 let self_ty = obligation.predicate.self_ty();
29967ef6
XL
1318 let object_ty = selcx.infcx().shallow_resolve(self_ty);
1319 let data = match object_ty.kind() {
1320 ty::Dynamic(data, ..) => data,
1321 ty::Infer(ty::TyVar(_)) => {
1322 // If the self-type is an inference variable, then it MAY wind up
1323 // being an object type, so induce an ambiguity.
1324 candidate_set.mark_ambiguous();
1325 return;
1326 }
1327 _ => return,
1328 };
1329 let env_predicates = data
1330 .projection_bounds()
1331 .filter(|bound| bound.item_def_id() == obligation.predicate.item_def_id)
1332 .map(|p| p.with_self_ty(tcx, object_ty).to_predicate(tcx));
1333
1334 assemble_candidates_from_predicates(
1335 selcx,
1336 obligation,
29967ef6 1337 candidate_set,
5099ac24 1338 ProjectionCandidate::Object,
29967ef6
XL
1339 env_predicates,
1340 false,
1341 );
1342}
1343
5099ac24
FG
1344#[tracing::instrument(
1345 level = "debug",
1346 skip(selcx, candidate_set, ctor, env_predicates, potentially_unnormalized_candidates)
1347)]
ba9703b0
XL
1348fn assemble_candidates_from_predicates<'cx, 'tcx>(
1349 selcx: &mut SelectionContext<'cx, 'tcx>,
1350 obligation: &ProjectionTyObligation<'tcx>,
5099ac24
FG
1351 candidate_set: &mut ProjectionCandidateSet<'tcx>,
1352 ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionCandidate<'tcx>,
ba9703b0 1353 env_predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
29967ef6 1354 potentially_unnormalized_candidates: bool,
ba9703b0 1355) {
ba9703b0
XL
1356 let infcx = selcx.infcx();
1357 for predicate in env_predicates {
5869c6ff
XL
1358 let bound_predicate = predicate.kind();
1359 if let ty::PredicateKind::Projection(data) = predicate.kind().skip_binder() {
29967ef6 1360 let data = bound_predicate.rebind(data);
5099ac24
FG
1361 if data.projection_def_id() != obligation.predicate.item_def_id {
1362 continue;
1363 }
ba9703b0 1364
5099ac24
FG
1365 let is_match = infcx.probe(|_| {
1366 selcx.match_projection_projections(
1367 obligation,
1368 data,
1369 potentially_unnormalized_candidates,
1370 )
1371 });
29967ef6 1372
5099ac24
FG
1373 match is_match {
1374 ProjectionMatchesProjection::Yes => {
1375 candidate_set.push_candidate(ctor(data));
1376
1377 if potentially_unnormalized_candidates
1378 && !obligation.predicate.has_infer_types_or_consts()
1379 {
1380 // HACK: Pick the first trait def candidate for a fully
1381 // inferred predicate. This is to allow duplicates that
1382 // differ only in normalization.
1383 return;
1384 }
1385 }
1386 ProjectionMatchesProjection::Ambiguous => {
1387 candidate_set.mark_ambiguous();
29967ef6 1388 }
5099ac24 1389 ProjectionMatchesProjection::No => {}
ba9703b0
XL
1390 }
1391 }
1392 }
1393}
1394
5099ac24 1395#[tracing::instrument(level = "debug", skip(selcx, obligation, candidate_set))]
ba9703b0
XL
1396fn assemble_candidates_from_impls<'cx, 'tcx>(
1397 selcx: &mut SelectionContext<'cx, 'tcx>,
1398 obligation: &ProjectionTyObligation<'tcx>,
5099ac24 1399 candidate_set: &mut ProjectionCandidateSet<'tcx>,
ba9703b0
XL
1400) {
1401 // If we are resolving `<T as TraitRef<...>>::Item == Type`,
1402 // start out by selecting the predicate `T as TraitRef<...>`:
c295e0f8 1403 let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx()));
ba9703b0
XL
1404 let trait_obligation = obligation.with(poly_trait_ref.to_poly_trait_predicate());
1405 let _ = selcx.infcx().commit_if_ok(|_| {
f035d41b
XL
1406 let impl_source = match selcx.select(&trait_obligation) {
1407 Ok(Some(impl_source)) => impl_source,
ba9703b0
XL
1408 Ok(None) => {
1409 candidate_set.mark_ambiguous();
1410 return Err(());
1411 }
1412 Err(e) => {
29967ef6 1413 debug!(error = ?e, "selection error");
ba9703b0
XL
1414 candidate_set.mark_error(e);
1415 return Err(());
1416 }
1417 };
1418
f035d41b 1419 let eligible = match &impl_source {
1b1a35ee
XL
1420 super::ImplSource::Closure(_)
1421 | super::ImplSource::Generator(_)
1422 | super::ImplSource::FnPointer(_)
5099ac24 1423 | super::ImplSource::TraitAlias(_) => true,
1b1a35ee 1424 super::ImplSource::UserDefined(impl_data) => {
ba9703b0
XL
1425 // We have to be careful when projecting out of an
1426 // impl because of specialization. If we are not in
1427 // codegen (i.e., projection mode is not "any"), and the
1428 // impl's type is declared as default, then we disable
1429 // projection (even if the trait ref is fully
1430 // monomorphic). In the case where trait ref is not
1431 // fully monomorphic (i.e., includes type parameters),
1432 // this is because those type parameters may
1433 // ultimately be bound to types from other crates that
1434 // may have specialized impls we can't see. In the
1435 // case where the trait ref IS fully monomorphic, this
1436 // is a policy decision that we made in the RFC in
1437 // order to preserve flexibility for the crate that
1438 // defined the specializable impl to specialize later
1439 // for existing types.
1440 //
1441 // In either case, we handle this by not adding a
1442 // candidate for an impl if it contains a `default`
1443 // type.
1444 //
1445 // NOTE: This should be kept in sync with the similar code in
fc512014 1446 // `rustc_ty_utils::instance::resolve_associated_item()`.
ba9703b0 1447 let node_item =
5099ac24 1448 assoc_def(selcx, impl_data.impl_def_id, obligation.predicate.item_def_id)
ee023bcb 1449 .map_err(|ErrorGuaranteed { .. }| ())?;
ba9703b0
XL
1450
1451 if node_item.is_final() {
1452 // Non-specializable items are always projectable.
1453 true
1454 } else {
1455 // Only reveal a specializable default if we're past type-checking
1456 // and the obligation is monomorphic, otherwise passes such as
1457 // transmute checking and polymorphic MIR optimizations could
1458 // get a result which isn't correct for all monomorphizations.
f035d41b 1459 if obligation.param_env.reveal() == Reveal::All {
ba9703b0
XL
1460 // NOTE(eddyb) inference variables can resolve to parameters, so
1461 // assume `poly_trait_ref` isn't monomorphic, if it contains any.
fc512014 1462 let poly_trait_ref = selcx.infcx().resolve_vars_if_possible(poly_trait_ref);
ba9703b0
XL
1463 !poly_trait_ref.still_further_specializable()
1464 } else {
1465 debug!(
29967ef6
XL
1466 assoc_ty = ?selcx.tcx().def_path_str(node_item.item.def_id),
1467 ?obligation.predicate,
1468 "assemble_candidates_from_impls: not eligible due to default",
ba9703b0
XL
1469 );
1470 false
1471 }
1472 }
1473 }
1b1a35ee 1474 super::ImplSource::DiscriminantKind(..) => {
f9f354fc
XL
1475 // While `DiscriminantKind` is automatically implemented for every type,
1476 // the concrete discriminant may not be known yet.
1477 //
1478 // Any type with multiple potential discriminant types is therefore not eligible.
1479 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1480
1b1a35ee 1481 match self_ty.kind() {
f9f354fc
XL
1482 ty::Bool
1483 | ty::Char
1484 | ty::Int(_)
1485 | ty::Uint(_)
1486 | ty::Float(_)
1487 | ty::Adt(..)
1488 | ty::Foreign(_)
1489 | ty::Str
1490 | ty::Array(..)
1491 | ty::Slice(_)
1492 | ty::RawPtr(..)
1493 | ty::Ref(..)
1494 | ty::FnDef(..)
1495 | ty::FnPtr(..)
1496 | ty::Dynamic(..)
1497 | ty::Closure(..)
1498 | ty::Generator(..)
1499 | ty::GeneratorWitness(..)
1500 | ty::Never
1501 | ty::Tuple(..)
1502 // Integers and floats always have `u8` as their discriminant.
1503 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1504
1505 ty::Projection(..)
1506 | ty::Opaque(..)
1507 | ty::Param(..)
1508 | ty::Bound(..)
1509 | ty::Placeholder(..)
1510 | ty::Infer(..)
f035d41b 1511 | ty::Error(_) => false,
f9f354fc
XL
1512 }
1513 }
6a06907d
XL
1514 super::ImplSource::Pointee(..) => {
1515 // While `Pointee` is automatically implemented for every type,
1516 // the concrete metadata type may not be known yet.
1517 //
1518 // Any type with multiple potential metadata types is therefore not eligible.
1519 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1520
5099ac24 1521 let tail = selcx.tcx().struct_tail_with_normalize(self_ty, |ty| {
ee023bcb
FG
1522 // We throw away any obligations we get from this, since we normalize
1523 // and confirm these obligations once again during confirmation
5099ac24
FG
1524 normalize_with_depth(
1525 selcx,
1526 obligation.param_env,
1527 obligation.cause.clone(),
1528 obligation.recursion_depth + 1,
1529 ty,
1530 )
1531 .value
1532 });
1533
6a06907d
XL
1534 match tail.kind() {
1535 ty::Bool
1536 | ty::Char
1537 | ty::Int(_)
1538 | ty::Uint(_)
1539 | ty::Float(_)
6a06907d
XL
1540 | ty::Str
1541 | ty::Array(..)
1542 | ty::Slice(_)
1543 | ty::RawPtr(..)
1544 | ty::Ref(..)
1545 | ty::FnDef(..)
1546 | ty::FnPtr(..)
1547 | ty::Dynamic(..)
1548 | ty::Closure(..)
1549 | ty::Generator(..)
1550 | ty::GeneratorWitness(..)
1551 | ty::Never
ee023bcb
FG
1552 // Extern types have unit metadata, according to RFC 2850
1553 | ty::Foreign(_)
6a06907d
XL
1554 // If returned by `struct_tail_without_normalization` this is a unit struct
1555 // without any fields, or not a struct, and therefore is Sized.
1556 | ty::Adt(..)
1557 // If returned by `struct_tail_without_normalization` this is the empty tuple.
1558 | ty::Tuple(..)
1559 // Integers and floats are always Sized, and so have unit type metadata.
1560 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1561
ee023bcb
FG
1562 // type parameters, opaques, and unnormalized projections have pointer
1563 // metadata if they're known (e.g. by the param_env) to be sized
1564 ty::Param(_) | ty::Projection(..) | ty::Opaque(..)
1565 if tail.is_sized(selcx.tcx().at(obligation.cause.span), obligation.param_env) =>
1566 {
1567 true
1568 }
1569
1570 // FIXME(compiler-errors): are Bound and Placeholder types ever known sized?
1571 ty::Param(_)
1572 | ty::Projection(..)
6a06907d 1573 | ty::Opaque(..)
6a06907d
XL
1574 | ty::Bound(..)
1575 | ty::Placeholder(..)
1576 | ty::Infer(..)
5099ac24
FG
1577 | ty::Error(_) => {
1578 if tail.has_infer_types() {
1579 candidate_set.mark_ambiguous();
1580 }
1581 false
ee023bcb 1582 }
6a06907d
XL
1583 }
1584 }
1b1a35ee 1585 super::ImplSource::Param(..) => {
ba9703b0
XL
1586 // This case tell us nothing about the value of an
1587 // associated type. Consider:
1588 //
1589 // ```
1590 // trait SomeTrait { type Foo; }
1591 // fn foo<T:SomeTrait>(...) { }
1592 // ```
1593 //
1594 // If the user writes `<T as SomeTrait>::Foo`, then the `T
1595 // : SomeTrait` binding does not help us decide what the
1596 // type `Foo` is (at least, not more specifically than
1597 // what we already knew).
1598 //
1599 // But wait, you say! What about an example like this:
1600 //
1601 // ```
1602 // fn bar<T:SomeTrait<Foo=usize>>(...) { ... }
1603 // ```
1604 //
ee023bcb 1605 // Doesn't the `T : SomeTrait<Foo=usize>` predicate help
ba9703b0
XL
1606 // resolve `T::Foo`? And of course it does, but in fact
1607 // that single predicate is desugared into two predicates
1608 // in the compiler: a trait predicate (`T : SomeTrait`) and a
1609 // projection. And the projection where clause is handled
1610 // in `assemble_candidates_from_param_env`.
1611 false
1612 }
29967ef6
XL
1613 super::ImplSource::Object(_) => {
1614 // Handled by the `Object` projection candidate. See
1615 // `assemble_candidates_from_object_ty` for an explanation of
1616 // why we special case object types.
1617 false
1618 }
94222f64
XL
1619 super::ImplSource::AutoImpl(..)
1620 | super::ImplSource::Builtin(..)
c295e0f8 1621 | super::ImplSource::TraitUpcasting(_)
ee023bcb 1622 | super::ImplSource::ConstDestruct(_) => {
ba9703b0 1623 // These traits have no associated types.
f9652781 1624 selcx.tcx().sess.delay_span_bug(
ba9703b0 1625 obligation.cause.span,
f9652781 1626 &format!("Cannot project an associated type from `{:?}`", impl_source),
ba9703b0 1627 );
f9652781 1628 return Err(());
ba9703b0
XL
1629 }
1630 };
1631
1632 if eligible {
5099ac24 1633 if candidate_set.push_candidate(ProjectionCandidate::Select(impl_source)) {
ba9703b0
XL
1634 Ok(())
1635 } else {
1636 Err(())
1637 }
1638 } else {
1639 Err(())
1640 }
1641 });
1642}
1643
1644fn confirm_candidate<'cx, 'tcx>(
1645 selcx: &mut SelectionContext<'cx, 'tcx>,
1646 obligation: &ProjectionTyObligation<'tcx>,
5099ac24 1647 candidate: ProjectionCandidate<'tcx>,
ba9703b0 1648) -> Progress<'tcx> {
29967ef6 1649 debug!(?obligation, ?candidate, "confirm_candidate");
f035d41b 1650 let mut progress = match candidate {
5099ac24
FG
1651 ProjectionCandidate::ParamEnv(poly_projection)
1652 | ProjectionCandidate::Object(poly_projection) => {
29967ef6
XL
1653 confirm_param_env_candidate(selcx, obligation, poly_projection, false)
1654 }
1655
5099ac24 1656 ProjectionCandidate::TraitDef(poly_projection) => {
29967ef6 1657 confirm_param_env_candidate(selcx, obligation, poly_projection, true)
ba9703b0
XL
1658 }
1659
5099ac24 1660 ProjectionCandidate::Select(impl_source) => {
29967ef6 1661 confirm_select_candidate(selcx, obligation, impl_source)
ba9703b0 1662 }
f035d41b 1663 };
5099ac24 1664
f035d41b
XL
1665 // When checking for cycle during evaluation, we compare predicates with
1666 // "syntactic" equality. Since normalization generally introduces a type
1667 // with new region variables, we need to resolve them to existing variables
1668 // when possible for this to work. See `auto-trait-projection-recursion.rs`
1669 // for a case where this matters.
5099ac24
FG
1670 if progress.term.has_infer_regions() {
1671 progress.term =
1672 progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx()));
ba9703b0 1673 }
f035d41b 1674 progress
ba9703b0
XL
1675}
1676
1677fn confirm_select_candidate<'cx, 'tcx>(
1678 selcx: &mut SelectionContext<'cx, 'tcx>,
1679 obligation: &ProjectionTyObligation<'tcx>,
f035d41b 1680 impl_source: Selection<'tcx>,
ba9703b0 1681) -> Progress<'tcx> {
f035d41b 1682 match impl_source {
1b1a35ee
XL
1683 super::ImplSource::UserDefined(data) => confirm_impl_candidate(selcx, obligation, data),
1684 super::ImplSource::Generator(data) => confirm_generator_candidate(selcx, obligation, data),
1685 super::ImplSource::Closure(data) => confirm_closure_candidate(selcx, obligation, data),
1686 super::ImplSource::FnPointer(data) => confirm_fn_pointer_candidate(selcx, obligation, data),
1687 super::ImplSource::DiscriminantKind(data) => {
f9f354fc
XL
1688 confirm_discriminant_kind_candidate(selcx, obligation, data)
1689 }
6a06907d 1690 super::ImplSource::Pointee(data) => confirm_pointee_candidate(selcx, obligation, data),
29967ef6
XL
1691 super::ImplSource::Object(_)
1692 | super::ImplSource::AutoImpl(..)
1b1a35ee
XL
1693 | super::ImplSource::Param(..)
1694 | super::ImplSource::Builtin(..)
94222f64 1695 | super::ImplSource::TraitUpcasting(_)
c295e0f8 1696 | super::ImplSource::TraitAlias(..)
ee023bcb 1697 | super::ImplSource::ConstDestruct(_) => {
29967ef6 1698 // we don't create Select candidates with this kind of resolution
ba9703b0
XL
1699 span_bug!(
1700 obligation.cause.span,
1701 "Cannot project an associated type from `{:?}`",
f035d41b 1702 impl_source
ba9703b0
XL
1703 )
1704 }
1705 }
1706}
1707
ba9703b0
XL
1708fn confirm_generator_candidate<'cx, 'tcx>(
1709 selcx: &mut SelectionContext<'cx, 'tcx>,
1710 obligation: &ProjectionTyObligation<'tcx>,
f035d41b 1711 impl_source: ImplSourceGeneratorData<'tcx, PredicateObligation<'tcx>>,
ba9703b0 1712) -> Progress<'tcx> {
f035d41b 1713 let gen_sig = impl_source.substs.as_generator().poly_sig();
ba9703b0
XL
1714 let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1715 selcx,
1716 obligation.param_env,
1717 obligation.cause.clone(),
1718 obligation.recursion_depth + 1,
fc512014 1719 gen_sig,
ba9703b0
XL
1720 );
1721
29967ef6 1722 debug!(?obligation, ?gen_sig, ?obligations, "confirm_generator_candidate");
ba9703b0
XL
1723
1724 let tcx = selcx.tcx();
1725
3dfed10e 1726 let gen_def_id = tcx.require_lang_item(LangItem::Generator, None);
ba9703b0
XL
1727
1728 let predicate = super::util::generator_trait_ref_and_outputs(
1729 tcx,
1730 gen_def_id,
1731 obligation.predicate.self_ty(),
1732 gen_sig,
1733 )
1734 .map_bound(|(trait_ref, yield_ty, return_ty)| {
5099ac24 1735 let name = tcx.associated_item(obligation.predicate.item_def_id).name;
ba9703b0
XL
1736 let ty = if name == sym::Return {
1737 return_ty
1738 } else if name == sym::Yield {
1739 yield_ty
1740 } else {
1741 bug!()
1742 };
1743
1744 ty::ProjectionPredicate {
1745 projection_ty: ty::ProjectionTy {
1746 substs: trait_ref.substs,
1747 item_def_id: obligation.predicate.item_def_id,
1748 },
5099ac24 1749 term: ty.into(),
ba9703b0
XL
1750 }
1751 });
1752
29967ef6 1753 confirm_param_env_candidate(selcx, obligation, predicate, false)
f035d41b 1754 .with_addl_obligations(impl_source.nested)
ba9703b0
XL
1755 .with_addl_obligations(obligations)
1756}
1757
f9f354fc
XL
1758fn confirm_discriminant_kind_candidate<'cx, 'tcx>(
1759 selcx: &mut SelectionContext<'cx, 'tcx>,
1760 obligation: &ProjectionTyObligation<'tcx>,
f035d41b 1761 _: ImplSourceDiscriminantKindData,
f9f354fc
XL
1762) -> Progress<'tcx> {
1763 let tcx = selcx.tcx();
1764
1765 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
cdc7bbd5
XL
1766 // We get here from `poly_project_and_unify_type` which replaces bound vars
1767 // with placeholders
1768 debug_assert!(!self_ty.has_escaping_bound_vars());
f9f354fc
XL
1769 let substs = tcx.mk_substs([self_ty.into()].iter());
1770
3dfed10e 1771 let discriminant_def_id = tcx.require_lang_item(LangItem::Discriminant, None);
f9f354fc
XL
1772
1773 let predicate = ty::ProjectionPredicate {
1774 projection_ty: ty::ProjectionTy { substs, item_def_id: discriminant_def_id },
5099ac24 1775 term: self_ty.discriminant_ty(tcx).into(),
f9f354fc
XL
1776 };
1777
fc512014
XL
1778 // We get here from `poly_project_and_unify_type` which replaces bound vars
1779 // with placeholders, so dummy is okay here.
1780 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
f9f354fc
XL
1781}
1782
6a06907d
XL
1783fn confirm_pointee_candidate<'cx, 'tcx>(
1784 selcx: &mut SelectionContext<'cx, 'tcx>,
1785 obligation: &ProjectionTyObligation<'tcx>,
1786 _: ImplSourcePointeeData,
1787) -> Progress<'tcx> {
1788 let tcx = selcx.tcx();
6a06907d 1789 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
6a06907d 1790
5099ac24 1791 let mut obligations = vec![];
ee023bcb 1792 let (metadata_ty, check_is_sized) = self_ty.ptr_metadata_ty(tcx, |ty| {
5099ac24
FG
1793 normalize_with_depth_to(
1794 selcx,
1795 obligation.param_env,
1796 obligation.cause.clone(),
1797 obligation.recursion_depth + 1,
1798 ty,
1799 &mut obligations,
1800 )
1801 });
ee023bcb
FG
1802 if check_is_sized {
1803 let sized_predicate = ty::Binder::dummy(ty::TraitRef::new(
1804 tcx.require_lang_item(LangItem::Sized, None),
1805 tcx.mk_substs_trait(self_ty, &[]),
1806 ))
1807 .without_const()
1808 .to_predicate(tcx);
1809 obligations.push(Obligation::new(
1810 obligation.cause.clone(),
1811 obligation.param_env,
1812 sized_predicate,
1813 ));
1814 }
5099ac24
FG
1815
1816 let substs = tcx.mk_substs([self_ty.into()].iter());
6a06907d
XL
1817 let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
1818
1819 let predicate = ty::ProjectionPredicate {
1820 projection_ty: ty::ProjectionTy { substs, item_def_id: metadata_def_id },
5099ac24 1821 term: metadata_ty.into(),
6a06907d
XL
1822 };
1823
136023e0 1824 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
5099ac24 1825 .with_addl_obligations(obligations)
6a06907d
XL
1826}
1827
ba9703b0
XL
1828fn confirm_fn_pointer_candidate<'cx, 'tcx>(
1829 selcx: &mut SelectionContext<'cx, 'tcx>,
1830 obligation: &ProjectionTyObligation<'tcx>,
f035d41b 1831 fn_pointer_impl_source: ImplSourceFnPointerData<'tcx, PredicateObligation<'tcx>>,
ba9703b0 1832) -> Progress<'tcx> {
f035d41b 1833 let fn_type = selcx.infcx().shallow_resolve(fn_pointer_impl_source.fn_ty);
ba9703b0
XL
1834 let sig = fn_type.fn_sig(selcx.tcx());
1835 let Normalized { value: sig, obligations } = normalize_with_depth(
1836 selcx,
1837 obligation.param_env,
1838 obligation.cause.clone(),
1839 obligation.recursion_depth + 1,
fc512014 1840 sig,
ba9703b0
XL
1841 );
1842
1843 confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
f035d41b 1844 .with_addl_obligations(fn_pointer_impl_source.nested)
ba9703b0
XL
1845 .with_addl_obligations(obligations)
1846}
1847
1848fn confirm_closure_candidate<'cx, 'tcx>(
1849 selcx: &mut SelectionContext<'cx, 'tcx>,
1850 obligation: &ProjectionTyObligation<'tcx>,
f035d41b 1851 impl_source: ImplSourceClosureData<'tcx, PredicateObligation<'tcx>>,
ba9703b0 1852) -> Progress<'tcx> {
f035d41b 1853 let closure_sig = impl_source.substs.as_closure().sig();
ba9703b0
XL
1854 let Normalized { value: closure_sig, obligations } = normalize_with_depth(
1855 selcx,
1856 obligation.param_env,
1857 obligation.cause.clone(),
1858 obligation.recursion_depth + 1,
fc512014 1859 closure_sig,
ba9703b0
XL
1860 );
1861
29967ef6 1862 debug!(?obligation, ?closure_sig, ?obligations, "confirm_closure_candidate");
ba9703b0
XL
1863
1864 confirm_callable_candidate(selcx, obligation, closure_sig, util::TupleArgumentsFlag::No)
f035d41b 1865 .with_addl_obligations(impl_source.nested)
ba9703b0
XL
1866 .with_addl_obligations(obligations)
1867}
1868
1869fn confirm_callable_candidate<'cx, 'tcx>(
1870 selcx: &mut SelectionContext<'cx, 'tcx>,
1871 obligation: &ProjectionTyObligation<'tcx>,
1872 fn_sig: ty::PolyFnSig<'tcx>,
1873 flag: util::TupleArgumentsFlag,
1874) -> Progress<'tcx> {
1875 let tcx = selcx.tcx();
1876
29967ef6 1877 debug!(?obligation, ?fn_sig, "confirm_callable_candidate");
ba9703b0 1878
3dfed10e
XL
1879 let fn_once_def_id = tcx.require_lang_item(LangItem::FnOnce, None);
1880 let fn_once_output_def_id = tcx.require_lang_item(LangItem::FnOnceOutput, None);
ba9703b0
XL
1881
1882 let predicate = super::util::closure_trait_ref_and_return_type(
1883 tcx,
1884 fn_once_def_id,
1885 obligation.predicate.self_ty(),
1886 fn_sig,
1887 flag,
1888 )
1889 .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
f035d41b
XL
1890 projection_ty: ty::ProjectionTy {
1891 substs: trait_ref.substs,
1892 item_def_id: fn_once_output_def_id,
1893 },
5099ac24 1894 term: ret_type.into(),
ba9703b0
XL
1895 });
1896
3c0e092e 1897 confirm_param_env_candidate(selcx, obligation, predicate, true)
ba9703b0
XL
1898}
1899
1900fn confirm_param_env_candidate<'cx, 'tcx>(
1901 selcx: &mut SelectionContext<'cx, 'tcx>,
1902 obligation: &ProjectionTyObligation<'tcx>,
1903 poly_cache_entry: ty::PolyProjectionPredicate<'tcx>,
29967ef6 1904 potentially_unnormalized_candidate: bool,
ba9703b0
XL
1905) -> Progress<'tcx> {
1906 let infcx = selcx.infcx();
1907 let cause = &obligation.cause;
1908 let param_env = obligation.param_env;
1909
1910 let (cache_entry, _) = infcx.replace_bound_vars_with_fresh_vars(
1911 cause.span,
1912 LateBoundRegionConversionTime::HigherRankedType,
fc512014 1913 poly_cache_entry,
ba9703b0
XL
1914 );
1915
6a06907d 1916 let cache_projection = cache_entry.projection_ty;
29967ef6 1917 let mut nested_obligations = Vec::new();
3c0e092e
XL
1918 let obligation_projection = obligation.predicate;
1919 let obligation_projection = ensure_sufficient_stack(|| {
1920 normalize_with_depth_to(
1921 selcx,
1922 obligation.param_env,
1923 obligation.cause.clone(),
1924 obligation.recursion_depth + 1,
1925 obligation_projection,
1926 &mut nested_obligations,
1927 )
1928 });
6a06907d 1929 let cache_projection = if potentially_unnormalized_candidate {
29967ef6
XL
1930 ensure_sufficient_stack(|| {
1931 normalize_with_depth_to(
1932 selcx,
1933 obligation.param_env,
1934 obligation.cause.clone(),
1935 obligation.recursion_depth + 1,
6a06907d 1936 cache_projection,
29967ef6
XL
1937 &mut nested_obligations,
1938 )
1939 })
1940 } else {
6a06907d 1941 cache_projection
29967ef6
XL
1942 };
1943
3c0e092e
XL
1944 debug!(?cache_projection, ?obligation_projection);
1945
6a06907d 1946 match infcx.at(cause, param_env).eq(cache_projection, obligation_projection) {
29967ef6
XL
1947 Ok(InferOk { value: _, obligations }) => {
1948 nested_obligations.extend(obligations);
1949 assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations);
5099ac24
FG
1950 // FIXME(associated_const_equality): Handle consts here as well? Maybe this progress type should just take
1951 // a term instead.
1952 Progress { term: cache_entry.term, obligations: nested_obligations }
29967ef6 1953 }
ba9703b0
XL
1954 Err(e) => {
1955 let msg = format!(
1956 "Failed to unify obligation `{:?}` with poly_projection `{:?}`: {:?}",
1957 obligation, poly_cache_entry, e,
1958 );
1959 debug!("confirm_param_env_candidate: {}", msg);
f035d41b 1960 let err = infcx.tcx.ty_error_with_message(obligation.cause.span, &msg);
5099ac24 1961 Progress { term: err.into(), obligations: vec![] }
ba9703b0
XL
1962 }
1963 }
1964}
1965
1966fn confirm_impl_candidate<'cx, 'tcx>(
1967 selcx: &mut SelectionContext<'cx, 'tcx>,
1968 obligation: &ProjectionTyObligation<'tcx>,
f035d41b 1969 impl_impl_source: ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>,
ba9703b0
XL
1970) -> Progress<'tcx> {
1971 let tcx = selcx.tcx();
1972
29967ef6 1973 let ImplSourceUserDefinedData { impl_def_id, substs, mut nested } = impl_impl_source;
ba9703b0
XL
1974 let assoc_item_id = obligation.predicate.item_def_id;
1975 let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
1976
1977 let param_env = obligation.param_env;
ee023bcb
FG
1978 let Ok(assoc_ty) = assoc_def(selcx, impl_def_id, assoc_item_id) else {
1979 return Progress { term: tcx.ty_error().into(), obligations: nested };
ba9703b0
XL
1980 };
1981
1982 if !assoc_ty.item.defaultness.has_value() {
1983 // This means that the impl is missing a definition for the
1984 // associated type. This error will be reported by the type
1985 // checker method `check_impl_items_against_trait`, so here we
1986 // just return Error.
1987 debug!(
1988 "confirm_impl_candidate: no associated type {:?} for {:?}",
5099ac24 1989 assoc_ty.item.name, obligation.predicate
ba9703b0 1990 );
5099ac24 1991 return Progress { term: tcx.ty_error().into(), obligations: nested };
ba9703b0 1992 }
f035d41b
XL
1993 // If we're trying to normalize `<Vec<u32> as X>::A<S>` using
1994 //`impl<T> X for Vec<T> { type A<Y> = Box<Y>; }`, then:
1995 //
1996 // * `obligation.predicate.substs` is `[Vec<u32>, S]`
1997 // * `substs` is `[u32]`
1998 // * `substs` ends up as `[u32, S]`
ba9703b0
XL
1999 let substs = obligation.predicate.substs.rebase_onto(tcx, trait_def_id, substs);
2000 let substs =
2001 translate_substs(selcx.infcx(), param_env, impl_def_id, substs, assoc_ty.defining_node);
f035d41b 2002 let ty = tcx.type_of(assoc_ty.item.def_id);
5099ac24
FG
2003 let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst);
2004 let term: ty::Term<'tcx> = if is_const {
2005 let identity_substs =
2006 crate::traits::InternalSubsts::identity_for_item(tcx, assoc_ty.item.def_id);
2007 let did = ty::WithOptConstParam::unknown(assoc_ty.item.def_id);
2008 let val = ty::ConstKind::Unevaluated(ty::Unevaluated::new(did, identity_substs));
2009 tcx.mk_const(ty::ConstS { ty, val }).into()
2010 } else {
2011 ty.into()
2012 };
ba9703b0 2013 if substs.len() != tcx.generics_of(assoc_ty.item.def_id).count() {
f035d41b 2014 let err = tcx.ty_error_with_message(
29967ef6 2015 obligation.cause.span,
f035d41b
XL
2016 "impl item and trait item have different parameter counts",
2017 );
5099ac24 2018 Progress { term: err.into(), obligations: nested }
ba9703b0 2019 } else {
29967ef6 2020 assoc_ty_own_obligations(selcx, obligation, &mut nested);
5099ac24 2021 Progress { term: term.subst(tcx, substs), obligations: nested }
ba9703b0
XL
2022 }
2023}
2024
29967ef6
XL
2025// Get obligations corresponding to the predicates from the where-clause of the
2026// associated type itself.
2027// Note: `feature(generic_associated_types)` is required to write such
ee023bcb 2028// predicates, even for non-generic associated types.
29967ef6
XL
2029fn assoc_ty_own_obligations<'cx, 'tcx>(
2030 selcx: &mut SelectionContext<'cx, 'tcx>,
2031 obligation: &ProjectionTyObligation<'tcx>,
2032 nested: &mut Vec<PredicateObligation<'tcx>>,
2033) {
2034 let tcx = selcx.tcx();
2035 for predicate in tcx
2036 .predicates_of(obligation.predicate.item_def_id)
2037 .instantiate_own(tcx, obligation.predicate.substs)
2038 .predicates
2039 {
2040 let normalized = normalize_with_depth_to(
2041 selcx,
2042 obligation.param_env,
2043 obligation.cause.clone(),
2044 obligation.recursion_depth + 1,
fc512014 2045 predicate,
29967ef6
XL
2046 nested,
2047 );
2048 nested.push(Obligation::with_depth(
2049 obligation.cause.clone(),
2050 obligation.recursion_depth + 1,
2051 obligation.param_env,
2052 normalized,
2053 ));
2054 }
2055}
2056
ba9703b0
XL
2057/// Locate the definition of an associated type in the specialization hierarchy,
2058/// starting from the given impl.
2059///
2060/// Based on the "projection mode", this lookup may in fact only examine the
2061/// topmost impl. See the comments for `Reveal` for more details.
5099ac24 2062fn assoc_def(
ba9703b0
XL
2063 selcx: &SelectionContext<'_, '_>,
2064 impl_def_id: DefId,
5099ac24 2065 assoc_def_id: DefId,
ee023bcb 2066) -> Result<specialization_graph::LeafDef, ErrorGuaranteed> {
ba9703b0 2067 let tcx = selcx.tcx();
ba9703b0
XL
2068 let trait_def_id = tcx.impl_trait_ref(impl_def_id).unwrap().def_id;
2069 let trait_def = tcx.trait_def(trait_def_id);
2070
2071 // This function may be called while we are still building the
2072 // specialization graph that is queried below (via TraitDef::ancestors()),
2073 // so, in order to avoid unnecessary infinite recursion, we manually look
2074 // for the associated item at the given impl.
2075 // If there is no such item in that impl, this function will fail with a
2076 // cycle error if the specialization graph is currently being built.
5099ac24
FG
2077 if let Some(&impl_item_id) = tcx.impl_item_implementor_ids(impl_def_id).get(&assoc_def_id) {
2078 let item = tcx.associated_item(impl_item_id);
2079 let impl_node = specialization_graph::Node::Impl(impl_def_id);
2080 return Ok(specialization_graph::LeafDef {
2081 item: *item,
2082 defining_node: impl_node,
2083 finalizing_node: if item.defaultness.is_default() { None } else { Some(impl_node) },
2084 });
ba9703b0
XL
2085 }
2086
2087 let ancestors = trait_def.ancestors(tcx, impl_def_id)?;
5099ac24 2088 if let Some(assoc_item) = ancestors.leaf_def(tcx, assoc_def_id) {
ba9703b0
XL
2089 Ok(assoc_item)
2090 } else {
2091 // This is saying that neither the trait nor
2092 // the impl contain a definition for this
2093 // associated type. Normally this situation
2094 // could only arise through a compiler bug --
2095 // if the user wrote a bad item name, it
2096 // should have failed in astconv.
5099ac24
FG
2097 bug!(
2098 "No associated type `{}` for {}",
2099 tcx.item_name(assoc_def_id),
2100 tcx.def_path_str(impl_def_id)
2101 )
ba9703b0
XL
2102 }
2103}
2104
a2a8927a 2105crate trait ProjectionCacheKeyExt<'cx, 'tcx>: Sized {
ba9703b0
XL
2106 fn from_poly_projection_predicate(
2107 selcx: &mut SelectionContext<'cx, 'tcx>,
f9f354fc 2108 predicate: ty::PolyProjectionPredicate<'tcx>,
ba9703b0
XL
2109 ) -> Option<Self>;
2110}
2111
a2a8927a 2112impl<'cx, 'tcx> ProjectionCacheKeyExt<'cx, 'tcx> for ProjectionCacheKey<'tcx> {
ba9703b0
XL
2113 fn from_poly_projection_predicate(
2114 selcx: &mut SelectionContext<'cx, 'tcx>,
f9f354fc 2115 predicate: ty::PolyProjectionPredicate<'tcx>,
ba9703b0
XL
2116 ) -> Option<Self> {
2117 let infcx = selcx.infcx();
2118 // We don't do cross-snapshot caching of obligations with escaping regions,
2119 // so there's no cache key to use
2120 predicate.no_bound_vars().map(|predicate| {
2121 ProjectionCacheKey::new(
2122 // We don't attempt to match up with a specific type-variable state
2123 // from a specific call to `opt_normalize_projection_type` - if
2124 // there's no precise match, the original cache entry is "stranded"
2125 // anyway.
fc512014 2126 infcx.resolve_vars_if_possible(predicate.projection_ty),
ba9703b0
XL
2127 )
2128 })
2129 }
2130}