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 //! See `doc.rs` for high-level documentation
13 use super::SelectionContext;
14 use super::{Obligation, ObligationCause};
18 use middle::subst::{Subst, TypeSpace};
19 use middle::ty::{self, Ty};
20 use middle::infer::InferCtxt;
21 use std::collections::HashSet;
24 use syntax::codemap::DUMMY_SP;
25 use util::ppaux::Repr;
27 pub fn impl_can_satisfy(infcx: &InferCtxt,
28 impl1_def_id: ast::DefId,
29 impl2_def_id: ast::DefId)
32 debug!("impl_can_satisfy(\
35 impl1_def_id.repr(infcx.tcx),
36 impl2_def_id.repr(infcx.tcx));
38 let param_env = ty::empty_parameter_environment(infcx.tcx);
39 let mut selcx = SelectionContext::intercrate(infcx, ¶m_env);
40 let cause = ObligationCause::dummy();
42 // `impl1` provides an implementation of `Foo<X,Y> for Z`.
44 util::fresh_substs_for_impl(infcx, DUMMY_SP, impl1_def_id);
46 (*ty::impl_trait_ref(infcx.tcx, impl1_def_id).unwrap()).subst(infcx.tcx, &impl1_substs);
48 project::normalize(&mut selcx, cause.clone(), &impl1_trait_ref);
50 // Determine whether `impl2` can provide an implementation for those
52 let obligation = Obligation::new(cause,
53 ty::Binder(ty::TraitPredicate {
54 trait_ref: Rc::new(impl1_trait_ref.value),
56 debug!("impl_can_satisfy(obligation={})", obligation.repr(infcx.tcx));
57 selcx.evaluate_impl(impl2_def_id, &obligation) &&
58 impl1_trait_ref.obligations.iter().all(
59 |o| selcx.evaluate_obligation(o))
62 pub enum OrphanCheckErr<'tcx> {
64 UncoveredTy(Ty<'tcx>),
67 /// Checks the coherence orphan rules. `impl_def_id` should be the
68 /// def-id of a trait impl. To pass, either the trait must be local, or else
69 /// two conditions must be satisfied:
71 /// 1. All type parameters in `Self` must be "covered" by some local type constructor.
72 /// 2. Some local type must appear in `Self`.
73 pub fn orphan_check<'tcx>(tcx: &ty::ctxt<'tcx>,
74 impl_def_id: ast::DefId)
75 -> Result<(), OrphanCheckErr<'tcx>>
77 debug!("impl_is_local({})", impl_def_id.repr(tcx));
79 // We only except this routine to be invoked on implementations
80 // of a trait, not inherent implementations.
81 let trait_ref = ty::impl_trait_ref(tcx, impl_def_id).unwrap();
82 debug!("trait_ref={}", trait_ref.repr(tcx));
84 // If the *trait* is local to the crate, ok.
85 if trait_ref.def_id.krate == ast::LOCAL_CRATE {
86 debug!("trait {} is local to current crate",
87 trait_ref.def_id.repr(tcx));
91 // First, create an ordered iterator over all the type parameters to the trait, with the self
92 // type appearing first.
93 let input_tys = Some(trait_ref.self_ty());
94 let input_tys = input_tys.iter().chain(trait_ref.substs.types.get_slice(TypeSpace).iter());
95 let mut input_tys = input_tys;
97 // Find the first input type that either references a type parameter OR
99 match input_tys.find(|&&input_ty| references_local_or_type_parameter(tcx, input_ty)) {
101 // Within this first type, check that all type parameters are covered by a local
102 // type constructor. Note that if there is no local type constructor, then any
103 // type parameter at all will be an error.
104 let covered_params = type_parameters_covered_by_ty(tcx, input_ty);
105 let all_params = type_parameters_reachable_from_ty(input_ty);
106 for ¶m in all_params.difference(&covered_params) {
107 return Err(OrphanCheckErr::UncoveredTy(param));
111 return Err(OrphanCheckErr::NoLocalInputType);
118 fn ty_is_local_constructor<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
119 debug!("ty_is_local_constructor({})", ty.repr(tcx));
134 ty::ty_projection(..) => {
138 ty::ty_enum(def_id, _) |
139 ty::ty_struct(def_id, _) => {
140 def_id.krate == ast::LOCAL_CRATE
143 ty::ty_uniq(_) => { // treat ~T like Box<T>
144 let krate = tcx.lang_items.owned_box().map(|d| d.krate);
145 krate == Some(ast::LOCAL_CRATE)
148 ty::ty_trait(ref tt) => {
149 tt.principal_def_id().krate == ast::LOCAL_CRATE
157 &format!("ty_is_local invoked on unexpected type: {}",
163 fn type_parameters_covered_by_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
167 if ty_is_local_constructor(tcx, ty) {
168 type_parameters_reachable_from_ty(ty)
170 ty.walk_children().flat_map(|t| type_parameters_covered_by_ty(tcx, t).into_iter()).collect()
174 /// All type parameters reachable from `ty`
175 fn type_parameters_reachable_from_ty<'tcx>(ty: Ty<'tcx>) -> HashSet<Ty<'tcx>> {
176 ty.walk().filter(|&t| is_type_parameter(t)).collect()
179 fn references_local_or_type_parameter<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
180 ty.walk().any(|ty| is_type_parameter(ty) || ty_is_local_constructor(tcx, ty))
183 fn is_type_parameter<'tcx>(ty: Ty<'tcx>) -> bool {
185 // FIXME(#20590) straighten story about projection types
186 ty::ty_projection(..) | ty::ty_param(..) => true,