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