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