]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/monomorphize/mod.rs
Hide some lints which are not quite right the way they are reported to the user
[rust.git] / src / librustc_mir / monomorphize / mod.rs
1 // Copyright 2012-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 use rustc::hir::def_id::DefId;
12 use rustc::middle::lang_items::DropInPlaceFnLangItem;
13 use rustc::traits;
14 use rustc::ty::adjustment::CustomCoerceUnsized;
15 use rustc::ty::{self, Ty, TyCtxt};
16
17 pub use rustc::ty::Instance;
18 pub use self::item::{MonoItem, MonoItemExt};
19
20 pub mod collector;
21 pub mod item;
22 pub mod partitioning;
23
24 #[inline(never)] // give this a place in the profiler
25 pub fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>, mono_items: I)
26     where I: Iterator<Item=&'a MonoItem<'tcx>>
27 {
28     let mut symbols: Vec<_> = mono_items.map(|mono_item| {
29         (mono_item, mono_item.symbol_name(tcx))
30     }).collect();
31
32     (&mut symbols[..]).sort_by(|&(_, ref sym1), &(_, ref sym2)|{
33         sym1.cmp(sym2)
34     });
35
36     for pair in (&symbols[..]).windows(2) {
37         let sym1 = &pair[0].1;
38         let sym2 = &pair[1].1;
39
40         if *sym1 == *sym2 {
41             let mono_item1 = pair[0].0;
42             let mono_item2 = pair[1].0;
43
44             let span1 = mono_item1.local_span(tcx);
45             let span2 = mono_item2.local_span(tcx);
46
47             // Deterministically select one of the spans for error reporting
48             let span = match (span1, span2) {
49                 (Some(span1), Some(span2)) => {
50                     Some(if span1.lo().0 > span2.lo().0 {
51                         span1
52                     } else {
53                         span2
54                     })
55                 }
56                 (Some(span), None) |
57                 (None, Some(span)) => Some(span),
58                 _ => None
59             };
60
61             let error_message = format!("symbol `{}` is already defined", sym1);
62
63             if let Some(span) = span {
64                 tcx.sess.span_fatal(span, &error_message)
65             } else {
66                 tcx.sess.fatal(&error_message)
67             }
68         }
69     }
70 }
71
72 fn fn_once_adapter_instance<'a, 'tcx>(
73     tcx: TyCtxt<'a, 'tcx, 'tcx>,
74     closure_did: DefId,
75     substs: ty::ClosureSubsts<'tcx>,
76     ) -> Instance<'tcx> {
77     debug!("fn_once_adapter_shim({:?}, {:?})",
78            closure_did,
79            substs);
80     let fn_once = tcx.lang_items().fn_once_trait().unwrap();
81     let call_once = tcx.associated_items(fn_once)
82         .find(|it| it.kind == ty::AssociatedKind::Method)
83         .unwrap().def_id;
84     let def = ty::InstanceDef::ClosureOnceShim { call_once };
85
86     let self_ty = tcx.mk_closure(closure_did, substs);
87
88     let sig = substs.closure_sig(closure_did, tcx);
89     let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
90     assert_eq!(sig.inputs().len(), 1);
91     let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
92
93     debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
94     Instance { def, substs }
95 }
96
97 fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind,
98                               trait_closure_kind: ty::ClosureKind)
99                               -> Result<bool, ()>
100 {
101     match (actual_closure_kind, trait_closure_kind) {
102         (ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
103         (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
104         (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
105             // No adapter needed.
106            Ok(false)
107         }
108         (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
109             // The closure fn `llfn` is a `fn(&self, ...)`.  We want a
110             // `fn(&mut self, ...)`. In fact, at codegen time, these are
111             // basically the same thing, so we can just return llfn.
112             Ok(false)
113         }
114         (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
115         (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
116             // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
117             // self, ...)`.  We want a `fn(self, ...)`. We can produce
118             // this by doing something like:
119             //
120             //     fn call_once(self, ...) { call_mut(&self, ...) }
121             //     fn call_once(mut self, ...) { call_mut(&mut self, ...) }
122             //
123             // These are both the same at codegen time.
124             Ok(true)
125         }
126         _ => Err(()),
127     }
128 }
129
130 pub fn resolve_closure<'a, 'tcx> (
131     tcx: TyCtxt<'a, 'tcx, 'tcx>,
132     def_id: DefId,
133     substs: ty::ClosureSubsts<'tcx>,
134     requested_kind: ty::ClosureKind)
135     -> Instance<'tcx>
136 {
137     let actual_kind = substs.closure_kind(def_id, tcx);
138
139     match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
140         Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),
141         _ => Instance::new(def_id, substs.substs)
142     }
143 }
144
145 pub fn resolve_drop_in_place<'a, 'tcx>(
146     tcx: TyCtxt<'a, 'tcx, 'tcx>,
147     ty: Ty<'tcx>)
148     -> ty::Instance<'tcx>
149 {
150     let def_id = tcx.require_lang_item(DropInPlaceFnLangItem);
151     let substs = tcx.intern_substs(&[ty.into()]);
152     Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap()
153 }
154
155 pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
156                                            source_ty: Ty<'tcx>,
157                                            target_ty: Ty<'tcx>)
158                                            -> CustomCoerceUnsized {
159     let def_id = tcx.lang_items().coerce_unsized_trait().unwrap();
160
161     let trait_ref = ty::Binder::bind(ty::TraitRef {
162         def_id: def_id,
163         substs: tcx.mk_substs_trait(source_ty, &[target_ty.into()])
164     });
165
166     match tcx.codegen_fulfill_obligation( (ty::ParamEnv::reveal_all(), trait_ref)) {
167         traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => {
168             tcx.coerce_unsized_info(impl_def_id).custom_kind.unwrap()
169         }
170         vtable => {
171             bug!("invalid CoerceUnsized vtable: {:?}", vtable);
172         }
173     }
174 }