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.
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.
11 //! Code for projecting associated types out of trait references.
13 use super::elaborate_predicates;
14 use super::get_impl_item_or_default;
15 use super::report_overflow_error;
16 use super::Obligation;
17 use super::ObligationCause;
18 use super::PredicateObligation;
19 use super::SelectionContext;
20 use super::SelectionError;
21 use super::VtableClosureData;
22 use super::VtableImplData;
25 use middle::infer::{self, TypeOrigin};
26 use middle::subst::Subst;
27 use middle::ty::{self, ToPredicate, RegionEscape, HasTypeFlags, ToPolyTraitRef, Ty, TyCtxt};
28 use middle::ty::fold::{TypeFoldable, TypeFolder};
30 use syntax::parse::token;
31 use util::common::FN_OUTPUT_NAME;
33 pub type PolyProjectionObligation<'tcx> =
34 Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
36 pub type ProjectionObligation<'tcx> =
37 Obligation<'tcx, ty::ProjectionPredicate<'tcx>>;
39 pub type ProjectionTyObligation<'tcx> =
40 Obligation<'tcx, ty::ProjectionTy<'tcx>>;
42 /// When attempting to resolve `<T as TraitRef>::Name` ...
44 pub enum ProjectionTyError<'tcx> {
45 /// ...we found multiple sources of information and couldn't resolve the ambiguity.
48 /// ...an error occurred matching `T : TraitRef`
49 TraitSelectionError(SelectionError<'tcx>),
53 pub struct MismatchedProjectionTypes<'tcx> {
54 pub err: ty::error::TypeError<'tcx>
57 #[derive(PartialEq, Eq, Debug)]
58 enum ProjectionTyCandidate<'tcx> {
59 // from a where-clause in the env or object type
60 ParamEnv(ty::PolyProjectionPredicate<'tcx>),
62 // from the definition of `Trait` when you have something like <<A as Trait>::B as Trait2>::C
63 TraitDef(ty::PolyProjectionPredicate<'tcx>),
66 Impl(VtableImplData<'tcx, PredicateObligation<'tcx>>),
68 // closure return type
69 Closure(VtableClosureData<'tcx, PredicateObligation<'tcx>>),
71 // fn pointer return type
75 struct ProjectionTyCandidateSet<'tcx> {
76 vec: Vec<ProjectionTyCandidate<'tcx>>,
80 /// Evaluates constraints of the form:
82 /// for<...> <T as Trait>::U == V
84 /// If successful, this may result in additional obligations.
85 pub fn poly_project_and_unify_type<'cx,'tcx>(
86 selcx: &mut SelectionContext<'cx,'tcx>,
87 obligation: &PolyProjectionObligation<'tcx>)
88 -> Result<Option<Vec<PredicateObligation<'tcx>>>, MismatchedProjectionTypes<'tcx>>
90 debug!("poly_project_and_unify_type(obligation={:?})",
93 let infcx = selcx.infcx();
94 infcx.commit_if_ok(|snapshot| {
95 let (skol_predicate, skol_map) =
96 infcx.skolemize_late_bound_regions(&obligation.predicate, snapshot);
98 let skol_obligation = obligation.with(skol_predicate);
99 match project_and_unify_type(selcx, &skol_obligation) {
101 match infcx.leak_check(&skol_map, snapshot) {
102 Ok(()) => Ok(infcx.plug_leaks(skol_map, snapshot, &result)),
103 Err(e) => Err(MismatchedProjectionTypes { err: e }),
113 /// Evaluates constraints of the form:
115 /// <T as Trait>::U == V
117 /// If successful, this may result in additional obligations.
118 fn project_and_unify_type<'cx,'tcx>(
119 selcx: &mut SelectionContext<'cx,'tcx>,
120 obligation: &ProjectionObligation<'tcx>)
121 -> Result<Option<Vec<PredicateObligation<'tcx>>>, MismatchedProjectionTypes<'tcx>>
123 debug!("project_and_unify_type(obligation={:?})",
126 let Normalized { value: normalized_ty, obligations } =
127 match opt_normalize_projection_type(selcx,
128 obligation.predicate.projection_ty.clone(),
129 obligation.cause.clone(),
130 obligation.recursion_depth) {
133 consider_unification_despite_ambiguity(selcx, obligation);
138 debug!("project_and_unify_type: normalized_ty={:?} obligations={:?}",
142 let infcx = selcx.infcx();
143 let origin = TypeOrigin::RelateOutputImplTypes(obligation.cause.span);
144 match infer::mk_eqty(infcx, true, origin, normalized_ty, obligation.predicate.ty) {
145 Ok(()) => Ok(Some(obligations)),
146 Err(err) => Err(MismatchedProjectionTypes { err: err }),
150 fn consider_unification_despite_ambiguity<'cx,'tcx>(selcx: &mut SelectionContext<'cx,'tcx>,
151 obligation: &ProjectionObligation<'tcx>) {
152 debug!("consider_unification_despite_ambiguity(obligation={:?})",
155 let def_id = obligation.predicate.projection_ty.trait_ref.def_id;
156 match selcx.tcx().lang_items.fn_trait_kind(def_id) {
161 let infcx = selcx.infcx();
162 let self_ty = obligation.predicate.projection_ty.trait_ref.self_ty();
163 let self_ty = infcx.shallow_resolve(self_ty);
164 debug!("consider_unification_despite_ambiguity: self_ty.sty={:?}",
167 ty::TyClosure(closure_def_id, ref substs) => {
168 let closure_typer = selcx.closure_typer();
169 let closure_type = closure_typer.closure_type(closure_def_id, substs);
170 let ty::Binder((_, ret_type)) =
171 util::closure_trait_ref_and_return_type(infcx.tcx,
175 util::TupleArgumentsFlag::No);
176 // We don't have to normalize the return type here - this is only
177 // reached for TyClosure: Fn inputs where the closure kind is
178 // still unknown, which should only occur in typeck where the
179 // closure type is already normalized.
181 infcx.replace_late_bound_regions_with_fresh_var(
182 obligation.cause.span,
183 infer::AssocTypeProjection(obligation.predicate.projection_ty.item_name),
184 &ty::Binder(ret_type));
186 debug!("consider_unification_despite_ambiguity: ret_type={:?}",
188 let origin = TypeOrigin::RelateOutputImplTypes(obligation.cause.span);
189 let obligation_ty = obligation.predicate.ty;
190 match infer::mk_eqty(infcx, true, origin, obligation_ty, ret_type) {
192 Err(_) => { /* ignore errors */ }
199 /// Normalizes any associated type projections in `value`, replacing
200 /// them with a fully resolved type where possible. The return value
201 /// combines the normalized result and any additional obligations that
202 /// were incurred as result.
203 pub fn normalize<'a,'b,'tcx,T>(selcx: &'a mut SelectionContext<'b,'tcx>,
204 cause: ObligationCause<'tcx>,
206 -> Normalized<'tcx, T>
207 where T : TypeFoldable<'tcx>
209 normalize_with_depth(selcx, cause, 0, value)
212 /// As `normalize`, but with a custom depth.
213 pub fn normalize_with_depth<'a,'b,'tcx,T>(selcx: &'a mut SelectionContext<'b,'tcx>,
214 cause: ObligationCause<'tcx>,
217 -> Normalized<'tcx, T>
218 where T : TypeFoldable<'tcx>
220 let mut normalizer = AssociatedTypeNormalizer::new(selcx, cause, depth);
221 let result = normalizer.fold(value);
225 obligations: normalizer.obligations,
229 struct AssociatedTypeNormalizer<'a,'b:'a,'tcx:'b> {
230 selcx: &'a mut SelectionContext<'b,'tcx>,
231 cause: ObligationCause<'tcx>,
232 obligations: Vec<PredicateObligation<'tcx>>,
236 impl<'a,'b,'tcx> AssociatedTypeNormalizer<'a,'b,'tcx> {
237 fn new(selcx: &'a mut SelectionContext<'b,'tcx>,
238 cause: ObligationCause<'tcx>,
240 -> AssociatedTypeNormalizer<'a,'b,'tcx>
242 AssociatedTypeNormalizer {
250 fn fold<T:TypeFoldable<'tcx>>(&mut self, value: &T) -> T {
251 let value = self.selcx.infcx().resolve_type_vars_if_possible(value);
253 if !value.has_projection_types() {
256 value.fold_with(self)
261 impl<'a,'b,'tcx> TypeFolder<'tcx> for AssociatedTypeNormalizer<'a,'b,'tcx> {
262 fn tcx(&self) -> &TyCtxt<'tcx> {
266 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
267 // We don't want to normalize associated types that occur inside of region
268 // binders, because they may contain bound regions, and we can't cope with that.
272 // for<'a> fn(<T as Foo<&'a>>::A)
274 // Instead of normalizing `<T as Foo<&'a>>::A` here, we'll
275 // normalize it when we instantiate those bound regions (which
276 // should occur eventually).
278 let ty = ty.super_fold_with(self);
280 ty::TyProjection(ref data) if !data.has_escaping_regions() => { // (*)
282 // (*) This is kind of hacky -- we need to be able to
283 // handle normalization within binders because
284 // otherwise we wind up a need to normalize when doing
285 // trait matching (since you can have a trait
286 // obligation like `for<'a> T::B : Fn(&'a int)`), but
287 // we can't normalize with bound regions in scope. So
288 // far now we just ignore binders but only normalize
289 // if all bound regions are gone (and then we still
290 // have to renormalize whenever we instantiate a
291 // binder). It would be better to normalize in a
292 // binding-aware fashion.
294 let Normalized { value: ty, obligations } =
295 normalize_projection_type(self.selcx,
299 self.obligations.extend(obligations);
311 pub struct Normalized<'tcx,T> {
313 pub obligations: Vec<PredicateObligation<'tcx>>,
316 pub type NormalizedTy<'tcx> = Normalized<'tcx, Ty<'tcx>>;
318 impl<'tcx,T> Normalized<'tcx,T> {
319 pub fn with<U>(self, value: U) -> Normalized<'tcx,U> {
320 Normalized { value: value, obligations: self.obligations }
324 /// The guts of `normalize`: normalize a specific projection like `<T
325 /// as Trait>::Item`. The result is always a type (and possibly
326 /// additional obligations). If ambiguity arises, which implies that
327 /// there are unresolved type variables in the projection, we will
328 /// substitute a fresh type variable `$X` and generate a new
329 /// obligation `<T as Trait>::Item == $X` for later.
330 pub fn normalize_projection_type<'a,'b,'tcx>(
331 selcx: &'a mut SelectionContext<'b,'tcx>,
332 projection_ty: ty::ProjectionTy<'tcx>,
333 cause: ObligationCause<'tcx>,
335 -> NormalizedTy<'tcx>
337 opt_normalize_projection_type(selcx, projection_ty.clone(), cause.clone(), depth)
338 .unwrap_or_else(move || {
339 // if we bottom out in ambiguity, create a type variable
340 // and a deferred predicate to resolve this when more type
341 // information is available.
343 let ty_var = selcx.infcx().next_ty_var();
344 let projection = ty::Binder(ty::ProjectionPredicate {
345 projection_ty: projection_ty,
348 let obligation = Obligation::with_depth(
349 cause, depth + 1, projection.to_predicate());
352 obligations: vec!(obligation)
357 /// The guts of `normalize`: normalize a specific projection like `<T
358 /// as Trait>::Item`. The result is always a type (and possibly
359 /// additional obligations). Returns `None` in the case of ambiguity,
360 /// which indicates that there are unbound type variables.
361 fn opt_normalize_projection_type<'a,'b,'tcx>(
362 selcx: &'a mut SelectionContext<'b,'tcx>,
363 projection_ty: ty::ProjectionTy<'tcx>,
364 cause: ObligationCause<'tcx>,
366 -> Option<NormalizedTy<'tcx>>
368 debug!("normalize_projection_type(\
369 projection_ty={:?}, \
374 let obligation = Obligation::with_depth(cause.clone(), depth, projection_ty.clone());
375 match project_type(selcx, &obligation) {
376 Ok(ProjectedTy::Progress(projected_ty, mut obligations)) => {
377 // if projection succeeded, then what we get out of this
378 // is also non-normalized (consider: it was derived from
379 // an impl, where-clause etc) and hence we must
382 debug!("normalize_projection_type: projected_ty={:?} depth={} obligations={:?}",
387 if projected_ty.has_projection_types() {
388 let mut normalizer = AssociatedTypeNormalizer::new(selcx, cause, depth+1);
389 let normalized_ty = normalizer.fold(&projected_ty);
391 debug!("normalize_projection_type: normalized_ty={:?} depth={}",
395 obligations.extend(normalizer.obligations);
397 value: normalized_ty,
398 obligations: obligations,
403 obligations: obligations,
407 Ok(ProjectedTy::NoProgress(projected_ty)) => {
408 debug!("normalize_projection_type: projected_ty={:?} no progress",
415 Err(ProjectionTyError::TooManyCandidates) => {
416 debug!("normalize_projection_type: too many candidates");
419 Err(ProjectionTyError::TraitSelectionError(_)) => {
420 debug!("normalize_projection_type: ERROR");
421 // if we got an error processing the `T as Trait` part,
422 // just return `ty::err` but add the obligation `T :
423 // Trait`, which when processed will cause the error to be
426 Some(normalize_to_error(selcx, projection_ty, cause, depth))
431 /// If we are projecting `<T as Trait>::Item`, but `T: Trait` does not
432 /// hold. In various error cases, we cannot generate a valid
433 /// normalized projection. Therefore, we create an inference variable
434 /// return an associated obligation that, when fulfilled, will lead to
437 /// Note that we used to return `TyError` here, but that was quite
438 /// dubious -- the premise was that an error would *eventually* be
439 /// reported, when the obligation was processed. But in general once
440 /// you see a `TyError` you are supposed to be able to assume that an
441 /// error *has been* reported, so that you can take whatever heuristic
442 /// paths you want to take. To make things worse, it was possible for
443 /// cycles to arise, where you basically had a setup like `<MyType<$0>
444 /// as Trait>::Foo == $0`. Here, normalizing `<MyType<$0> as
445 /// Trait>::Foo> to `[type error]` would lead to an obligation of
446 /// `<MyType<[type error]> as Trait>::Foo`. We are supposed to report
447 /// an error for this obligation, but we legitimately should not,
448 /// because it contains `[type error]`. Yuck! (See issue #29857 for
449 /// one case where this arose.)
450 fn normalize_to_error<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>,
451 projection_ty: ty::ProjectionTy<'tcx>,
452 cause: ObligationCause<'tcx>,
454 -> NormalizedTy<'tcx>
456 let trait_ref = projection_ty.trait_ref.to_poly_trait_ref();
457 let trait_obligation = Obligation { cause: cause,
458 recursion_depth: depth,
459 predicate: trait_ref.to_predicate() };
460 let new_value = selcx.infcx().next_ty_var();
463 obligations: vec!(trait_obligation)
467 enum ProjectedTy<'tcx> {
468 Progress(Ty<'tcx>, Vec<PredicateObligation<'tcx>>),
469 NoProgress(Ty<'tcx>),
472 /// Compute the result of a projection type (if we can).
473 fn project_type<'cx,'tcx>(
474 selcx: &mut SelectionContext<'cx,'tcx>,
475 obligation: &ProjectionTyObligation<'tcx>)
476 -> Result<ProjectedTy<'tcx>, ProjectionTyError<'tcx>>
478 debug!("project(obligation={:?})",
481 let recursion_limit = selcx.tcx().sess.recursion_limit.get();
482 if obligation.recursion_depth >= recursion_limit {
483 debug!("project: overflow!");
484 report_overflow_error(selcx.infcx(), &obligation, true);
487 let obligation_trait_ref =
488 selcx.infcx().resolve_type_vars_if_possible(&obligation.predicate.trait_ref);
490 debug!("project: obligation_trait_ref={:?}", obligation_trait_ref);
492 if obligation_trait_ref.references_error() {
493 return Ok(ProjectedTy::Progress(selcx.tcx().types.err, vec!()));
496 let mut candidates = ProjectionTyCandidateSet {
501 assemble_candidates_from_param_env(selcx,
503 &obligation_trait_ref,
506 assemble_candidates_from_trait_def(selcx,
508 &obligation_trait_ref,
511 if let Err(e) = assemble_candidates_from_impls(selcx,
513 &obligation_trait_ref,
515 return Err(ProjectionTyError::TraitSelectionError(e));
518 debug!("{} candidates, ambiguous={}",
519 candidates.vec.len(),
520 candidates.ambiguous);
522 // Inherent ambiguity that prevents us from even enumerating the
524 if candidates.ambiguous {
525 return Err(ProjectionTyError::TooManyCandidates);
530 // Note: `candidates.vec` seems to be on the critical path of the
531 // compiler. Replacing it with an hash set was also tried, which would
532 // render the following dedup unnecessary. It led to cleaner code but
533 // prolonged compiling time of `librustc` from 5m30s to 6m in one test, or
534 // ~9% performance lost.
535 if candidates.vec.len() > 1 {
537 while i < candidates.vec.len() {
538 let has_dup = (0..i).any(|j| candidates.vec[i] == candidates.vec[j]);
540 candidates.vec.swap_remove(i);
547 // Prefer where-clauses. As in select, if there are multiple
548 // candidates, we prefer where-clause candidates over impls. This
549 // may seem a bit surprising, since impls are the source of
550 // "truth" in some sense, but in fact some of the impls that SEEM
551 // applicable are not, because of nested obligations. Where
552 // clauses are the safer choice. See the comment on
553 // `select::SelectionCandidate` and #21974 for more details.
554 if candidates.vec.len() > 1 {
555 debug!("retaining param-env candidates only from {:?}", candidates.vec);
556 candidates.vec.retain(|c| match *c {
557 ProjectionTyCandidate::ParamEnv(..) => true,
558 ProjectionTyCandidate::Impl(..) |
559 ProjectionTyCandidate::Closure(..) |
560 ProjectionTyCandidate::TraitDef(..) |
561 ProjectionTyCandidate::FnPointer(..) => false,
563 debug!("resulting candidate set: {:?}", candidates.vec);
564 if candidates.vec.len() != 1 {
565 return Err(ProjectionTyError::TooManyCandidates);
569 assert!(candidates.vec.len() <= 1);
571 match candidates.vec.pop() {
573 let (ty, obligations) = confirm_candidate(selcx, obligation, candidate);
574 Ok(ProjectedTy::Progress(ty, obligations))
577 Ok(ProjectedTy::NoProgress(selcx.tcx().mk_projection(
578 obligation.predicate.trait_ref.clone(),
579 obligation.predicate.item_name)))
584 /// The first thing we have to do is scan through the parameter
585 /// environment to see whether there are any projection predicates
586 /// there that can answer this question.
587 fn assemble_candidates_from_param_env<'cx,'tcx>(
588 selcx: &mut SelectionContext<'cx,'tcx>,
589 obligation: &ProjectionTyObligation<'tcx>,
590 obligation_trait_ref: &ty::TraitRef<'tcx>,
591 candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
593 debug!("assemble_candidates_from_param_env(..)");
594 let env_predicates = selcx.param_env().caller_bounds.iter().cloned();
595 assemble_candidates_from_predicates(selcx,
597 obligation_trait_ref,
599 ProjectionTyCandidate::ParamEnv,
603 /// In the case of a nested projection like <<A as Foo>::FooT as Bar>::BarT, we may find
604 /// that the definition of `Foo` has some clues:
608 /// type FooT : Bar<BarT=i32>
612 /// Here, for example, we could conclude that the result is `i32`.
613 fn assemble_candidates_from_trait_def<'cx,'tcx>(
614 selcx: &mut SelectionContext<'cx,'tcx>,
615 obligation: &ProjectionTyObligation<'tcx>,
616 obligation_trait_ref: &ty::TraitRef<'tcx>,
617 candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
619 debug!("assemble_candidates_from_trait_def(..)");
621 // Check whether the self-type is itself a projection.
622 let trait_ref = match obligation_trait_ref.self_ty().sty {
623 ty::TyProjection(ref data) => data.trait_ref.clone(),
624 ty::TyInfer(ty::TyVar(_)) => {
625 // If the self-type is an inference variable, then it MAY wind up
626 // being a projected type, so induce an ambiguity.
627 candidate_set.ambiguous = true;
633 // If so, extract what we know from the trait and try to come up with a good answer.
634 let trait_predicates = selcx.tcx().lookup_predicates(trait_ref.def_id);
635 let bounds = trait_predicates.instantiate(selcx.tcx(), trait_ref.substs);
636 let bounds = elaborate_predicates(selcx.tcx(), bounds.predicates.into_vec());
637 assemble_candidates_from_predicates(selcx,
639 obligation_trait_ref,
641 ProjectionTyCandidate::TraitDef,
645 fn assemble_candidates_from_predicates<'cx,'tcx,I>(
646 selcx: &mut SelectionContext<'cx,'tcx>,
647 obligation: &ProjectionTyObligation<'tcx>,
648 obligation_trait_ref: &ty::TraitRef<'tcx>,
649 candidate_set: &mut ProjectionTyCandidateSet<'tcx>,
650 ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionTyCandidate<'tcx>,
652 where I: Iterator<Item=ty::Predicate<'tcx>>
654 debug!("assemble_candidates_from_predicates(obligation={:?})",
656 let infcx = selcx.infcx();
657 for predicate in env_predicates {
658 debug!("assemble_candidates_from_predicates: predicate={:?}",
661 ty::Predicate::Projection(ref data) => {
662 let same_name = data.item_name() == obligation.predicate.item_name;
664 let is_match = same_name && infcx.probe(|_| {
665 let origin = TypeOrigin::Misc(obligation.cause.span);
666 let data_poly_trait_ref =
667 data.to_poly_trait_ref();
668 let obligation_poly_trait_ref =
669 obligation_trait_ref.to_poly_trait_ref();
670 infcx.sub_poly_trait_refs(false,
673 obligation_poly_trait_ref).is_ok()
676 debug!("assemble_candidates_from_predicates: candidate={:?} \
677 is_match={} same_name={}",
678 data, is_match, same_name);
681 candidate_set.vec.push(ctor(data.clone()));
689 fn assemble_candidates_from_object_type<'cx,'tcx>(
690 selcx: &mut SelectionContext<'cx,'tcx>,
691 obligation: &ProjectionTyObligation<'tcx>,
692 obligation_trait_ref: &ty::TraitRef<'tcx>,
693 candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
695 let self_ty = obligation_trait_ref.self_ty();
696 let object_ty = selcx.infcx().shallow_resolve(self_ty);
697 debug!("assemble_candidates_from_object_type(object_ty={:?})",
699 let data = match object_ty.sty {
700 ty::TyTrait(ref data) => data,
702 selcx.tcx().sess.span_bug(
703 obligation.cause.span,
704 &format!("assemble_candidates_from_object_type called with non-object: {:?}",
708 let projection_bounds = data.projection_bounds_with_self_ty(selcx.tcx(), object_ty);
709 let env_predicates = projection_bounds.iter()
710 .map(|p| p.to_predicate())
712 let env_predicates = elaborate_predicates(selcx.tcx(), env_predicates);
713 assemble_candidates_from_predicates(selcx,
715 obligation_trait_ref,
717 ProjectionTyCandidate::ParamEnv,
721 fn assemble_candidates_from_impls<'cx,'tcx>(
722 selcx: &mut SelectionContext<'cx,'tcx>,
723 obligation: &ProjectionTyObligation<'tcx>,
724 obligation_trait_ref: &ty::TraitRef<'tcx>,
725 candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
726 -> Result<(), SelectionError<'tcx>>
728 // If we are resolving `<T as TraitRef<...>>::Item == Type`,
729 // start out by selecting the predicate `T as TraitRef<...>`:
730 let poly_trait_ref = obligation_trait_ref.to_poly_trait_ref();
731 let trait_obligation = obligation.with(poly_trait_ref.to_poly_trait_predicate());
732 let vtable = match selcx.select(&trait_obligation) {
733 Ok(Some(vtable)) => vtable,
735 candidate_set.ambiguous = true;
739 debug!("assemble_candidates_from_impls: selection error {:?}",
746 super::VtableImpl(data) => {
747 if data.substs.types.needs_infer() {
748 let assoc_ty_opt = get_impl_item_or_default(selcx.tcx(), data.impl_def_id, |cand| {
749 if let &ty::TypeTraitItem(ref assoc_ty) = cand {
750 if assoc_ty.name == obligation.predicate.item_name {
751 return Some(assoc_ty.defaultness);
757 if let Some((defaultness, source)) = assoc_ty_opt {
758 if !source.is_from_trait() && defaultness == hir::Defaultness::Default {
759 // FIXME: is it OK to not mark as ambiguous?
763 selcx.tcx().sess.span_bug(obligation.cause.span,
764 &format!("No associated type for {:?}",
765 obligation_trait_ref));
769 debug!("assemble_candidates_from_impls: impl candidate {:?}",
772 candidate_set.vec.push(
773 ProjectionTyCandidate::Impl(data));
775 super::VtableObject(_) => {
776 assemble_candidates_from_object_type(
777 selcx, obligation, obligation_trait_ref, candidate_set);
779 super::VtableClosure(data) => {
780 candidate_set.vec.push(
781 ProjectionTyCandidate::Closure(data));
783 super::VtableFnPointer(fn_type) => {
784 candidate_set.vec.push(
785 ProjectionTyCandidate::FnPointer(fn_type));
787 super::VtableParam(..) => {
788 // This case tell us nothing about the value of an
789 // associated type. Consider:
792 // trait SomeTrait { type Foo; }
793 // fn foo<T:SomeTrait>(...) { }
796 // If the user writes `<T as SomeTrait>::Foo`, then the `T
797 // : SomeTrait` binding does not help us decide what the
798 // type `Foo` is (at least, not more specifically than
799 // what we already knew).
801 // But wait, you say! What about an example like this:
804 // fn bar<T:SomeTrait<Foo=usize>>(...) { ... }
807 // Doesn't the `T : Sometrait<Foo=usize>` predicate help
808 // resolve `T::Foo`? And of course it does, but in fact
809 // that single predicate is desugared into two predicates
810 // in the compiler: a trait predicate (`T : SomeTrait`) and a
811 // projection. And the projection where clause is handled
812 // in `assemble_candidates_from_param_env`.
814 super::VtableDefaultImpl(..) |
815 super::VtableBuiltin(..) => {
816 // These traits have no associated types.
817 selcx.tcx().sess.span_bug(
818 obligation.cause.span,
819 &format!("Cannot project an associated type from `{:?}`",
827 fn confirm_candidate<'cx,'tcx>(
828 selcx: &mut SelectionContext<'cx,'tcx>,
829 obligation: &ProjectionTyObligation<'tcx>,
830 candidate: ProjectionTyCandidate<'tcx>)
831 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
833 debug!("confirm_candidate(candidate={:?}, obligation={:?})",
838 ProjectionTyCandidate::ParamEnv(poly_projection) |
839 ProjectionTyCandidate::TraitDef(poly_projection) => {
840 confirm_param_env_candidate(selcx, obligation, poly_projection)
843 ProjectionTyCandidate::Impl(impl_vtable) => {
844 confirm_impl_candidate(selcx, obligation, impl_vtable)
847 ProjectionTyCandidate::Closure(closure_vtable) => {
848 confirm_closure_candidate(selcx, obligation, closure_vtable)
851 ProjectionTyCandidate::FnPointer(fn_type) => {
852 confirm_fn_pointer_candidate(selcx, obligation, fn_type)
857 fn confirm_fn_pointer_candidate<'cx,'tcx>(
858 selcx: &mut SelectionContext<'cx,'tcx>,
859 obligation: &ProjectionTyObligation<'tcx>,
861 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
863 let fn_type = selcx.infcx().shallow_resolve(fn_type);
864 let sig = fn_type.fn_sig();
865 confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
868 fn confirm_closure_candidate<'cx,'tcx>(
869 selcx: &mut SelectionContext<'cx,'tcx>,
870 obligation: &ProjectionTyObligation<'tcx>,
871 vtable: VtableClosureData<'tcx, PredicateObligation<'tcx>>)
872 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
874 let closure_typer = selcx.closure_typer();
875 let closure_type = closure_typer.closure_type(vtable.closure_def_id, &vtable.substs);
879 } = normalize_with_depth(selcx,
880 obligation.cause.clone(),
881 obligation.recursion_depth+1,
883 let (ty, mut cc_obligations) = confirm_callable_candidate(selcx,
886 util::TupleArgumentsFlag::No);
887 obligations.append(&mut cc_obligations);
891 fn confirm_callable_candidate<'cx,'tcx>(
892 selcx: &mut SelectionContext<'cx,'tcx>,
893 obligation: &ProjectionTyObligation<'tcx>,
894 fn_sig: &ty::PolyFnSig<'tcx>,
895 flag: util::TupleArgumentsFlag)
896 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
898 let tcx = selcx.tcx();
900 debug!("confirm_callable_candidate({:?},{:?})",
904 // the `Output` associated type is declared on `FnOnce`
905 let fn_once_def_id = tcx.lang_items.fn_once_trait().unwrap();
907 // Note: we unwrap the binder here but re-create it below (1)
908 let ty::Binder((trait_ref, ret_type)) =
909 util::closure_trait_ref_and_return_type(tcx,
911 obligation.predicate.trait_ref.self_ty(),
915 let predicate = ty::Binder(ty::ProjectionPredicate { // (1) recreate binder here
916 projection_ty: ty::ProjectionTy {
917 trait_ref: trait_ref,
918 item_name: token::intern(FN_OUTPUT_NAME),
923 confirm_param_env_candidate(selcx, obligation, predicate)
926 fn confirm_param_env_candidate<'cx,'tcx>(
927 selcx: &mut SelectionContext<'cx,'tcx>,
928 obligation: &ProjectionTyObligation<'tcx>,
929 poly_projection: ty::PolyProjectionPredicate<'tcx>)
930 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
932 let infcx = selcx.infcx();
935 infcx.replace_late_bound_regions_with_fresh_var(
936 obligation.cause.span,
937 infer::LateBoundRegionConversionTime::HigherRankedType,
940 assert_eq!(projection.projection_ty.item_name,
941 obligation.predicate.item_name);
943 let origin = TypeOrigin::RelateOutputImplTypes(obligation.cause.span);
944 match infcx.eq_trait_refs(false,
946 obligation.predicate.trait_ref.clone(),
947 projection.projection_ty.trait_ref.clone()) {
950 selcx.tcx().sess.span_bug(
951 obligation.cause.span,
952 &format!("Failed to unify `{:?}` and `{:?}` in projection: {}",
959 (projection.ty, vec!())
962 fn confirm_impl_candidate<'cx,'tcx>(
963 selcx: &mut SelectionContext<'cx,'tcx>,
964 obligation: &ProjectionTyObligation<'tcx>,
965 impl_vtable: VtableImplData<'tcx, PredicateObligation<'tcx>>)
966 -> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
968 let VtableImplData { substs, nested, impl_def_id } = impl_vtable;
970 get_impl_item_or_default(selcx.tcx(), impl_def_id, |cand| {
971 if let &ty::TypeTraitItem(ref assoc_ty) = cand {
972 if assoc_ty.name == obligation.predicate.item_name {
973 if let Some(ty) = assoc_ty.ty {
976 // This means that the impl is missing a definition for the
977 // associated type. This error will be reported by the type
978 // checker method `check_impl_items_against_trait`, so here
979 // we just return TyError.
980 debug!("confirm_impl_candidate: no associated type {:?} for {:?}",
982 obligation.predicate.trait_ref);
983 return Some(selcx.tcx().types.err);
988 }).map(|(ty, source)| {
989 (ty.subst(selcx.tcx(), &source.translate_substs(selcx.tcx(), substs)), nested)
990 }).unwrap_or_else(|| {
991 selcx.tcx().sess.span_bug(obligation.cause.span,
992 &format!("No associated type for {:?}",
993 obligation.predicate.trait_ref));