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