]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/instance.rs
Rollup merge of #66325 - BartMassey:master, r=joshtriplett
[rust.git] / src / librustc / ty / instance.rs
1 use crate::hir::CodegenFnAttrFlags;
2 use crate::hir::def::Namespace;
3 use crate::hir::def_id::DefId;
4 use crate::ty::{self, Ty, TypeFoldable, SubstsRef, TyCtxt};
5 use crate::ty::print::{FmtPrinter, Printer};
6 use crate::traits;
7 use crate::middle::lang_items::DropInPlaceFnLangItem;
8 use rustc_target::spec::abi::Abi;
9 use rustc_macros::HashStable;
10
11 use std::fmt;
12
13 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
14 #[derive(HashStable, Lift)]
15 pub struct Instance<'tcx> {
16     pub def: InstanceDef<'tcx>,
17     pub substs: SubstsRef<'tcx>,
18 }
19
20 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)]
21 pub enum InstanceDef<'tcx> {
22     Item(DefId),
23     Intrinsic(DefId),
24
25     /// `<T as Trait>::method` where `method` receives unsizeable `self: Self`.
26     VtableShim(DefId),
27
28     /// `fn()` pointer where the function itself cannot be turned into a pointer.
29     ///
30     /// One example is `<dyn Trait as Trait>::fn`, where the shim contains
31     /// a virtual call, which codegen supports only via a direct call to the
32     /// `<dyn Trait as Trait>::fn` instance (an `InstanceDef::Virtual`).
33     ///
34     /// Another example is functions annotated with `#[track_caller]`, which
35     /// must have their implicit caller location argument populated for a call.
36     /// Because this is a required part of the function's ABI but can't be tracked
37     /// as a property of the function pointer, we use a single "caller location"
38     /// (the definition of the function itself).
39     ReifyShim(DefId),
40
41     /// `<fn() as FnTrait>::call_*`
42     /// `DefId` is `FnTrait::call_*`.
43     FnPtrShim(DefId, Ty<'tcx>),
44
45     /// `<dyn Trait as Trait>::fn`, "direct calls" of which are implicitly
46     /// codegen'd as virtual calls.
47     ///
48     /// NB: if this is reified to a `fn` pointer, a `ReifyShim` is used
49     /// (see `ReifyShim` above for more details on that).
50     Virtual(DefId, usize),
51
52     /// `<[mut closure] as FnOnce>::call_once`
53     ClosureOnceShim { call_once: DefId },
54
55     /// `drop_in_place::<T>; None` for empty drop glue.
56     DropGlue(DefId, Option<Ty<'tcx>>),
57
58     ///`<T as Clone>::clone` shim.
59     CloneShim(DefId, Ty<'tcx>),
60 }
61
62 impl<'tcx> Instance<'tcx> {
63     pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
64         let ty = tcx.type_of(self.def.def_id());
65         tcx.subst_and_normalize_erasing_regions(
66             self.substs,
67             ty::ParamEnv::reveal_all(),
68             &ty,
69         )
70     }
71 }
72
73 impl<'tcx> InstanceDef<'tcx> {
74     #[inline]
75     pub fn def_id(&self) -> DefId {
76         match *self {
77             InstanceDef::Item(def_id) |
78             InstanceDef::VtableShim(def_id) |
79             InstanceDef::ReifyShim(def_id) |
80             InstanceDef::FnPtrShim(def_id, _) |
81             InstanceDef::Virtual(def_id, _) |
82             InstanceDef::Intrinsic(def_id, ) |
83             InstanceDef::ClosureOnceShim { call_once: def_id } |
84             InstanceDef::DropGlue(def_id, _) |
85             InstanceDef::CloneShim(def_id, _) => def_id
86         }
87     }
88
89     #[inline]
90     pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
91         tcx.get_attrs(self.def_id())
92     }
93
94     pub fn is_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
95         use crate::hir::map::DefPathData;
96         let def_id = match *self {
97             ty::InstanceDef::Item(def_id) => def_id,
98             ty::InstanceDef::DropGlue(_, Some(_)) => return false,
99             _ => return true
100         };
101         match tcx.def_key(def_id).disambiguated_data.data {
102             DefPathData::Ctor | DefPathData::ClosureExpr => true,
103             _ => false
104         }
105     }
106
107     pub fn requires_local(&self, tcx: TyCtxt<'tcx>) -> bool {
108         if self.is_inline(tcx) {
109             return true
110         }
111         if let ty::InstanceDef::DropGlue(..) = *self {
112             // Drop glue wants to be instantiated at every codegen
113             // unit, but without an #[inline] hint. We should make this
114             // available to normal end-users.
115             return true
116         }
117         tcx.codegen_fn_attrs(self.def_id()).requests_inline()
118     }
119
120     pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool {
121         tcx.codegen_fn_attrs(self.def_id()).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
122     }
123 }
124
125 impl<'tcx> fmt::Display for Instance<'tcx> {
126     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127         ty::tls::with(|tcx| {
128             let substs = tcx.lift(&self.substs).expect("could not lift for printing");
129             FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
130                 .print_def_path(self.def_id(), substs)?;
131             Ok(())
132         })?;
133
134         match self.def {
135             InstanceDef::Item(_) => Ok(()),
136             InstanceDef::VtableShim(_) => {
137                 write!(f, " - shim(vtable)")
138             }
139             InstanceDef::ReifyShim(_) => {
140                 write!(f, " - shim(reify)")
141             }
142             InstanceDef::Intrinsic(_) => {
143                 write!(f, " - intrinsic")
144             }
145             InstanceDef::Virtual(_, num) => {
146                 write!(f, " - virtual#{}", num)
147             }
148             InstanceDef::FnPtrShim(_, ty) => {
149                 write!(f, " - shim({:?})", ty)
150             }
151             InstanceDef::ClosureOnceShim { .. } => {
152                 write!(f, " - shim")
153             }
154             InstanceDef::DropGlue(_, ty) => {
155                 write!(f, " - shim({:?})", ty)
156             }
157             InstanceDef::CloneShim(_, ty) => {
158                 write!(f, " - shim({:?})", ty)
159             }
160         }
161     }
162 }
163
164 impl<'tcx> Instance<'tcx> {
165     pub fn new(def_id: DefId, substs: SubstsRef<'tcx>)
166                -> Instance<'tcx> {
167         assert!(!substs.has_escaping_bound_vars(),
168                 "substs of instance {:?} not normalized for codegen: {:?}",
169                 def_id, substs);
170         Instance { def: InstanceDef::Item(def_id), substs: substs }
171     }
172
173     pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
174         Instance::new(def_id, tcx.empty_substs_for_def_id(def_id))
175     }
176
177     #[inline]
178     pub fn def_id(&self) -> DefId {
179         self.def.def_id()
180     }
181
182     /// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly,
183     /// this is used to find the precise code that will run for a trait method invocation,
184     /// if known.
185     ///
186     /// Returns `None` if we cannot resolve `Instance` to a specific instance.
187     /// For example, in a context like this,
188     ///
189     /// ```
190     /// fn foo<T: Debug>(t: T) { ... }
191     /// ```
192     ///
193     /// trying to resolve `Debug::fmt` applied to `T` will yield `None`, because we do not
194     /// know what code ought to run. (Note that this setting is also affected by the
195     /// `RevealMode` in the parameter environment.)
196     ///
197     /// Presuming that coherence and type-check have succeeded, if this method is invoked
198     /// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
199     /// `Some`.
200     pub fn resolve(
201         tcx: TyCtxt<'tcx>,
202         param_env: ty::ParamEnv<'tcx>,
203         def_id: DefId,
204         substs: SubstsRef<'tcx>,
205     ) -> Option<Instance<'tcx>> {
206         debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
207         let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
208             debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
209             let item = tcx.associated_item(def_id);
210             resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
211         } else {
212             let ty = tcx.type_of(def_id);
213             let item_type = tcx.subst_and_normalize_erasing_regions(
214                 substs,
215                 param_env,
216                 &ty,
217             );
218
219             let def = match item_type.kind {
220                 ty::FnDef(..) if {
221                     let f = item_type.fn_sig(tcx);
222                     f.abi() == Abi::RustIntrinsic ||
223                         f.abi() == Abi::PlatformIntrinsic
224                 } =>
225                 {
226                     debug!(" => intrinsic");
227                     ty::InstanceDef::Intrinsic(def_id)
228                 }
229                 _ => {
230                     if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
231                         let ty = substs.type_at(0);
232                         if ty.needs_drop(tcx, ty::ParamEnv::reveal_all()) {
233                             debug!(" => nontrivial drop glue");
234                             ty::InstanceDef::DropGlue(def_id, Some(ty))
235                         } else {
236                             debug!(" => trivial drop glue");
237                             ty::InstanceDef::DropGlue(def_id, None)
238                         }
239                     } else {
240                         debug!(" => free item");
241                         ty::InstanceDef::Item(def_id)
242                     }
243                 }
244             };
245             Some(Instance {
246                 def: def,
247                 substs: substs
248             })
249         };
250         debug!("resolve(def_id={:?}, substs={:?}) = {:?}", def_id, substs, result);
251         result
252     }
253
254     pub fn resolve_for_fn_ptr(
255         tcx: TyCtxt<'tcx>,
256         param_env: ty::ParamEnv<'tcx>,
257         def_id: DefId,
258         substs: SubstsRef<'tcx>,
259     ) -> Option<Instance<'tcx>> {
260         debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
261         Instance::resolve(tcx, param_env, def_id, substs).map(|mut resolved| {
262             match resolved.def {
263                 InstanceDef::Item(def_id) if resolved.def.requires_caller_location(tcx) => {
264                     debug!(" => fn pointer created for function with #[track_caller]");
265                     resolved.def = InstanceDef::ReifyShim(def_id);
266                 }
267                 InstanceDef::Virtual(def_id, _) => {
268                     debug!(" => fn pointer created for virtual call");
269                     resolved.def = InstanceDef::ReifyShim(def_id);
270                 }
271                 _ => {}
272             }
273
274             resolved
275         })
276     }
277
278     pub fn resolve_for_vtable(
279         tcx: TyCtxt<'tcx>,
280         param_env: ty::ParamEnv<'tcx>,
281         def_id: DefId,
282         substs: SubstsRef<'tcx>,
283     ) -> Option<Instance<'tcx>> {
284         debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
285         let fn_sig = tcx.fn_sig(def_id);
286         let is_vtable_shim = fn_sig.inputs().skip_binder().len() > 0
287             && fn_sig.input(0).skip_binder().is_param(0)
288             && tcx.generics_of(def_id).has_self;
289         if is_vtable_shim {
290             debug!(" => associated item with unsizeable self: Self");
291             Some(Instance {
292                 def: InstanceDef::VtableShim(def_id),
293                 substs,
294             })
295         } else {
296             Instance::resolve(tcx, param_env, def_id, substs)
297         }
298     }
299
300     pub fn resolve_closure(
301         tcx: TyCtxt<'tcx>,
302         def_id: DefId,
303         substs: ty::SubstsRef<'tcx>,
304         requested_kind: ty::ClosureKind,
305     ) -> Instance<'tcx> {
306         let actual_kind = substs.as_closure().kind(def_id, tcx);
307
308         match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
309             Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
310             _ => Instance::new(def_id, substs)
311         }
312     }
313
314     pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
315         let def_id = tcx.require_lang_item(DropInPlaceFnLangItem, None);
316         let substs = tcx.intern_substs(&[ty.into()]);
317         Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap()
318     }
319
320     pub fn fn_once_adapter_instance(
321         tcx: TyCtxt<'tcx>,
322         closure_did: DefId,
323         substs: ty::SubstsRef<'tcx>,
324     ) -> Instance<'tcx> {
325         debug!("fn_once_adapter_shim({:?}, {:?})",
326                closure_did,
327                substs);
328         let fn_once = tcx.lang_items().fn_once_trait().unwrap();
329         let call_once = tcx.associated_items(fn_once)
330             .find(|it| it.kind == ty::AssocKind::Method)
331             .unwrap().def_id;
332         let def = ty::InstanceDef::ClosureOnceShim { call_once };
333
334         let self_ty = tcx.mk_closure(closure_did, substs);
335
336         let sig = substs.as_closure().sig(closure_did, tcx);
337         let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
338         assert_eq!(sig.inputs().len(), 1);
339         let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
340
341         debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
342         Instance { def, substs }
343     }
344
345     pub fn is_vtable_shim(&self) -> bool {
346         if let InstanceDef::VtableShim(..) = self.def {
347             true
348         } else {
349             false
350         }
351     }
352 }
353
354 fn resolve_associated_item<'tcx>(
355     tcx: TyCtxt<'tcx>,
356     trait_item: &ty::AssocItem,
357     param_env: ty::ParamEnv<'tcx>,
358     trait_id: DefId,
359     rcvr_substs: SubstsRef<'tcx>,
360 ) -> Option<Instance<'tcx>> {
361     let def_id = trait_item.def_id;
362     debug!("resolve_associated_item(trait_item={:?}, \
363             param_env={:?}, \
364             trait_id={:?}, \
365             rcvr_substs={:?})",
366             def_id, param_env, trait_id, rcvr_substs);
367
368     let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
369     let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref)));
370
371     // Now that we know which impl is being used, we can dispatch to
372     // the actual function:
373     match vtbl {
374         traits::VtableImpl(impl_data) => {
375             let (def_id, substs) = traits::find_associated_item(
376                 tcx, param_env, trait_item, rcvr_substs, &impl_data);
377             let substs = tcx.erase_regions(&substs);
378             Some(ty::Instance::new(def_id, substs))
379         }
380         traits::VtableGenerator(generator_data) => {
381             Some(Instance {
382                 def: ty::InstanceDef::Item(generator_data.generator_def_id),
383                 substs: generator_data.substs
384             })
385         }
386         traits::VtableClosure(closure_data) => {
387             let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
388             Some(Instance::resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
389                                            trait_closure_kind))
390         }
391         traits::VtableFnPointer(ref data) => {
392             Some(Instance {
393                 def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
394                 substs: rcvr_substs
395             })
396         }
397         traits::VtableObject(ref data) => {
398             let index = tcx.get_vtable_index_of_object_method(data, def_id);
399             Some(Instance {
400                 def: ty::InstanceDef::Virtual(def_id, index),
401                 substs: rcvr_substs
402             })
403         }
404         traits::VtableBuiltin(..) => {
405             if tcx.lang_items().clone_trait().is_some() {
406                 Some(Instance {
407                     def: ty::InstanceDef::CloneShim(def_id, trait_ref.self_ty()),
408                     substs: rcvr_substs
409                 })
410             } else {
411                 None
412             }
413         }
414         traits::VtableAutoImpl(..) |
415         traits::VtableParam(..) |
416         traits::VtableTraitAlias(..) => None
417     }
418 }
419
420 fn needs_fn_once_adapter_shim(
421     actual_closure_kind: ty::ClosureKind,
422     trait_closure_kind: ty::ClosureKind,
423 ) -> Result<bool, ()> {
424     match (actual_closure_kind, trait_closure_kind) {
425         (ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
426             (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
427             (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
428                 // No adapter needed.
429                 Ok(false)
430             }
431         (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
432             // The closure fn `llfn` is a `fn(&self, ...)`.  We want a
433             // `fn(&mut self, ...)`. In fact, at codegen time, these are
434             // basically the same thing, so we can just return llfn.
435             Ok(false)
436         }
437         (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
438             (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
439                 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
440                 // self, ...)`.  We want a `fn(self, ...)`. We can produce
441                 // this by doing something like:
442                 //
443                 //     fn call_once(self, ...) { call_mut(&self, ...) }
444                 //     fn call_once(mut self, ...) { call_mut(&mut self, ...) }
445                 //
446                 // These are both the same at codegen time.
447                 Ok(true)
448         }
449         (ty::ClosureKind::FnMut, _) |
450         (ty::ClosureKind::FnOnce, _) => Err(())
451     }
452 }