1 use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
2 use crate::ty::print::{FmtPrinter, Printer};
3 use crate::ty::subst::{InternalSubsts, Subst};
4 use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeFoldable};
5 use rustc_errors::ErrorReported;
6 use rustc_hir::def::Namespace;
7 use rustc_hir::def_id::{CrateNum, DefId};
8 use rustc_hir::lang_items::LangItem;
9 use rustc_macros::HashStable;
10 
11 use std::fmt;
12 
13 /// A monomorphized `InstanceDef`.
14 ///
15 /// Monomorphization happens on-the-fly and no monomorphized MIR is ever created. Instead, this type
16 /// simply couples a potentially generic `InstanceDef` with some substs, and codegen and const eval
17 /// will do all required substitution as they run.
18 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
19 #[derive(HashStable, Lift)]
20 pub struct Instance<'tcx> {
21     pub def: InstanceDef<'tcx>,
22     pub substs: SubstsRef<'tcx>,
23 }
24 
25 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
26 #[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable)]
27 pub enum InstanceDef<'tcx> {
28     /// A user-defined callable item.
29     ///
30     /// This includes:
31     /// - `fn` items
32     /// - closures
33     /// - generators
34     Item(ty::WithOptConstParam<DefId>),
35 
36     /// An intrinsic `fn` item (with `"rust-intrinsic"` or `"platform-intrinsic"` ABI).
37     ///
38     /// Alongside `Virtual`, this is the only `InstanceDef` that does not have its own callable MIR.
39     /// Instead, codegen and const eval "magically" evaluate calls to intrinsics purely in the
40     /// caller.
41     Intrinsic(DefId),
42 
43     /// `<T as Trait>::method` where `method` receives unsizeable `self: Self` (part of the
44     /// `unsized_locals` feature).
45     ///
46     /// The generated shim will take `Self` via `*mut Self` - conceptually this is `&owned Self` -
47     /// and dereference the argument to call the original function.
48     VtableShim(DefId),
49 
50     /// `fn()` pointer where the function itself cannot be turned into a pointer.
51     ///
52     /// One example is `<dyn Trait as Trait>::fn`, where the shim contains
53     /// a virtual call, which codegen supports only via a direct call to the
54     /// `<dyn Trait as Trait>::fn` instance (an `InstanceDef::Virtual`).
55     ///
56     /// Another example is functions annotated with `#[track_caller]`, which
57     /// must have their implicit caller location argument populated for a call.
58     /// Because this is a required part of the function's ABI but can't be tracked
59     /// as a property of the function pointer, we use a single "caller location"
60     /// (the definition of the function itself).
61     ReifyShim(DefId),
62 
63     /// `<fn() as FnTrait>::call_*` (generated `FnTrait` implementation for `fn()` pointers).
64     ///
65     /// `DefId` is `FnTrait::call_*`.
66     FnPtrShim(DefId, Ty<'tcx>),
67 
68     /// Dynamic dispatch to `<dyn Trait as Trait>::fn`.
69     ///
70     /// This `InstanceDef` does not have callable MIR. Calls to `Virtual` instances must be
71     /// codegen'd as virtual calls through the vtable.
72     ///
73     /// If this is reified to a `fn` pointer, a `ReifyShim` is used (see `ReifyShim` above for more
74     /// details on that).
75     Virtual(DefId, usize),
76 
77     /// `<[FnMut closure] as FnOnce>::call_once`.
78     ///
79     /// The `DefId` is the ID of the `call_once` method in `FnOnce`.
80     ClosureOnceShim { call_once: DefId, track_caller: bool },
81 
82     /// `core::ptr::drop_in_place::<T>`.
83     ///
84     /// The `DefId` is for `core::ptr::drop_in_place`.
85     /// The `Option<Ty<'tcx>>` is either `Some(T)`, or `None` for empty drop
86     /// glue.
87     DropGlue(DefId, Option<Ty<'tcx>>),
88 
89     /// Compiler-generated `<T as Clone>::clone` implementation.
90     ///
91     /// For all types that automatically implement `Copy`, a trivial `Clone` impl is provided too.
92     /// Additionally, arrays, tuples, and closures get a `Clone` shim even if they aren't `Copy`.
93     ///
94     /// The `DefId` is for `Clone::clone`, the `Ty` is the type `T` with the builtin `Clone` impl.
95     CloneShim(DefId, Ty<'tcx>),
96 }
97 
98 impl<'tcx> Instance<'tcx> {
99     /// Returns the `Ty` corresponding to this `Instance`, with generic substitutions applied and
100     /// lifetimes erased, allowing a `ParamEnv` to be specified for use during normalization.
ty(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Ty<'tcx>101     pub fn ty(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Ty<'tcx> {
102         let ty = tcx.type_of(self.def.def_id());
103         tcx.subst_and_normalize_erasing_regions(self.substs, param_env, &ty)
104     }
105 
106     /// Finds a crate that contains a monomorphization of this instance that
107     /// can be linked to from the local crate. A return value of `None` means
108     /// no upstream crate provides such an exported monomorphization.
109     ///
110     /// This method already takes into account the global `-Zshare-generics`
111     /// setting, always returning `None` if `share-generics` is off.
upstream_monomorphization(&self, tcx: TyCtxt<'tcx>) -> Option<CrateNum>112     pub fn upstream_monomorphization(&self, tcx: TyCtxt<'tcx>) -> Option<CrateNum> {
113         // If we are not in share generics mode, we don't link to upstream
114         // monomorphizations but always instantiate our own internal versions
115         // instead.
116         if !tcx.sess.opts.share_generics() {
117             return None;
118         }
119 
120         // If this is an item that is defined in the local crate, no upstream
121         // crate can know about it/provide a monomorphization.
122         if self.def_id().is_local() {
123             return None;
124         }
125 
126         // If this a non-generic instance, it cannot be a shared monomorphization.
127         self.substs.non_erasable_generics().next()?;
128 
129         match self.def {
130             InstanceDef::Item(def) => tcx
131                 .upstream_monomorphizations_for(def.did)
132                 .and_then(|monos| monos.get(&self.substs).cloned()),
133             InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.substs),
134             _ => None,
135         }
136     }
137 }
138 
139 impl<'tcx> InstanceDef<'tcx> {
140     #[inline]
def_id(self) -> DefId141     pub fn def_id(self) -> DefId {
142         match self {
143             InstanceDef::Item(def) => def.did,
144             InstanceDef::VtableShim(def_id)
145             | InstanceDef::ReifyShim(def_id)
146             | InstanceDef::FnPtrShim(def_id, _)
147             | InstanceDef::Virtual(def_id, _)
148             | InstanceDef::Intrinsic(def_id)
149             | InstanceDef::ClosureOnceShim { call_once: def_id, track_caller: _ }
150             | InstanceDef::DropGlue(def_id, _)
151             | InstanceDef::CloneShim(def_id, _) => def_id,
152         }
153     }
154 
155     /// Returns the `DefId` of instances which might not require codegen locally.
def_id_if_not_guaranteed_local_codegen(self) -> Option<DefId>156     pub fn def_id_if_not_guaranteed_local_codegen(self) -> Option<DefId> {
157         match self {
158             ty::InstanceDef::Item(def) => Some(def.did),
159             ty::InstanceDef::DropGlue(def_id, Some(_)) => Some(def_id),
160             InstanceDef::VtableShim(..)
161             | InstanceDef::ReifyShim(..)
162             | InstanceDef::FnPtrShim(..)
163             | InstanceDef::Virtual(..)
164             | InstanceDef::Intrinsic(..)
165             | InstanceDef::ClosureOnceShim { .. }
166             | InstanceDef::DropGlue(..)
167             | InstanceDef::CloneShim(..) => None,
168         }
169     }
170 
171     #[inline]
with_opt_param(self) -> ty::WithOptConstParam<DefId>172     pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
173         match self {
174             InstanceDef::Item(def) => def,
175             InstanceDef::VtableShim(def_id)
176             | InstanceDef::ReifyShim(def_id)
177             | InstanceDef::FnPtrShim(def_id, _)
178             | InstanceDef::Virtual(def_id, _)
179             | InstanceDef::Intrinsic(def_id)
180             | InstanceDef::ClosureOnceShim { call_once: def_id, track_caller: _ }
181             | InstanceDef::DropGlue(def_id, _)
182             | InstanceDef::CloneShim(def_id, _) => ty::WithOptConstParam::unknown(def_id),
183         }
184     }
185 
186     #[inline]
attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx>187     pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
188         tcx.get_attrs(self.def_id())
189     }
190 
191     /// Returns `true` if the LLVM version of this instance is unconditionally
192     /// marked with `inline`. This implies that a copy of this instance is
193     /// generated in every codegen unit.
194     /// Note that this is only a hint. See the documentation for
195     /// `generates_cgu_internal_copy` for more information.
requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool196     pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
197         use rustc_hir::definitions::DefPathData;
198         let def_id = match *self {
199             ty::InstanceDef::Item(def) => def.did,
200             ty::InstanceDef::DropGlue(_, Some(_)) => return false,
201             _ => return true,
202         };
203         matches!(
204             tcx.def_key(def_id).disambiguated_data.data,
205             DefPathData::Ctor | DefPathData::ClosureExpr
206         )
207     }
208 
209     /// Returns `true` if the machine code for this instance is instantiated in
210     /// each codegen unit that references it.
211     /// Note that this is only a hint! The compiler can globally decide to *not*
212     /// do this in order to speed up compilation. CGU-internal copies are
213     /// only exist to enable inlining. If inlining is not performed (e.g. at
214     /// `-Copt-level=0`) then the time for generating them is wasted and it's
215     /// better to create a single copy with external linkage.
generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool216     pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool {
217         if self.requires_inline(tcx) {
218             return true;
219         }
220         if let ty::InstanceDef::DropGlue(.., Some(ty)) = *self {
221             // Drop glue generally wants to be instantiated at every codegen
222             // unit, but without an #[inline] hint. We should make this
223             // available to normal end-users.
224             if tcx.sess.opts.incremental.is_none() {
225                 return true;
226             }
227             // When compiling with incremental, we can generate a *lot* of
228             // codegen units. Including drop glue into all of them has a
229             // considerable compile time cost.
230             //
231             // We include enums without destructors to allow, say, optimizing
232             // drops of `Option::None` before LTO. We also respect the intent of
233             // `#[inline]` on `Drop::drop` implementations.
234             return ty.ty_adt_def().map_or(true, |adt_def| {
235                 adt_def.destructor(tcx).map_or_else(
236                     || adt_def.is_enum(),
237                     |dtor| tcx.codegen_fn_attrs(dtor.did).requests_inline(),
238                 )
239             });
240         }
241         tcx.codegen_fn_attrs(self.def_id()).requests_inline()
242     }
243 
requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool244     pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool {
245         match *self {
246             InstanceDef::Item(ty::WithOptConstParam { did: def_id, .. })
247             | InstanceDef::Virtual(def_id, _) => {
248                 tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
249             }
250             InstanceDef::ClosureOnceShim { call_once: _, track_caller } => track_caller,
251             _ => false,
252         }
253     }
254 
255     /// Returns `true` when the MIR body associated with this instance should be monomorphized
256     /// by its users (e.g. codegen or miri) by substituting the `substs` from `Instance` (see
257     /// `Instance::substs_for_mir_body`).
258     ///
259     /// Otherwise, returns `false` only for some kinds of shims where the construction of the MIR
260     /// body should perform necessary substitutions.
has_polymorphic_mir_body(&self) -> bool261     pub fn has_polymorphic_mir_body(&self) -> bool {
262         match *self {
263             InstanceDef::CloneShim(..)
264             | InstanceDef::FnPtrShim(..)
265             | InstanceDef::DropGlue(_, Some(_)) => false,
266             InstanceDef::ClosureOnceShim { .. }
267             | InstanceDef::DropGlue(..)
268             | InstanceDef::Item(_)
269             | InstanceDef::Intrinsic(..)
270             | InstanceDef::ReifyShim(..)
271             | InstanceDef::Virtual(..)
272             | InstanceDef::VtableShim(..) => true,
273         }
274     }
275 }
276 
277 impl<'tcx> fmt::Display for Instance<'tcx> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result278     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
279         ty::tls::with(|tcx| {
280             let substs = tcx.lift(self.substs).expect("could not lift for printing");
281             FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
282                 .print_def_path(self.def_id(), substs)?;
283             Ok(())
284         })?;
285 
286         match self.def {
287             InstanceDef::Item(_) => Ok(()),
288             InstanceDef::VtableShim(_) => write!(f, " - shim(vtable)"),
289             InstanceDef::ReifyShim(_) => write!(f, " - shim(reify)"),
290             InstanceDef::Intrinsic(_) => write!(f, " - intrinsic"),
291             InstanceDef::Virtual(_, num) => write!(f, " - virtual#{}", num),
292             InstanceDef::FnPtrShim(_, ty) => write!(f, " - shim({})", ty),
293             InstanceDef::ClosureOnceShim { .. } => write!(f, " - shim"),
294             InstanceDef::DropGlue(_, None) => write!(f, " - shim(None)"),
295             InstanceDef::DropGlue(_, Some(ty)) => write!(f, " - shim(Some({}))", ty),
296             InstanceDef::CloneShim(_, ty) => write!(f, " - shim({})", ty),
297         }
298     }
299 }
300 
301 impl<'tcx> Instance<'tcx> {
new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx>302     pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx> {
303         assert!(
304             !substs.has_escaping_bound_vars(),
305             "substs of instance {:?} not normalized for codegen: {:?}",
306             def_id,
307             substs
308         );
309         Instance { def: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)), substs }
310     }
311 
mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx>312     pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
313         let substs = InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind {
314             ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
315             ty::GenericParamDefKind::Type { .. } => {
316                 bug!("Instance::mono: {:?} has type parameters", def_id)
317             }
318             ty::GenericParamDefKind::Const { .. } => {
319                 bug!("Instance::mono: {:?} has const parameters", def_id)
320             }
321         });
322 
323         Instance::new(def_id, substs)
324     }
325 
326     #[inline]
def_id(&self) -> DefId327     pub fn def_id(&self) -> DefId {
328         self.def.def_id()
329     }
330 
331     /// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly,
332     /// this is used to find the precise code that will run for a trait method invocation,
333     /// if known.
334     ///
335     /// Returns `Ok(None)` if we cannot resolve `Instance` to a specific instance.
336     /// For example, in a context like this,
337     ///
338     /// ```
339     /// fn foo<T: Debug>(t: T) { ... }
340     /// ```
341     ///
342     /// trying to resolve `Debug::fmt` applied to `T` will yield `Ok(None)`, because we do not
343     /// know what code ought to run. (Note that this setting is also affected by the
344     /// `RevealMode` in the parameter environment.)
345     ///
346     /// Presuming that coherence and type-check have succeeded, if this method is invoked
347     /// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
348     /// `Ok(Some(instance))`.
349     ///
350     /// Returns `Err(ErrorReported)` when the `Instance` resolution process
351     /// couldn't complete due to errors elsewhere - this is distinct
352     /// from `Ok(None)` to avoid misleading diagnostics when an error
353     /// has already been/will be emitted, for the original cause
resolve( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, def_id: DefId, substs: SubstsRef<'tcx>, ) -> Result<Option<Instance<'tcx>>, ErrorReported>354     pub fn resolve(
355         tcx: TyCtxt<'tcx>,
356         param_env: ty::ParamEnv<'tcx>,
357         def_id: DefId,
358         substs: SubstsRef<'tcx>,
359     ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
360         Instance::resolve_opt_const_arg(
361             tcx,
362             param_env,
363             ty::WithOptConstParam::unknown(def_id),
364             substs,
365         )
366     }
367 
368     // This should be kept up to date with `resolve`.
369     #[instrument(level = "debug", skip(tcx))]
resolve_opt_const_arg( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, def: ty::WithOptConstParam<DefId>, substs: SubstsRef<'tcx>, ) -> Result<Option<Instance<'tcx>>, ErrorReported>370     pub fn resolve_opt_const_arg(
371         tcx: TyCtxt<'tcx>,
372         param_env: ty::ParamEnv<'tcx>,
373         def: ty::WithOptConstParam<DefId>,
374         substs: SubstsRef<'tcx>,
375     ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
376         // All regions in the result of this query are erased, so it's
377         // fine to erase all of the input regions.
378 
379         // HACK(eddyb) erase regions in `substs` first, so that `param_env.and(...)`
380         // below is more likely to ignore the bounds in scope (e.g. if the only
381         // generic parameters mentioned by `substs` were lifetime ones).
382         let substs = tcx.erase_regions(substs);
383 
384         // FIXME(eddyb) should this always use `param_env.with_reveal_all()`?
385         if let Some((did, param_did)) = def.as_const_arg() {
386             tcx.resolve_instance_of_const_arg(
387                 tcx.erase_regions(param_env.and((did, param_did, substs))),
388             )
389         } else {
390             tcx.resolve_instance(tcx.erase_regions(param_env.and((def.did, substs))))
391         }
392     }
393 
resolve_for_fn_ptr( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, def_id: DefId, substs: SubstsRef<'tcx>, ) -> Option<Instance<'tcx>>394     pub fn resolve_for_fn_ptr(
395         tcx: TyCtxt<'tcx>,
396         param_env: ty::ParamEnv<'tcx>,
397         def_id: DefId,
398         substs: SubstsRef<'tcx>,
399     ) -> Option<Instance<'tcx>> {
400         debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
401         // Use either `resolve_closure` or `resolve_for_vtable`
402         assert!(!tcx.is_closure(def_id), "Called `resolve_for_fn_ptr` on closure: {:?}", def_id);
403         Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| {
404             match resolved.def {
405                 InstanceDef::Item(def) if resolved.def.requires_caller_location(tcx) => {
406                     debug!(" => fn pointer created for function with #[track_caller]");
407                     resolved.def = InstanceDef::ReifyShim(def.did);
408                 }
409                 InstanceDef::Virtual(def_id, _) => {
410                     debug!(" => fn pointer created for virtual call");
411                     resolved.def = InstanceDef::ReifyShim(def_id);
412                 }
413                 _ => {}
414             }
415 
416             resolved
417         })
418     }
419 
resolve_for_vtable( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, def_id: DefId, substs: SubstsRef<'tcx>, ) -> Option<Instance<'tcx>>420     pub fn resolve_for_vtable(
421         tcx: TyCtxt<'tcx>,
422         param_env: ty::ParamEnv<'tcx>,
423         def_id: DefId,
424         substs: SubstsRef<'tcx>,
425     ) -> Option<Instance<'tcx>> {
426         debug!("resolve_for_vtable(def_id={:?}, substs={:?})", def_id, substs);
427         let fn_sig = tcx.fn_sig(def_id);
428         let is_vtable_shim = !fn_sig.inputs().skip_binder().is_empty()
429             && fn_sig.input(0).skip_binder().is_param(0)
430             && tcx.generics_of(def_id).has_self;
431         if is_vtable_shim {
432             debug!(" => associated item with unsizeable self: Self");
433             Some(Instance { def: InstanceDef::VtableShim(def_id), substs })
434         } else {
435             Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| {
436                 match resolved.def {
437                     InstanceDef::Item(def) => {
438                         // We need to generate a shim when we cannot guarantee that
439                         // the caller of a trait object method will be aware of
440                         // `#[track_caller]` - this ensures that the caller
441                         // and callee ABI will always match.
442                         //
443                         // The shim is generated when all of these conditions are met:
444                         //
445                         // 1) The underlying method expects a caller location parameter
446                         // in the ABI
447                         if resolved.def.requires_caller_location(tcx)
448                             // 2) The caller location parameter comes from having `#[track_caller]`
449                             // on the implementation, and *not* on the trait method.
450                             && !tcx.should_inherit_track_caller(def.did)
451                             // If the method implementation comes from the trait definition itself
452                             // (e.g. `trait Foo { #[track_caller] my_fn() { /* impl */ } }`),
453                             // then we don't need to generate a shim. This check is needed because
454                             // `should_inherit_track_caller` returns `false` if our method
455                             // implementation comes from the trait block, and not an impl block
456                             && !matches!(
457                                 tcx.opt_associated_item(def.did),
458                                 Some(ty::AssocItem {
459                                     container: ty::AssocItemContainer::TraitContainer(_),
460                                     ..
461                                 })
462                             )
463                         {
464                             if tcx.is_closure(def.did) {
465                                 debug!(" => vtable fn pointer created for closure with #[track_caller]: {:?} for method {:?} {:?}",
466                                        def.did, def_id, substs);
467 
468                                 // Create a shim for the `FnOnce/FnMut/Fn` method we are calling
469                                 // - unlike functions, invoking a closure always goes through a
470                                 // trait.
471                                 resolved = Instance { def: InstanceDef::ReifyShim(def_id), substs };
472                             } else {
473                                 debug!(
474                                     " => vtable fn pointer created for function with #[track_caller]: {:?}", def.did
475                                 );
476                                 resolved.def = InstanceDef::ReifyShim(def.did);
477                             }
478                         }
479                     }
480                     InstanceDef::Virtual(def_id, _) => {
481                         debug!(" => vtable fn pointer created for virtual call");
482                         resolved.def = InstanceDef::ReifyShim(def_id);
483                     }
484                     _ => {}
485                 }
486 
487                 resolved
488             })
489         }
490     }
491 
resolve_closure( tcx: TyCtxt<'tcx>, def_id: DefId, substs: ty::SubstsRef<'tcx>, requested_kind: ty::ClosureKind, ) -> Instance<'tcx>492     pub fn resolve_closure(
493         tcx: TyCtxt<'tcx>,
494         def_id: DefId,
495         substs: ty::SubstsRef<'tcx>,
496         requested_kind: ty::ClosureKind,
497     ) -> Instance<'tcx> {
498         let actual_kind = substs.as_closure().kind();
499 
500         match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
501             Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
502             _ => Instance::new(def_id, substs),
503         }
504     }
505 
resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx>506     pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
507         let def_id = tcx.require_lang_item(LangItem::DropInPlace, None);
508         let substs = tcx.intern_substs(&[ty.into()]);
509         Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap().unwrap()
510     }
511 
fn_once_adapter_instance( tcx: TyCtxt<'tcx>, closure_did: DefId, substs: ty::SubstsRef<'tcx>, ) -> Instance<'tcx>512     pub fn fn_once_adapter_instance(
513         tcx: TyCtxt<'tcx>,
514         closure_did: DefId,
515         substs: ty::SubstsRef<'tcx>,
516     ) -> Instance<'tcx> {
517         debug!("fn_once_adapter_shim({:?}, {:?})", closure_did, substs);
518         let fn_once = tcx.require_lang_item(LangItem::FnOnce, None);
519         let call_once = tcx
520             .associated_items(fn_once)
521             .in_definition_order()
522             .find(|it| it.kind == ty::AssocKind::Fn)
523             .unwrap()
524             .def_id;
525         let track_caller =
526             tcx.codegen_fn_attrs(closure_did).flags.contains(CodegenFnAttrFlags::TRACK_CALLER);
527         let def = ty::InstanceDef::ClosureOnceShim { call_once, track_caller };
528 
529         let self_ty = tcx.mk_closure(closure_did, substs);
530 
531         let sig = substs.as_closure().sig();
532         let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), sig);
533         assert_eq!(sig.inputs().len(), 1);
534         let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
535 
536         debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
537         Instance { def, substs }
538     }
539 
540     /// Depending on the kind of `InstanceDef`, the MIR body associated with an
541     /// instance is expressed in terms of the generic parameters of `self.def_id()`, and in other
542     /// cases the MIR body is expressed in terms of the types found in the substitution array.
543     /// In the former case, we want to substitute those generic types and replace them with the
544     /// values from the substs when monomorphizing the function body. But in the latter case, we
545     /// don't want to do that substitution, since it has already been done effectively.
546     ///
547     /// This function returns `Some(substs)` in the former case and `None` otherwise -- i.e., if
548     /// this function returns `None`, then the MIR body does not require substitution during
549     /// codegen.
substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>>550     fn substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>> {
551         if self.def.has_polymorphic_mir_body() { Some(self.substs) } else { None }
552     }
553 
subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: &T) -> T where T: TypeFoldable<'tcx> + Copy,554     pub fn subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: &T) -> T
555     where
556         T: TypeFoldable<'tcx> + Copy,
557     {
558         if let Some(substs) = self.substs_for_mir_body() { v.subst(tcx, substs) } else { *v }
559     }
560 
561     #[inline(always)]
subst_mir_and_normalize_erasing_regions<T>( &self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, v: T, ) -> T where T: TypeFoldable<'tcx> + Clone,562     pub fn subst_mir_and_normalize_erasing_regions<T>(
563         &self,
564         tcx: TyCtxt<'tcx>,
565         param_env: ty::ParamEnv<'tcx>,
566         v: T,
567     ) -> T
568     where
569         T: TypeFoldable<'tcx> + Clone,
570     {
571         if let Some(substs) = self.substs_for_mir_body() {
572             tcx.subst_and_normalize_erasing_regions(substs, param_env, v)
573         } else {
574             tcx.normalize_erasing_regions(param_env, v)
575         }
576     }
577 
578     /// Returns a new `Instance` where generic parameters in `instance.substs` are replaced by
579     /// identity parameters if they are determined to be unused in `instance.def`.
polymorphize(self, tcx: TyCtxt<'tcx>) -> Self580     pub fn polymorphize(self, tcx: TyCtxt<'tcx>) -> Self {
581         debug!("polymorphize: running polymorphization analysis");
582         if !tcx.sess.opts.debugging_opts.polymorphize {
583             return self;
584         }
585 
586         let polymorphized_substs = polymorphize(tcx, self.def, self.substs);
587         debug!("polymorphize: self={:?} polymorphized_substs={:?}", self, polymorphized_substs);
588         Self { def: self.def, substs: polymorphized_substs }
589     }
590 }
591 
polymorphize<'tcx>( tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>, substs: SubstsRef<'tcx>, ) -> SubstsRef<'tcx>592 fn polymorphize<'tcx>(
593     tcx: TyCtxt<'tcx>,
594     instance: ty::InstanceDef<'tcx>,
595     substs: SubstsRef<'tcx>,
596 ) -> SubstsRef<'tcx> {
597     debug!("polymorphize({:?}, {:?})", instance, substs);
598     let unused = tcx.unused_generic_params(instance);
599     debug!("polymorphize: unused={:?}", unused);
600 
601     // If this is a closure or generator then we need to handle the case where another closure
602     // from the function is captured as an upvar and hasn't been polymorphized. In this case,
603     // the unpolymorphized upvar closure would result in a polymorphized closure producing
604     // multiple mono items (and eventually symbol clashes).
605     let def_id = instance.def_id();
606     let upvars_ty = if tcx.is_closure(def_id) {
607         Some(substs.as_closure().tupled_upvars_ty())
608     } else if tcx.type_of(def_id).is_generator() {
609         Some(substs.as_generator().tupled_upvars_ty())
610     } else {
611         None
612     };
613     let has_upvars = upvars_ty.map_or(false, |ty| ty.tuple_fields().count() > 0);
614     debug!("polymorphize: upvars_ty={:?} has_upvars={:?}", upvars_ty, has_upvars);
615 
616     struct PolymorphizationFolder<'tcx> {
617         tcx: TyCtxt<'tcx>,
618     }
619 
620     impl ty::TypeFolder<'tcx> for PolymorphizationFolder<'tcx> {
621         fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
622             self.tcx
623         }
624 
625         fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
626             debug!("fold_ty: ty={:?}", ty);
627             match ty.kind {
628                 ty::Closure(def_id, substs) => {
629                     let polymorphized_substs = polymorphize(
630                         self.tcx,
631                         ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)),
632                         substs,
633                     );
634                     if substs == polymorphized_substs {
635                         ty
636                     } else {
637                         self.tcx.mk_closure(def_id, polymorphized_substs)
638                     }
639                 }
640                 ty::Generator(def_id, substs, movability) => {
641                     let polymorphized_substs = polymorphize(
642                         self.tcx,
643                         ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)),
644                         substs,
645                     );
646                     if substs == polymorphized_substs {
647                         ty
648                     } else {
649                         self.tcx.mk_generator(def_id, polymorphized_substs, movability)
650                     }
651                 }
652                 _ => ty.super_fold_with(self),
653             }
654         }
655     }
656 
657     InternalSubsts::for_item(tcx, def_id, |param, _| {
658         let is_unused = unused.contains(param.index).unwrap_or(false);
659         debug!("polymorphize: param={:?} is_unused={:?}", param, is_unused);
660         match param.kind {
661             // Upvar case: If parameter is a type parameter..
662             ty::GenericParamDefKind::Type { .. } if
663                 // ..and has upvars..
664                 has_upvars &&
665                 // ..and this param has the same type as the tupled upvars..
666                 upvars_ty == Some(substs[param.index as usize].expect_ty()) => {
667                     // ..then double-check that polymorphization marked it used..
668                     debug_assert!(!is_unused);
669                     // ..and polymorphize any closures/generators captured as upvars.
670                     let upvars_ty = upvars_ty.unwrap();
671                     let polymorphized_upvars_ty = upvars_ty.fold_with(
672                         &mut PolymorphizationFolder { tcx });
673                     debug!("polymorphize: polymorphized_upvars_ty={:?}", polymorphized_upvars_ty);
674                     ty::GenericArg::from(polymorphized_upvars_ty)
675                 },
676 
677             // Simple case: If parameter is a const or type parameter..
678             ty::GenericParamDefKind::Const { .. } | ty::GenericParamDefKind::Type { .. } if
679                 // ..and is within range and unused..
680                 unused.contains(param.index).unwrap_or(false) =>
681                     // ..then use the identity for this parameter.
682                     tcx.mk_param_from_def(param),
683 
684             // Otherwise, use the parameter as before.
685             _ => substs[param.index as usize],
686         }
687     })
688 }
689 
needs_fn_once_adapter_shim( actual_closure_kind: ty::ClosureKind, trait_closure_kind: ty::ClosureKind, ) -> Result<bool, ()>690 fn needs_fn_once_adapter_shim(
691     actual_closure_kind: ty::ClosureKind,
692     trait_closure_kind: ty::ClosureKind,
693 ) -> Result<bool, ()> {
694     match (actual_closure_kind, trait_closure_kind) {
695         (ty::ClosureKind::Fn, ty::ClosureKind::Fn)
696         | (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut)
697         | (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
698             // No adapter needed.
699             Ok(false)
700         }
701         (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
702             // The closure fn `llfn` is a `fn(&self, ...)`.  We want a
703             // `fn(&mut self, ...)`. In fact, at codegen time, these are
704             // basically the same thing, so we can just return llfn.
705             Ok(false)
706         }
707         (ty::ClosureKind::Fn | ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
708             // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
709             // self, ...)`.  We want a `fn(self, ...)`. We can produce
710             // this by doing something like:
711             //
712             //     fn call_once(self, ...) { call_mut(&self, ...) }
713             //     fn call_once(mut self, ...) { call_mut(&mut self, ...) }
714             //
715             // These are both the same at codegen time.
716             Ok(true)
717         }
718         (ty::ClosureKind::FnMut | ty::ClosureKind::FnOnce, _) => Err(()),
719     }
720 }
721