]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/trait_def.rs
Replace Rc with Lrc for shared data
[rust.git] / src / librustc / ty / trait_def.rs
1 // Copyright 2012-2015 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 use hir;
12 use hir::def_id::DefId;
13 use hir::map::DefPathHash;
14 use ich::{self, StableHashingContext};
15 use traits::specialization_graph;
16 use ty::fast_reject;
17 use ty::fold::TypeFoldable;
18 use ty::{Ty, TyCtxt};
19
20 use rustc_data_structures::fx::FxHashMap;
21 use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
22                                            StableHasherResult};
23 use rustc_data_structures::sync::Lrc;
24
25 /// A trait's definition with type information.
26 pub struct TraitDef {
27     pub def_id: DefId,
28
29     pub unsafety: hir::Unsafety,
30
31     /// If `true`, then this trait had the `#[rustc_paren_sugar]`
32     /// attribute, indicating that it should be used with `Foo()`
33     /// sugar. This is a temporary thing -- eventually any trait will
34     /// be usable with the sugar (or without it).
35     pub paren_sugar: bool,
36
37     pub has_auto_impl: bool,
38
39     /// The ICH of this trait's DefPath, cached here so it doesn't have to be
40     /// recomputed all the time.
41     pub def_path_hash: DefPathHash,
42 }
43
44 pub struct TraitImpls {
45     blanket_impls: Vec<DefId>,
46     /// Impls indexed by their simplified self-type, for fast lookup.
47     non_blanket_impls: FxHashMap<fast_reject::SimplifiedType, Vec<DefId>>,
48 }
49
50 impl<'a, 'gcx, 'tcx> TraitDef {
51     pub fn new(def_id: DefId,
52                unsafety: hir::Unsafety,
53                paren_sugar: bool,
54                has_auto_impl: bool,
55                def_path_hash: DefPathHash)
56                -> TraitDef {
57         TraitDef {
58             def_id,
59             paren_sugar,
60             unsafety,
61             has_auto_impl,
62             def_path_hash,
63         }
64     }
65
66     pub fn ancestors(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
67                      of_impl: DefId)
68                      -> specialization_graph::Ancestors {
69         specialization_graph::ancestors(tcx, self.def_id, of_impl)
70     }
71 }
72
73 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
74     pub fn for_each_impl<F: FnMut(DefId)>(self, def_id: DefId, mut f: F) {
75         let impls = self.trait_impls_of(def_id);
76
77         for &impl_def_id in impls.blanket_impls.iter() {
78             f(impl_def_id);
79         }
80
81         for v in impls.non_blanket_impls.values() {
82             for &impl_def_id in v {
83                 f(impl_def_id);
84             }
85         }
86     }
87
88     /// Iterate over every impl that could possibly match the
89     /// self-type `self_ty`.
90     pub fn for_each_relevant_impl<F: FnMut(DefId)>(self,
91                                                    def_id: DefId,
92                                                    self_ty: Ty<'tcx>,
93                                                    mut f: F)
94     {
95         let impls = self.trait_impls_of(def_id);
96
97         for &impl_def_id in impls.blanket_impls.iter() {
98             f(impl_def_id);
99         }
100
101         // simplify_type(.., false) basically replaces type parameters and
102         // projections with infer-variables. This is, of course, done on
103         // the impl trait-ref when it is instantiated, but not on the
104         // predicate trait-ref which is passed here.
105         //
106         // for example, if we match `S: Copy` against an impl like
107         // `impl<T:Copy> Copy for Option<T>`, we replace the type variable
108         // in `Option<T>` with an infer variable, to `Option<_>` (this
109         // doesn't actually change fast_reject output), but we don't
110         // replace `S` with anything - this impl of course can't be
111         // selected, and as there are hundreds of similar impls,
112         // considering them would significantly harm performance.
113
114         // This depends on the set of all impls for the trait. That is
115         // unfortunate. When we get red-green recompilation, we would like
116         // to have a way of knowing whether the set of relevant impls
117         // changed. The most naive
118         // way would be to compute the Vec of relevant impls and see whether
119         // it differs between compilations. That shouldn't be too slow by
120         // itself - we do quite a bit of work for each relevant impl anyway.
121         //
122         // If we want to be faster, we could have separate queries for
123         // blanket and non-blanket impls, and compare them separately.
124         //
125         // I think we'll cross that bridge when we get to it.
126         if let Some(simp) = fast_reject::simplify_type(self, self_ty, true) {
127             if let Some(impls) = impls.non_blanket_impls.get(&simp) {
128                 for &impl_def_id in impls {
129                     f(impl_def_id);
130                 }
131             }
132         } else {
133             for v in impls.non_blanket_impls.values() {
134                 for &impl_def_id in v {
135                     f(impl_def_id);
136                 }
137             }
138         }
139     }
140 }
141
142 // Query provider for `trait_impls_of`.
143 pub(super) fn trait_impls_of_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
144                                                 trait_id: DefId)
145                                                 -> Lrc<TraitImpls> {
146     let mut remote_impls = Vec::new();
147
148     // Traits defined in the current crate can't have impls in upstream
149     // crates, so we don't bother querying the cstore.
150     if !trait_id.is_local() {
151         for &cnum in tcx.crates().iter() {
152             let impls = tcx.implementations_of_trait((cnum, trait_id));
153             remote_impls.extend(impls.iter().cloned());
154         }
155     }
156
157     let mut blanket_impls = Vec::new();
158     let mut non_blanket_impls = FxHashMap();
159
160     let local_impls = tcx.hir
161                          .trait_impls(trait_id)
162                          .into_iter()
163                          .map(|&node_id| tcx.hir.local_def_id(node_id));
164
165      for impl_def_id in local_impls.chain(remote_impls.into_iter()) {
166         let impl_self_ty = tcx.type_of(impl_def_id);
167         if impl_def_id.is_local() && impl_self_ty.references_error() {
168             continue
169         }
170
171         if let Some(simplified_self_ty) =
172             fast_reject::simplify_type(tcx, impl_self_ty, false)
173         {
174             non_blanket_impls
175                 .entry(simplified_self_ty)
176                 .or_insert(vec![])
177                 .push(impl_def_id);
178         } else {
179             blanket_impls.push(impl_def_id);
180         }
181     }
182
183     Lrc::new(TraitImpls {
184         blanket_impls: blanket_impls,
185         non_blanket_impls: non_blanket_impls,
186     })
187 }
188
189 impl<'gcx> HashStable<StableHashingContext<'gcx>> for TraitImpls {
190     fn hash_stable<W: StableHasherResult>(&self,
191                                           hcx: &mut StableHashingContext<'gcx>,
192                                           hasher: &mut StableHasher<W>) {
193         let TraitImpls {
194             ref blanket_impls,
195             ref non_blanket_impls,
196         } = *self;
197
198         ich::hash_stable_trait_impls(hcx, hasher, blanket_impls, non_blanket_impls);
199     }
200 }