]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/traits/coherence.rs
rollup merge of #20594: nikomatsakis/orphan-ordered
[rust.git] / src / librustc / middle / traits / coherence.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 //! See `doc.rs` for high-level documentation
12
13 use super::SelectionContext;
14 use super::{Obligation, ObligationCause};
15 use super::util;
16
17 use middle::subst::{Subst};
18 use middle::ty::{self, Ty};
19 use middle::infer::InferCtxt;
20 use std::collections::HashSet;
21 use std::rc::Rc;
22 use syntax::ast;
23 use syntax::codemap::DUMMY_SP;
24 use util::ppaux::Repr;
25
26 pub fn impl_can_satisfy(infcx: &InferCtxt,
27                         impl1_def_id: ast::DefId,
28                         impl2_def_id: ast::DefId)
29                         -> bool
30 {
31     debug!("impl_can_satisfy(\
32            impl1_def_id={}, \
33            impl2_def_id={})",
34            impl1_def_id.repr(infcx.tcx),
35            impl2_def_id.repr(infcx.tcx));
36
37     // `impl1` provides an implementation of `Foo<X,Y> for Z`.
38     let impl1_substs =
39         util::fresh_substs_for_impl(infcx, DUMMY_SP, impl1_def_id);
40     let impl1_trait_ref =
41         (*ty::impl_trait_ref(infcx.tcx, impl1_def_id).unwrap()).subst(infcx.tcx, &impl1_substs);
42
43     // Determine whether `impl2` can provide an implementation for those
44     // same types.
45     let param_env = ty::empty_parameter_environment(infcx.tcx);
46     let mut selcx = SelectionContext::intercrate(infcx, &param_env);
47     let obligation = Obligation::new(ObligationCause::dummy(),
48                                      ty::Binder(ty::TraitPredicate {
49                                          trait_ref: Rc::new(impl1_trait_ref),
50                                      }));
51     debug!("impl_can_satisfy(obligation={})", obligation.repr(infcx.tcx));
52     selcx.evaluate_impl(impl2_def_id, &obligation)
53 }
54
55 #[allow(missing_copy_implementations)]
56 pub enum OrphanCheckErr<'tcx> {
57     NoLocalInputType,
58     UncoveredTy(Ty<'tcx>),
59 }
60
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:
64 ///
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>>
70 {
71     debug!("impl_is_local({})", impl_def_id.repr(tcx));
72
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));
77
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));
82         return Ok(());
83     }
84
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 &param in all_params.difference(&covered_params) {
89         return Err(OrphanCheckErr::UncoveredTy(param));
90     }
91
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);
95     }
96
97     return Ok(());
98 }
99
100 fn ty_is_local_constructor<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool {
101     debug!("ty_is_local_constructor({})", ty.repr(tcx));
102
103     match ty.sty {
104         ty::ty_bool |
105         ty::ty_char |
106         ty::ty_int(..) |
107         ty::ty_uint(..) |
108         ty::ty_float(..) |
109         ty::ty_str(..) |
110         ty::ty_bare_fn(..) |
111         ty::ty_vec(..) |
112         ty::ty_ptr(..) |
113         ty::ty_rptr(..) |
114         ty::ty_tup(..) |
115         ty::ty_param(..) |
116         ty::ty_projection(..) => {
117             false
118         }
119
120         ty::ty_enum(def_id, _) |
121         ty::ty_struct(def_id, _) => {
122             def_id.krate == ast::LOCAL_CRATE
123         }
124
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)
128         }
129
130         ty::ty_trait(ref tt) => {
131             tt.principal_def_id().krate == ast::LOCAL_CRATE
132         }
133
134         ty::ty_unboxed_closure(..) |
135         ty::ty_infer(..) |
136         ty::ty_open(..) |
137         ty::ty_err => {
138             tcx.sess.bug(
139                 format!("ty_is_local invoked on unexpected type: {}",
140                         ty.repr(tcx))[])
141         }
142     }
143 }
144
145 fn type_parameters_covered_by_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
146                                        ty: Ty<'tcx>)
147                                        -> HashSet<Ty<'tcx>>
148 {
149     if ty_is_local_constructor(tcx, ty) {
150         type_parameters_reachable_from_ty(ty)
151     } else {
152         ty.walk_children().flat_map(|t| type_parameters_covered_by_ty(tcx, t).into_iter()).collect()
153     }
154 }
155
156 /// All type parameters reachable from `ty`
157 fn type_parameters_reachable_from_ty<'tcx>(ty: Ty<'tcx>) -> HashSet<Ty<'tcx>> {
158     ty.walk()
159         .filter(|&t| {
160             match t.sty {
161                 // FIXME(#20590) straighten story about projection types
162                 ty::ty_projection(..) | ty::ty_param(..) => true,
163                 _ => false,
164             }
165         })
166         .collect()
167 }