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};
17 use middle::subst::{Subst};
18 use middle::ty::{self, Ty};
19 use middle::infer::InferCtxt;
20 use std::collections::HashSet;
23 use syntax::codemap::DUMMY_SP;
24 use util::ppaux::Repr;
26 pub fn impl_can_satisfy(infcx: &InferCtxt,
27 impl1_def_id: ast::DefId,
28 impl2_def_id: ast::DefId)
31 debug!("impl_can_satisfy(\
34 impl1_def_id.repr(infcx.tcx),
35 impl2_def_id.repr(infcx.tcx));
37 // `impl1` provides an implementation of `Foo<X,Y> for Z`.
39 util::fresh_substs_for_impl(infcx, DUMMY_SP, impl1_def_id);
41 (*ty::impl_trait_ref(infcx.tcx, impl1_def_id).unwrap()).subst(infcx.tcx, &impl1_substs);
43 // Determine whether `impl2` can provide an implementation for those
45 let param_env = ty::empty_parameter_environment(infcx.tcx);
46 let mut selcx = SelectionContext::intercrate(infcx, ¶m_env);
47 let obligation = Obligation::new(ObligationCause::dummy(),
48 ty::Binder(ty::TraitPredicate {
49 trait_ref: Rc::new(impl1_trait_ref),
51 debug!("impl_can_satisfy(obligation={})", obligation.repr(infcx.tcx));
52 selcx.evaluate_impl(impl2_def_id, &obligation)
55 #[allow(missing_copy_implementations)]
56 pub enum OrphanCheckErr<'tcx> {
58 UncoveredTy(Ty<'tcx>),
61 /// Checks the coherence orphan rules. `impl_def_id` should be the
62 /// def-id of a trait impl. To pass, either the trait must be local, or else
63 /// two conditions must be satisfied:
65 /// 1. All type parameters in `Self` must be "covered" by some local type constructor.
66 /// 2. Some local type must appear in `Self`.
67 pub fn orphan_check<'tcx>(tcx: &ty::ctxt<'tcx>,
68 impl_def_id: ast::DefId)
69 -> Result<(), OrphanCheckErr<'tcx>>
71 debug!("impl_is_local({})", impl_def_id.repr(tcx));
73 // We only except this routine to be invoked on implementations
74 // of a trait, not inherent implementations.
75 let trait_ref = ty::impl_trait_ref(tcx, impl_def_id).unwrap();
76 debug!("trait_ref={}", trait_ref.repr(tcx));
78 // If the *trait* is local to the crate, ok.
79 if trait_ref.def_id.krate == ast::LOCAL_CRATE {
80 debug!("trait {} is local to current crate",
81 trait_ref.def_id.repr(tcx));
85 // Otherwise, check that (1) all type parameters are covered.
86 let covered_params = type_parameters_covered_by_ty(tcx, trait_ref.self_ty());
87 let all_params = type_parameters_reachable_from_ty(trait_ref.self_ty());
88 for ¶m in all_params.difference(&covered_params) {
89 return Err(OrphanCheckErr::UncoveredTy(param));
92 // And (2) some local type appears.
93 if !trait_ref.self_ty().walk().any(|t| ty_is_local_constructor(tcx, t)) {
94 return Err(OrphanCheckErr::NoLocalInputType);
100 fn ty_is_local_constructor<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
101 debug!("ty_is_local_constructor({})", ty.repr(tcx));
116 ty::ty_projection(..) => {
120 ty::ty_enum(def_id, _) |
121 ty::ty_struct(def_id, _) => {
122 def_id.krate == ast::LOCAL_CRATE
125 ty::ty_uniq(_) => { // treat ~T like Box<T>
126 let krate = tcx.lang_items.owned_box().map(|d| d.krate);
127 krate == Some(ast::LOCAL_CRATE)
130 ty::ty_trait(ref tt) => {
131 tt.principal_def_id().krate == ast::LOCAL_CRATE
134 ty::ty_unboxed_closure(..) |
139 format!("ty_is_local invoked on unexpected type: {}",
145 fn type_parameters_covered_by_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
149 if ty_is_local_constructor(tcx, ty) {
150 type_parameters_reachable_from_ty(ty)
152 ty.walk_children().flat_map(|t| type_parameters_covered_by_ty(tcx, t).into_iter()).collect()
156 /// All type parameters reachable from `ty`
157 fn type_parameters_reachable_from_ty<'tcx>(ty: Ty<'tcx>) -> HashSet<Ty<'tcx>> {
161 // FIXME(#20590) straighten story about projection types
162 ty::ty_projection(..) | ty::ty_param(..) => true,