]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/blanket_impl.rs
Rollup merge of #55343 - Keruspe:remap-debuginfo-release, r=alexcrichton
[rust.git] / src / librustdoc / clean / blanket_impl.rs
1 // Copyright 2018 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 rustc::hir;
12 use rustc::traits;
13 use rustc::ty::ToPredicate;
14 use rustc::ty::subst::Subst;
15 use rustc::infer::InferOk;
16 use syntax_pos::DUMMY_SP;
17
18 use core::DocAccessLevels;
19
20 use super::*;
21
22 use self::def_ctor::{get_def_from_def_id, get_def_from_node_id};
23
24 pub struct BlanketImplFinder<'a, 'tcx: 'a, 'rcx: 'a, 'cstore: 'rcx> {
25     pub cx: &'a core::DocContext<'a, 'tcx, 'rcx, 'cstore>,
26 }
27
28 impl<'a, 'tcx, 'rcx, 'cstore> BlanketImplFinder <'a, 'tcx, 'rcx, 'cstore> {
29     pub fn new(cx: &'a core::DocContext<'a, 'tcx, 'rcx, 'cstore>) -> Self {
30         BlanketImplFinder { cx }
31     }
32
33     pub fn get_with_def_id(&self, def_id: DefId) -> Vec<Item> {
34         get_def_from_def_id(&self.cx, def_id, &|def_ctor| {
35             self.get_blanket_impls(def_id, &def_ctor, None)
36         })
37     }
38
39     pub fn get_with_node_id(&self, id: ast::NodeId, name: String) -> Vec<Item> {
40         get_def_from_node_id(&self.cx, id, name, &|def_ctor, name| {
41             let did = self.cx.tcx.hir.local_def_id(id);
42             self.get_blanket_impls(did, &def_ctor, Some(name))
43         })
44     }
45
46     pub fn get_blanket_impls<F>(
47         &self,
48         def_id: DefId,
49         def_ctor: &F,
50         name: Option<String>,
51     ) -> Vec<Item>
52     where F: Fn(DefId) -> Def {
53         debug!("get_blanket_impls(def_id={:?}, ...)", def_id);
54         let mut impls = Vec::new();
55         if self.cx
56             .tcx
57             .get_attrs(def_id)
58             .lists("doc")
59             .has_word("hidden")
60         {
61             debug!(
62                 "get_blanket_impls(def_id={:?}, def_ctor=...): item has doc('hidden'), \
63                  aborting",
64                 def_id
65             );
66             return impls;
67         }
68         let ty = self.cx.tcx.type_of(def_id);
69         let generics = self.cx.tcx.generics_of(def_id);
70         let real_name = name.clone().map(|name| Ident::from_str(&name));
71         let param_env = self.cx.tcx.param_env(def_id);
72         for &trait_def_id in self.cx.all_traits.iter() {
73             if !self.cx.renderinfo.borrow().access_levels.is_doc_reachable(trait_def_id) ||
74                self.cx.generated_synthetics
75                       .borrow_mut()
76                       .get(&(def_id, trait_def_id))
77                       .is_some() {
78                 continue
79             }
80             self.cx.tcx.for_each_relevant_impl(trait_def_id, ty, |impl_def_id| {
81                 self.cx.tcx.infer_ctxt().enter(|infcx| {
82                     debug!("get_blanet_impls: Considering impl for trait '{:?}' {:?}",
83                            trait_def_id, impl_def_id);
84                     let t_generics = infcx.tcx.generics_of(impl_def_id);
85                     let trait_ref = infcx.tcx.impl_trait_ref(impl_def_id)
86                                              .expect("Cannot get impl trait");
87
88                     match trait_ref.self_ty().sty {
89                         ty::Param(_) => {},
90                         _ => return,
91                     }
92
93                     let substs = infcx.fresh_substs_for_item(DUMMY_SP, def_id);
94                     let ty = ty.subst(infcx.tcx, substs);
95                     let param_env = param_env.subst(infcx.tcx, substs);
96
97                     let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
98                     let trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
99
100                     // Require the type the impl is implemented on to match
101                     // our type, and ignore the impl if there was a mismatch.
102                     let cause = traits::ObligationCause::dummy();
103                     let eq_result = infcx.at(&cause, param_env)
104                                          .eq(trait_ref.self_ty(), ty);
105                     if let Ok(InferOk { value: (), obligations }) = eq_result {
106                         // FIXME(eddyb) ignoring `obligations` might cause false positives.
107                         drop(obligations);
108
109                         debug!(
110                             "invoking predicate_may_hold: param_env={:?}, trait_ref={:?}, ty={:?}",
111                              param_env, trait_ref, ty
112                         );
113                         let may_apply = match infcx.evaluate_obligation(
114                             &traits::Obligation::new(
115                                 cause.clone(),
116                                 param_env,
117                                 trait_ref.to_predicate(),
118                             ),
119                         ) {
120                             Ok(eval_result) => eval_result.may_apply(),
121                             Err(traits::OverflowError) => true, // overflow doesn't mean yes *or* no
122                         };
123                         debug!("get_blanket_impls: found applicable impl: {}\
124                                for trait_ref={:?}, ty={:?}",
125                                may_apply, trait_ref, ty);
126
127                         if !may_apply {
128                             return
129                         }
130                         self.cx.generated_synthetics.borrow_mut()
131                                                     .insert((def_id, trait_def_id));
132                         let trait_ = hir::TraitRef {
133                             path: get_path_for_type(infcx.tcx,
134                                                     trait_def_id,
135                                                     hir::def::Def::Trait),
136                             ref_id: ast::DUMMY_NODE_ID,
137                             hir_ref_id: hir::DUMMY_HIR_ID,
138                         };
139                         let provided_trait_methods =
140                             infcx.tcx.provided_trait_methods(trait_def_id)
141                                      .into_iter()
142                                      .map(|meth| meth.ident.to_string())
143                                      .collect();
144
145                         let ty = self.cx.get_real_ty(def_id, def_ctor, &real_name, generics);
146                         let predicates = infcx.tcx.predicates_of(impl_def_id);
147
148                         impls.push(Item {
149                             source: infcx.tcx.def_span(impl_def_id).clean(self.cx),
150                             name: None,
151                             attrs: Default::default(),
152                             visibility: None,
153                             def_id: self.cx.next_def_id(impl_def_id.krate),
154                             stability: None,
155                             deprecation: None,
156                             inner: ImplItem(Impl {
157                                 unsafety: hir::Unsafety::Normal,
158                                 generics: (t_generics, &predicates).clean(self.cx),
159                                 provided_trait_methods,
160                                 trait_: Some(trait_.clean(self.cx)),
161                                 for_: ty.clean(self.cx),
162                                 items: infcx.tcx.associated_items(impl_def_id)
163                                                 .collect::<Vec<_>>()
164                                                 .clean(self.cx),
165                                 polarity: None,
166                                 synthetic: false,
167                                 blanket_impl: Some(infcx.tcx.type_of(impl_def_id)
168                                                             .clean(self.cx)),
169                             }),
170                         });
171                     }
172                 });
173             });
174         }
175         impls
176     }
177 }