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