]> git.proxmox.com Git - rustc.git/blob - src/librustc/middle/traits/project.rs
bf90b3a02a870f5f6e013d552cc22940566f7d19
[rustc.git] / src / librustc / middle / traits / project.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Code for projecting associated types out of trait references.
12
13 use super::elaborate_predicates;
14 use super::report_overflow_error;
15 use super::Obligation;
16 use super::ObligationCause;
17 use super::PredicateObligation;
18 use super::SelectionContext;
19 use super::SelectionError;
20 use super::VtableClosureData;
21 use super::VtableImplData;
22 use super::util;
23
24 use middle::infer;
25 use middle::subst::Subst;
26 use middle::ty::{self, ToPredicate, RegionEscape, HasTypeFlags, ToPolyTraitRef, Ty};
27 use middle::ty::fold::{TypeFoldable, TypeFolder};
28 use syntax::parse::token;
29 use util::common::FN_OUTPUT_NAME;
30
31 pub type PolyProjectionObligation<'tcx> =
32 Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
33
34 pub type ProjectionObligation<'tcx> =
35 Obligation<'tcx, ty::ProjectionPredicate<'tcx>>;
36
37 pub type ProjectionTyObligation<'tcx> =
38 Obligation<'tcx, ty::ProjectionTy<'tcx>>;
39
40 /// When attempting to resolve `<T as TraitRef>::Name` ...
41 #[derive(Debug)]
42 pub enum ProjectionTyError<'tcx> {
43 /// ...we found multiple sources of information and couldn't resolve the ambiguity.
44 TooManyCandidates,
45
46 /// ...an error occurred matching `T : TraitRef`
47 TraitSelectionError(SelectionError<'tcx>),
48 }
49
50 #[derive(Clone)]
51 pub struct MismatchedProjectionTypes<'tcx> {
52 pub err: ty::error::TypeError<'tcx>
53 }
54
55 #[derive(PartialEq, Eq, Debug)]
56 enum ProjectionTyCandidate<'tcx> {
57 // from a where-clause in the env or object type
58 ParamEnv(ty::PolyProjectionPredicate<'tcx>),
59
60 // from the definition of `Trait` when you have something like <<A as Trait>::B as Trait2>::C
61 TraitDef(ty::PolyProjectionPredicate<'tcx>),
62
63 // defined in an impl
64 Impl(VtableImplData<'tcx, PredicateObligation<'tcx>>),
65
66 // closure return type
67 Closure(VtableClosureData<'tcx, PredicateObligation<'tcx>>),
68
69 // fn pointer return type
70 FnPointer(Ty<'tcx>),
71 }
72
73 struct ProjectionTyCandidateSet<'tcx> {
74 vec: Vec<ProjectionTyCandidate<'tcx>>,
75 ambiguous: bool
76 }
77
78 /// Evaluates constraints of the form:
79 ///
80 /// for<...> <T as Trait>::U == V
81 ///
82 /// If successful, this may result in additional obligations.
83 pub fn poly_project_and_unify_type<'cx,'tcx>(
84 selcx: &mut SelectionContext<'cx,'tcx>,
85 obligation: &PolyProjectionObligation<'tcx>)
86 -> Result<Option<Vec<PredicateObligation<'tcx>>>, MismatchedProjectionTypes<'tcx>>
87 {
88 debug!("poly_project_and_unify_type(obligation={:?})",
89 obligation);
90
91 let infcx = selcx.infcx();
92 infcx.commit_if_ok(|snapshot| {
93 let (skol_predicate, skol_map) =
94 infcx.skolemize_late_bound_regions(&obligation.predicate, snapshot);
95
96 let skol_obligation = obligation.with(skol_predicate);
97 match project_and_unify_type(selcx, &skol_obligation) {
98 Ok(result) => {
99 match infcx.leak_check(&skol_map, snapshot) {
100 Ok(()) => Ok(infcx.plug_leaks(skol_map, snapshot, &result)),
101 Err(e) => Err(MismatchedProjectionTypes { err: e }),
102 }
103 }
104 Err(e) => {
105 Err(e)
106 }
107 }
108 })
109 }
110
111 /// Evaluates constraints of the form:
112 ///
113 /// <T as Trait>::U == V
114 ///
115 /// If successful, this may result in additional obligations.
116 fn project_and_unify_type<'cx,'tcx>(
117 selcx: &mut SelectionContext<'cx,'tcx>,
118 obligation: &ProjectionObligation<'tcx>)
119 -> Result<Option<Vec<PredicateObligation<'tcx>>>, MismatchedProjectionTypes<'tcx>>
120 {
121 debug!("project_and_unify_type(obligation={:?})",
122 obligation);
123
124 let Normalized { value: normalized_ty, obligations } =
125 match opt_normalize_projection_type(selcx,
126 obligation.predicate.projection_ty.clone(),
127 obligation.cause.clone(),
128 obligation.recursion_depth) {
129 Some(n) => n,
130 None => {
131 consider_unification_despite_ambiguity(selcx, obligation);
132 return Ok(None);
133 }
134 };
135
136 debug!("project_and_unify_type: normalized_ty={:?} obligations={:?}",
137 normalized_ty,
138 obligations);
139
140 let infcx = selcx.infcx();
141 let origin = infer::RelateOutputImplTypes(obligation.cause.span);
142 match infer::mk_eqty(infcx, true, origin, normalized_ty, obligation.predicate.ty) {
143 Ok(()) => Ok(Some(obligations)),
144 Err(err) => Err(MismatchedProjectionTypes { err: err }),
145 }
146 }
147
148 fn consider_unification_despite_ambiguity<'cx,'tcx>(selcx: &mut SelectionContext<'cx,'tcx>,
149 obligation: &ProjectionObligation<'tcx>) {
150 debug!("consider_unification_despite_ambiguity(obligation={:?})",
151 obligation);
152
153 let def_id = obligation.predicate.projection_ty.trait_ref.def_id;
154 match selcx.tcx().lang_items.fn_trait_kind(def_id) {
155 Some(_) => { }
156 None => { return; }
157 }
158
159 let infcx = selcx.infcx();
160 let self_ty = obligation.predicate.projection_ty.trait_ref.self_ty();
161 let self_ty = infcx.shallow_resolve(self_ty);
162 debug!("consider_unification_despite_ambiguity: self_ty.sty={:?}",
163 self_ty.sty);
164 match self_ty.sty {
165 ty::TyClosure(closure_def_id, ref substs) => {
166 let closure_typer = selcx.closure_typer();
167 let closure_type = closure_typer.closure_type(closure_def_id, substs);
168 let ty::Binder((_, ret_type)) =
169 util::closure_trait_ref_and_return_type(infcx.tcx,
170 def_id,
171 self_ty,
172 &closure_type.sig,
173 util::TupleArgumentsFlag::No);
174 // We don't have to normalize the return type here - this is only
175 // reached for TyClosure: Fn inputs where the closure kind is
176 // still unknown, which should only occur in typeck where the
177 // closure type is already normalized.
178 let (ret_type, _) =
179 infcx.replace_late_bound_regions_with_fresh_var(
180 obligation.cause.span,
181 infer::AssocTypeProjection(obligation.predicate.projection_ty.item_name),
182 &ty::Binder(ret_type));
183
184 debug!("consider_unification_despite_ambiguity: ret_type={:?}",
185 ret_type);
186 let origin = infer::RelateOutputImplTypes(obligation.cause.span);
187 let obligation_ty = obligation.predicate.ty;
188 match infer::mk_eqty(infcx, true, origin, obligation_ty, ret_type) {
189 Ok(()) => { }
190 Err(_) => { /* ignore errors */ }
191 }
192 }
193 _ => { }
194 }
195 }
196
197 /// Normalizes any associated type projections in `value`, replacing
198 /// them with a fully resolved type where possible. The return value
199 /// combines the normalized result and any additional obligations that
200 /// were incurred as result.
201 pub fn normalize<'a,'b,'tcx,T>(selcx: &'a mut SelectionContext<'b,'tcx>,
202 cause: ObligationCause<'tcx>,
203 value: &T)
204 -> Normalized<'tcx, T>
205 where T : TypeFoldable<'tcx> + HasTypeFlags
206 {
207 normalize_with_depth(selcx, cause, 0, value)
208 }
209
210 /// As `normalize`, but with a custom depth.
211 pub fn normalize_with_depth<'a,'b,'tcx,T>(selcx: &'a mut SelectionContext<'b,'tcx>,
212 cause: ObligationCause<'tcx>,
213 depth: usize,
214 value: &T)
215 -> Normalized<'tcx, T>
216 where T : TypeFoldable<'tcx> + HasTypeFlags
217 {
218 let mut normalizer = AssociatedTypeNormalizer::new(selcx, cause, depth);
219 let result = normalizer.fold(value);
220
221 Normalized {
222 value: result,
223 obligations: normalizer.obligations,
224 }
225 }
226
227 struct AssociatedTypeNormalizer<'a,'b:'a,'tcx:'b> {
228 selcx: &'a mut SelectionContext<'b,'tcx>,
229 cause: ObligationCause<'tcx>,
230 obligations: Vec<PredicateObligation<'tcx>>,
231 depth: usize,
232 }
233
234 impl<'a,'b,'tcx> AssociatedTypeNormalizer<'a,'b,'tcx> {
235 fn new(selcx: &'a mut SelectionContext<'b,'tcx>,
236 cause: ObligationCause<'tcx>,
237 depth: usize)
238 -> AssociatedTypeNormalizer<'a,'b,'tcx>
239 {
240 AssociatedTypeNormalizer {
241 selcx: selcx,
242 cause: cause,
243 obligations: vec!(),
244 depth: depth,
245 }
246 }
247
248 fn fold<T:TypeFoldable<'tcx> + HasTypeFlags>(&mut self, value: &T) -> T {
249 let value = self.selcx.infcx().resolve_type_vars_if_possible(value);
250
251 if !value.has_projection_types() {
252 value.clone()
253 } else {
254 value.fold_with(self)
255 }
256 }
257 }
258
259 impl<'a,'b,'tcx> TypeFolder<'tcx> for AssociatedTypeNormalizer<'a,'b,'tcx> {
260 fn tcx(&self) -> &ty::ctxt<'tcx> {
261 self.selcx.tcx()
262 }
263
264 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
265 // We don't want to normalize associated types that occur inside of region
266 // binders, because they may contain bound regions, and we can't cope with that.
267 //
268 // Example:
269 //
270 // for<'a> fn(<T as Foo<&'a>>::A)
271 //
272 // Instead of normalizing `<T as Foo<&'a>>::A` here, we'll
273 // normalize it when we instantiate those bound regions (which
274 // should occur eventually).
275
276 let ty = ty::fold::super_fold_ty(self, ty);
277 match ty.sty {
278 ty::TyProjection(ref data) if !data.has_escaping_regions() => { // (*)
279
280 // (*) This is kind of hacky -- we need to be able to
281 // handle normalization within binders because
282 // otherwise we wind up a need to normalize when doing
283 // trait matching (since you can have a trait
284 // obligation like `for<'a> T::B : Fn(&'a int)`), but
285 // we can't normalize with bound regions in scope. So
286 // far now we just ignore binders but only normalize
287 // if all bound regions are gone (and then we still
288 // have to renormalize whenever we instantiate a
289 // binder). It would be better to normalize in a
290 // binding-aware fashion.
291
292 let Normalized { value: ty, obligations } =
293 normalize_projection_type(self.selcx,
294 data.clone(),
295 self.cause.clone(),
296 self.depth);
297 self.obligations.extend(obligations);
298 ty
299 }
300
301 _ => {
302 ty
303 }
304 }
305 }
306 }
307
308 #[derive(Clone)]
309 pub struct Normalized<'tcx,T> {
310 pub value: T,
311 pub obligations: Vec<PredicateObligation<'tcx>>,
312 }
313
314 pub type NormalizedTy<'tcx> = Normalized<'tcx, Ty<'tcx>>;
315
316 impl<'tcx,T> Normalized<'tcx,T> {
317 pub fn with<U>(self, value: U) -> Normalized<'tcx,U> {
318 Normalized { value: value, obligations: self.obligations }
319 }
320 }
321
322 /// The guts of `normalize`: normalize a specific projection like `<T
323 /// as Trait>::Item`. The result is always a type (and possibly
324 /// additional obligations). If ambiguity arises, which implies that
325 /// there are unresolved type variables in the projection, we will
326 /// substitute a fresh type variable `$X` and generate a new
327 /// obligation `<T as Trait>::Item == $X` for later.
328 pub fn normalize_projection_type<'a,'b,'tcx>(
329 selcx: &'a mut SelectionContext<'b,'tcx>,
330 projection_ty: ty::ProjectionTy<'tcx>,
331 cause: ObligationCause<'tcx>,
332 depth: usize)
333 -> NormalizedTy<'tcx>
334 {
335 opt_normalize_projection_type(selcx, projection_ty.clone(), cause.clone(), depth)
336 .unwrap_or_else(move || {
337 // if we bottom out in ambiguity, create a type variable
338 // and a deferred predicate to resolve this when more type
339 // information is available.
340
341 let ty_var = selcx.infcx().next_ty_var();
342 let projection = ty::Binder(ty::ProjectionPredicate {
343 projection_ty: projection_ty,
344 ty: ty_var
345 });
346 let obligation = Obligation::with_depth(cause, depth+1, projection.to_predicate());
347 Normalized {
348 value: ty_var,
349 obligations: vec!(obligation)
350 }
351 })
352 }
353
354 /// The guts of `normalize`: normalize a specific projection like `<T
355 /// as Trait>::Item`. The result is always a type (and possibly
356 /// additional obligations). Returns `None` in the case of ambiguity,
357 /// which indicates that there are unbound type variables.
358 fn opt_normalize_projection_type<'a,'b,'tcx>(
359 selcx: &'a mut SelectionContext<'b,'tcx>,
360 projection_ty: ty::ProjectionTy<'tcx>,
361 cause: ObligationCause<'tcx>,
362 depth: usize)
363 -> Option<NormalizedTy<'tcx>>
364 {
365 debug!("normalize_projection_type(\
366 projection_ty={:?}, \
367 depth={})",
368 projection_ty,
369 depth);
370
371 let obligation = Obligation::with_depth(cause.clone(), depth, projection_ty.clone());
372 match project_type(selcx, &obligation) {
373 Ok(ProjectedTy::Progress(projected_ty, mut obligations)) => {
374 // if projection succeeded, then what we get out of this
375 // is also non-normalized (consider: it was derived from
376 // an impl, where-clause etc) and hence we must
377 // re-normalize it
378
379 debug!("normalize_projection_type: projected_ty={:?} depth={} obligations={:?}",
380 projected_ty,
381 depth,
382 obligations);
383
384 if projected_ty.has_projection_types() {
385 let mut normalizer = AssociatedTypeNormalizer::new(selcx, cause, depth);
386 let normalized_ty = normalizer.fold(&projected_ty);
387
388 debug!("normalize_projection_type: normalized_ty={:?} depth={}",
389 normalized_ty,
390 depth);
391
392 obligations.extend(normalizer.obligations);
393 Some(Normalized {
394 value: normalized_ty,
395 obligations: obligations,
396 })
397 } else {
398 Some(Normalized {
399 value: projected_ty,
400 obligations: obligations,
401 })
402 }
403 }
404 Ok(ProjectedTy::NoProgress(projected_ty)) => {
405 debug!("normalize_projection_type: projected_ty={:?} no progress",
406 projected_ty);
407 Some(Normalized {
408 value: projected_ty,
409 obligations: vec!()
410 })
411 }
412 Err(ProjectionTyError::TooManyCandidates) => {
413 debug!("normalize_projection_type: too many candidates");
414 None
415 }
416 Err(ProjectionTyError::TraitSelectionError(_)) => {
417 debug!("normalize_projection_type: ERROR");
418 // if we got an error processing the `T as Trait` part,
419 // just return `ty::err` but add the obligation `T :
420 // Trait`, which when processed will cause the error to be
421 // reported later
422
423 Some(normalize_to_error(selcx, projection_ty, cause, depth))
424 }
425 }
426 }
427
428 /// in various error cases, we just set TyError and return an obligation
429 /// that, when fulfilled, will lead to an error.
430 ///
431 /// FIXME: the TyError created here can enter the obligation we create,
432 /// leading to error messages involving TyError.
433 fn normalize_to_error<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>,
434 projection_ty: ty::ProjectionTy<'tcx>,
435 cause: ObligationCause<'tcx>,
436 depth: usize)
437 -> NormalizedTy<'tcx>
438 {
439 let trait_ref = projection_ty.trait_ref.to_poly_trait_ref();
440 let trait_obligation = Obligation { cause: cause,
441 recursion_depth: depth,
442 predicate: trait_ref.to_predicate() };
443 Normalized {
444 value: selcx.tcx().types.err,
445 obligations: vec!(trait_obligation)
446 }
447 }
448
449 enum ProjectedTy<'tcx> {
450 Progress(Ty<'tcx>, Vec<PredicateObligation<'tcx>>),
451 NoProgress(Ty<'tcx>),
452 }
453
454 /// Compute the result of a projection type (if we can).
455 fn project_type<'cx,'tcx>(
456 selcx: &mut SelectionContext<'cx,'tcx>,
457 obligation: &ProjectionTyObligation<'tcx>)
458 -> Result<ProjectedTy<'tcx>, ProjectionTyError<'tcx>>
459 {
460 debug!("project(obligation={:?})",
461 obligation);
462
463 let recursion_limit = selcx.tcx().sess.recursion_limit.get();
464 if obligation.recursion_depth >= recursion_limit {
465 debug!("project: overflow!");
466 report_overflow_error(selcx.infcx(), &obligation);
467 }
468
469 let obligation_trait_ref =
470 selcx.infcx().resolve_type_vars_if_possible(&obligation.predicate.trait_ref);
471
472 debug!("project: obligation_trait_ref={:?}", obligation_trait_ref);
473
474 if obligation_trait_ref.references_error() {
475 return Ok(ProjectedTy::Progress(selcx.tcx().types.err, vec!()));
476 }
477
478 let mut candidates = ProjectionTyCandidateSet {
479 vec: Vec::new(),
480 ambiguous: false,
481 };
482
483 assemble_candidates_from_param_env(selcx,
484 obligation,
485 &obligation_trait_ref,
486 &mut candidates);
487
488 assemble_candidates_from_trait_def(selcx,
489 obligation,
490 &obligation_trait_ref,
491 &mut candidates);
492
493 if let Err(e) = assemble_candidates_from_impls(selcx,
494 obligation,
495 &obligation_trait_ref,
496 &mut candidates) {
497 return Err(ProjectionTyError::TraitSelectionError(e));
498 }
499
500 debug!("{} candidates, ambiguous={}",
501 candidates.vec.len(),
502 candidates.ambiguous);
503
504 // Inherent ambiguity that prevents us from even enumerating the
505 // candidates.
506 if candidates.ambiguous {
507 return Err(ProjectionTyError::TooManyCandidates);
508 }
509
510 // Drop duplicates.
511 //
512 // Note: `candidates.vec` seems to be on the critical path of the
513 // compiler. Replacing it with an hash set was also tried, which would
514 // render the following dedup unnecessary. It led to cleaner code but
515 // prolonged compiling time of `librustc` from 5m30s to 6m in one test, or
516 // ~9% performance lost.
517 if candidates.vec.len() > 1 {
518 let mut i = 0;
519 while i < candidates.vec.len() {
520 let has_dup = (0..i).any(|j| candidates.vec[i] == candidates.vec[j]);
521 if has_dup {
522 candidates.vec.swap_remove(i);
523 } else {
524 i += 1;
525 }
526 }
527 }
528
529 // Prefer where-clauses. As in select, if there are multiple
530 // candidates, we prefer where-clause candidates over impls. This
531 // may seem a bit surprising, since impls are the source of
532 // "truth" in some sense, but in fact some of the impls that SEEM
533 // applicable are not, because of nested obligations. Where
534 // clauses are the safer choice. See the comment on
535 // `select::SelectionCandidate` and #21974 for more details.
536 if candidates.vec.len() > 1 {
537 debug!("retaining param-env candidates only from {:?}", candidates.vec);
538 candidates.vec.retain(|c| match *c {
539 ProjectionTyCandidate::ParamEnv(..) => true,
540 ProjectionTyCandidate::Impl(..) |
541 ProjectionTyCandidate::Closure(..) |
542 ProjectionTyCandidate::TraitDef(..) |
543 ProjectionTyCandidate::FnPointer(..) => false,
544 });
545 debug!("resulting candidate set: {:?}", candidates.vec);
546 if candidates.vec.len() != 1 {
547 return Err(ProjectionTyError::TooManyCandidates);
548 }
549 }
550
551 assert!(candidates.vec.len() <= 1);
552
553 match candidates.vec.pop() {
554 Some(candidate) => {
555 let (ty, obligations) = confirm_candidate(selcx, obligation, candidate);
556 Ok(ProjectedTy::Progress(ty, obligations))
557 }
558 None => {
559 Ok(ProjectedTy::NoProgress(selcx.tcx().mk_projection(
560 obligation.predicate.trait_ref.clone(),
561 obligation.predicate.item_name)))
562 }
563 }
564 }
565
566 /// The first thing we have to do is scan through the parameter
567 /// environment to see whether there are any projection predicates
568 /// there that can answer this question.
569 fn assemble_candidates_from_param_env<'cx,'tcx>(
570 selcx: &mut SelectionContext<'cx,'tcx>,
571 obligation: &ProjectionTyObligation<'tcx>,
572 obligation_trait_ref: &ty::TraitRef<'tcx>,
573 candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
574 {
575 debug!("assemble_candidates_from_param_env(..)");
576 let env_predicates = selcx.param_env().caller_bounds.iter().cloned();
577 assemble_candidates_from_predicates(selcx,
578 obligation,
579 obligation_trait_ref,
580 candidate_set,
581 ProjectionTyCandidate::ParamEnv,
582 env_predicates);
583 }
584
585 /// In the case of a nested projection like <<A as Foo>::FooT as Bar>::BarT, we may find
586 /// that the definition of `Foo` has some clues:
587 ///
588 /// ```
589 /// trait Foo {
590 /// type FooT : Bar<BarT=i32>
591 /// }
592 /// ```
593 ///
594 /// Here, for example, we could conclude that the result is `i32`.
595 fn assemble_candidates_from_trait_def<'cx,'tcx>(
596 selcx: &mut SelectionContext<'cx,'tcx>,
597 obligation: &ProjectionTyObligation<'tcx>,
598 obligation_trait_ref: &ty::TraitRef<'tcx>,
599 candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
600 {
601 debug!("assemble_candidates_from_trait_def(..)");
602
603 // Check whether the self-type is itself a projection.
604 let trait_ref = match obligation_trait_ref.self_ty().sty {
605 ty::TyProjection(ref data) => data.trait_ref.clone(),
606 ty::TyInfer(ty::TyVar(_)) => {
607 // If the self-type is an inference variable, then it MAY wind up
608 // being a projected type, so induce an ambiguity.
609 candidate_set.ambiguous = true;
610 return;
611 }
612 _ => { return; }
613 };
614
615 // If so, extract what we know from the trait and try to come up with a good answer.
616 let trait_predicates = selcx.tcx().lookup_predicates(trait_ref.def_id);
617 let bounds = trait_predicates.instantiate(selcx.tcx(), trait_ref.substs);
618 let bounds = elaborate_predicates(selcx.tcx(), bounds.predicates.into_vec());
619 assemble_candidates_from_predicates(selcx,
620 obligation,
621 obligation_trait_ref,
622 candidate_set,
623 ProjectionTyCandidate::TraitDef,
624 bounds)
625 }
626
627 fn assemble_candidates_from_predicates<'cx,'tcx,I>(
628 selcx: &mut SelectionContext<'cx,'tcx>,
629 obligation: &ProjectionTyObligation<'tcx>,
630 obligation_trait_ref: &ty::TraitRef<'tcx>,
631 candidate_set: &mut ProjectionTyCandidateSet<'tcx>,
632 ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionTyCandidate<'tcx>,
633 env_predicates: I)
634 where I: Iterator<Item=ty::Predicate<'tcx>>
635 {
636 debug!("assemble_candidates_from_predicates(obligation={:?})",
637 obligation);
638 let infcx = selcx.infcx();
639 for predicate in env_predicates {
640 debug!("assemble_candidates_from_predicates: predicate={:?}",
641 predicate);
642 match predicate {
643 ty::Predicate::Projection(ref data) => {
644 let same_name = data.item_name() == obligation.predicate.item_name;
645
646 let is_match = same_name && infcx.probe(|_| {
647 let origin = infer::Misc(obligation.cause.span);
648 let data_poly_trait_ref =
649 data.to_poly_trait_ref();
650 let obligation_poly_trait_ref =
651 obligation_trait_ref.to_poly_trait_ref();
652 infcx.sub_poly_trait_refs(false,
653 origin,
654 data_poly_trait_ref,
655 obligation_poly_trait_ref).is_ok()
656 });
657
658 debug!("assemble_candidates_from_predicates: candidate={:?} \
659 is_match={} same_name={}",
660 data, is_match, same_name);
661
662 if is_match {
663 candidate_set.vec.push(ctor(data.clone()));
664 }
665 }
666 _ => { }
667 }
668 }
669 }
670
671 fn assemble_candidates_from_object_type<'cx,'tcx>(
672 selcx: &mut SelectionContext<'cx,'tcx>,
673 obligation: &ProjectionTyObligation<'tcx>,
674 obligation_trait_ref: &ty::TraitRef<'tcx>,
675 candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
676 {
677 let self_ty = obligation_trait_ref.self_ty();
678 let object_ty = selcx.infcx().shallow_resolve(self_ty);
679 debug!("assemble_candidates_from_object_type(object_ty={:?})",
680 object_ty);
681 let data = match object_ty.sty {
682 ty::TyTrait(ref data) => data,
683 _ => {
684 selcx.tcx().sess.span_bug(
685 obligation.cause.span,
686 &format!("assemble_candidates_from_object_type called with non-object: {:?}",
687 object_ty));
688 }
689 };
690 let projection_bounds = data.projection_bounds_with_self_ty(selcx.tcx(), object_ty);
691 let env_predicates = projection_bounds.iter()
692 .map(|p| p.to_predicate())
693 .collect();
694 let env_predicates = elaborate_predicates(selcx.tcx(), env_predicates);
695 assemble_candidates_from_predicates(selcx,
696 obligation,
697 obligation_trait_ref,
698 candidate_set,
699 ProjectionTyCandidate::ParamEnv,
700 env_predicates)
701 }
702
703 fn assemble_candidates_from_impls<'cx,'tcx>(
704 selcx: &mut SelectionContext<'cx,'tcx>,
705 obligation: &ProjectionTyObligation<'tcx>,
706 obligation_trait_ref: &ty::TraitRef<'tcx>,
707 candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
708 -> Result<(), SelectionError<'tcx>>
709 {
710 // If we are resolving `<T as TraitRef<...>>::Item == Type`,
711 // start out by selecting the predicate `T as TraitRef<...>`:
712 let poly_trait_ref = obligation_trait_ref.to_poly_trait_ref();
713 let trait_obligation = obligation.with(poly_trait_ref.to_poly_trait_predicate());
714 let vtable = match selcx.select(&trait_obligation) {
715 Ok(Some(vtable)) => vtable,
716 Ok(None) => {
717 candidate_set.ambiguous = true;
718 return Ok(());
719 }
720 Err(e) => {
721 debug!("assemble_candidates_from_impls: selection error {:?}",
722 e);
723 return Err(e);
724 }
725 };
726
727 match vtable {
728 super::VtableImpl(data) => {
729 debug!("assemble_candidates_from_impls: impl candidate {:?}",
730 data);
731
732 candidate_set.vec.push(
733 ProjectionTyCandidate::Impl(data));
734 }
735 super::VtableObject(_) => {
736 assemble_candidates_from_object_type(
737 selcx, obligation, obligation_trait_ref, candidate_set);
738 }
739 super::VtableClosure(data) => {
740 candidate_set.vec.push(
741 ProjectionTyCandidate::Closure(data));
742 }
743 super::VtableFnPointer(fn_type) => {
744 candidate_set.vec.push(
745 ProjectionTyCandidate::FnPointer(fn_type));
746 }
747 super::VtableParam(..) => {
748 // This case tell us nothing about the value of an
749 // associated type. Consider:
750 //
751 // ```
752 // trait SomeTrait { type Foo; }
753 // fn foo<T:SomeTrait>(...) { }
754 // ```
755 //
756 // If the user writes `<T as SomeTrait>::Foo`, then the `T
757 // : SomeTrait` binding does not help us decide what the
758 // type `Foo` is (at least, not more specifically than
759 // what we already knew).
760 //
761 // But wait, you say! What about an example like this:
762 //
763 // ```
764 // fn bar<T:SomeTrait<Foo=usize>>(...) { ... }
765 // ```
766 //
767 // Doesn't the `T : Sometrait<Foo=usize>` predicate help
768 // resolve `T::Foo`? And of course it does, but in fact
769 // that single predicate is desugared into two predicates
770 // in the compiler: a trait predicate (`T : SomeTrait`) and a
771 // projection. And the projection where clause is handled
772 // in `assemble_candidates_from_param_env`.
773 }
774 super::VtableDefaultImpl(..) |
775 super::VtableBuiltin(..) => {
776 // These traits have no associated types.
777 selcx.tcx().sess.span_bug(
778 obligation.cause.span,
779 &format!("Cannot project an associated type from `{:?}`",
780 vtable));
781 }
782 }
783
784 Ok(())
785 }
786
787 fn confirm_candidate<'cx,'tcx>(
788 selcx: &mut SelectionContext<'cx,'tcx>,
789 obligation: &ProjectionTyObligation<'tcx>,
790 candidate: ProjectionTyCandidate<'tcx>)
791 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
792 {
793 debug!("confirm_candidate(candidate={:?}, obligation={:?})",
794 candidate,
795 obligation);
796
797 match candidate {
798 ProjectionTyCandidate::ParamEnv(poly_projection) |
799 ProjectionTyCandidate::TraitDef(poly_projection) => {
800 confirm_param_env_candidate(selcx, obligation, poly_projection)
801 }
802
803 ProjectionTyCandidate::Impl(impl_vtable) => {
804 confirm_impl_candidate(selcx, obligation, impl_vtable)
805 }
806
807 ProjectionTyCandidate::Closure(closure_vtable) => {
808 confirm_closure_candidate(selcx, obligation, closure_vtable)
809 }
810
811 ProjectionTyCandidate::FnPointer(fn_type) => {
812 confirm_fn_pointer_candidate(selcx, obligation, fn_type)
813 }
814 }
815 }
816
817 fn confirm_fn_pointer_candidate<'cx,'tcx>(
818 selcx: &mut SelectionContext<'cx,'tcx>,
819 obligation: &ProjectionTyObligation<'tcx>,
820 fn_type: Ty<'tcx>)
821 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
822 {
823 let fn_type = selcx.infcx().shallow_resolve(fn_type);
824 let sig = fn_type.fn_sig();
825 confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
826 }
827
828 fn confirm_closure_candidate<'cx,'tcx>(
829 selcx: &mut SelectionContext<'cx,'tcx>,
830 obligation: &ProjectionTyObligation<'tcx>,
831 vtable: VtableClosureData<'tcx, PredicateObligation<'tcx>>)
832 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
833 {
834 let closure_typer = selcx.closure_typer();
835 let closure_type = closure_typer.closure_type(vtable.closure_def_id, &vtable.substs);
836 let Normalized {
837 value: closure_type,
838 mut obligations
839 } = normalize_with_depth(selcx,
840 obligation.cause.clone(),
841 obligation.recursion_depth+1,
842 &closure_type);
843 let (ty, mut cc_obligations) = confirm_callable_candidate(selcx,
844 obligation,
845 &closure_type.sig,
846 util::TupleArgumentsFlag::No);
847 obligations.append(&mut cc_obligations);
848 (ty, obligations)
849 }
850
851 fn confirm_callable_candidate<'cx,'tcx>(
852 selcx: &mut SelectionContext<'cx,'tcx>,
853 obligation: &ProjectionTyObligation<'tcx>,
854 fn_sig: &ty::PolyFnSig<'tcx>,
855 flag: util::TupleArgumentsFlag)
856 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
857 {
858 let tcx = selcx.tcx();
859
860 debug!("confirm_callable_candidate({:?},{:?})",
861 obligation,
862 fn_sig);
863
864 // the `Output` associated type is declared on `FnOnce`
865 let fn_once_def_id = tcx.lang_items.fn_once_trait().unwrap();
866
867 // Note: we unwrap the binder here but re-create it below (1)
868 let ty::Binder((trait_ref, ret_type)) =
869 util::closure_trait_ref_and_return_type(tcx,
870 fn_once_def_id,
871 obligation.predicate.trait_ref.self_ty(),
872 fn_sig,
873 flag);
874
875 let predicate = ty::Binder(ty::ProjectionPredicate { // (1) recreate binder here
876 projection_ty: ty::ProjectionTy {
877 trait_ref: trait_ref,
878 item_name: token::intern(FN_OUTPUT_NAME),
879 },
880 ty: ret_type
881 });
882
883 confirm_param_env_candidate(selcx, obligation, predicate)
884 }
885
886 fn confirm_param_env_candidate<'cx,'tcx>(
887 selcx: &mut SelectionContext<'cx,'tcx>,
888 obligation: &ProjectionTyObligation<'tcx>,
889 poly_projection: ty::PolyProjectionPredicate<'tcx>)
890 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
891 {
892 let infcx = selcx.infcx();
893
894 let projection =
895 infcx.replace_late_bound_regions_with_fresh_var(
896 obligation.cause.span,
897 infer::LateBoundRegionConversionTime::HigherRankedType,
898 &poly_projection).0;
899
900 assert_eq!(projection.projection_ty.item_name,
901 obligation.predicate.item_name);
902
903 let origin = infer::RelateOutputImplTypes(obligation.cause.span);
904 match infcx.sub_trait_refs(false,
905 origin,
906 obligation.predicate.trait_ref.clone(),
907 projection.projection_ty.trait_ref.clone()) {
908 Ok(()) => { }
909 Err(e) => {
910 selcx.tcx().sess.span_bug(
911 obligation.cause.span,
912 &format!("Failed to unify `{:?}` and `{:?}` in projection: {}",
913 obligation,
914 projection,
915 e));
916 }
917 }
918
919 (projection.ty, vec!())
920 }
921
922 fn confirm_impl_candidate<'cx,'tcx>(
923 selcx: &mut SelectionContext<'cx,'tcx>,
924 obligation: &ProjectionTyObligation<'tcx>,
925 impl_vtable: VtableImplData<'tcx, PredicateObligation<'tcx>>)
926 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
927 {
928 // there don't seem to be nicer accessors to these:
929 let impl_or_trait_items_map = selcx.tcx().impl_or_trait_items.borrow();
930
931 // Look for the associated type in the impl
932 for impl_item in &selcx.tcx().impl_items.borrow()[&impl_vtable.impl_def_id] {
933 if let ty::TypeTraitItem(ref assoc_ty) = impl_or_trait_items_map[&impl_item.def_id()] {
934 if assoc_ty.name == obligation.predicate.item_name {
935 return (assoc_ty.ty.unwrap().subst(selcx.tcx(), &impl_vtable.substs),
936 impl_vtable.nested);
937 }
938 }
939 }
940
941 // It is not in the impl - get the default from the trait.
942 let trait_ref = obligation.predicate.trait_ref;
943 for trait_item in selcx.tcx().trait_items(trait_ref.def_id).iter() {
944 if let &ty::TypeTraitItem(ref assoc_ty) = trait_item {
945 if assoc_ty.name == obligation.predicate.item_name {
946 if let Some(ty) = assoc_ty.ty {
947 return (ty.subst(selcx.tcx(), trait_ref.substs),
948 impl_vtable.nested);
949 } else {
950 // This means that the impl is missing a
951 // definition for the associated type. This error
952 // ought to be reported by the type checker method
953 // `check_impl_items_against_trait`, so here we
954 // just return TyError.
955 debug!("confirm_impl_candidate: no associated type {:?} for {:?}",
956 assoc_ty.name,
957 trait_ref);
958 return (selcx.tcx().types.err, vec!());
959 }
960 }
961 }
962 }
963
964 selcx.tcx().sess.span_bug(obligation.cause.span,
965 &format!("No associated type for {:?}",
966 trait_ref));
967 }