1 use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
2 use crate::ty::print::{FmtPrinter, Printer};
3 use crate::ty::subst::InternalSubsts;
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::{DropInPlaceFnLangItem, FnOnceTraitLangItem};
9 use rustc_macros::HashStable;
13 /// A monomorphized `InstanceDef`.
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, RustcEncodable, RustcDecodable)]
19 #[derive(HashStable, Lift)]
20 pub struct Instance<'tcx> {
21 pub def: InstanceDef<'tcx>,
22 pub substs: SubstsRef<'tcx>,
25 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)]
26 pub enum InstanceDef<'tcx> {
27 /// A user-defined callable item.
33 Item(ty::WithOptConstParam<DefId>),
35 /// An intrinsic `fn` item (with `"rust-intrinsic"` or `"platform-intrinsic"` ABI).
37 /// Alongside `Virtual`, this is the only `InstanceDef` that does not have its own callable MIR.
38 /// Instead, codegen and const eval "magically" evaluate calls to intrinsics purely in the
42 /// `<T as Trait>::method` where `method` receives unsizeable `self: Self` (part of the
43 /// `unsized_locals` feature).
45 /// The generated shim will take `Self` via `*mut Self` - conceptually this is `&owned Self` -
46 /// and dereference the argument to call the original function.
49 /// `fn()` pointer where the function itself cannot be turned into a pointer.
51 /// One example is `<dyn Trait as Trait>::fn`, where the shim contains
52 /// a virtual call, which codegen supports only via a direct call to the
53 /// `<dyn Trait as Trait>::fn` instance (an `InstanceDef::Virtual`).
55 /// Another example is functions annotated with `#[track_caller]`, which
56 /// must have their implicit caller location argument populated for a call.
57 /// Because this is a required part of the function's ABI but can't be tracked
58 /// as a property of the function pointer, we use a single "caller location"
59 /// (the definition of the function itself).
62 /// `<fn() as FnTrait>::call_*` (generated `FnTrait` implementation for `fn()` pointers).
64 /// `DefId` is `FnTrait::call_*`.
66 /// NB: the (`fn` pointer) type must currently be monomorphic to avoid double substitution
67 /// problems with the MIR shim bodies. `Instance::resolve` enforces this.
68 // FIXME(#69925) support polymorphic MIR shim bodies properly instead.
69 FnPtrShim(DefId, Ty<'tcx>),
71 /// Dynamic dispatch to `<dyn Trait as Trait>::fn`.
73 /// This `InstanceDef` does not have callable MIR. Calls to `Virtual` instances must be
74 /// codegen'd as virtual calls through the vtable.
76 /// If this is reified to a `fn` pointer, a `ReifyShim` is used (see `ReifyShim` above for more
78 Virtual(DefId, usize),
80 /// `<[FnMut closure] as FnOnce>::call_once`.
82 /// The `DefId` is the ID of the `call_once` method in `FnOnce`.
83 ClosureOnceShim { call_once: DefId },
85 /// `core::ptr::drop_in_place::<T>`.
87 /// The `DefId` is for `core::ptr::drop_in_place`.
88 /// The `Option<Ty<'tcx>>` is either `Some(T)`, or `None` for empty drop
91 /// NB: the type must currently be monomorphic to avoid double substitution
92 /// problems with the MIR shim bodies. `Instance::resolve` enforces this.
93 // FIXME(#69925) support polymorphic MIR shim bodies properly instead.
94 DropGlue(DefId, Option<Ty<'tcx>>),
96 /// Compiler-generated `<T as Clone>::clone` implementation.
98 /// For all types that automatically implement `Copy`, a trivial `Clone` impl is provided too.
99 /// Additionally, arrays, tuples, and closures get a `Clone` shim even if they aren't `Copy`.
101 /// The `DefId` is for `Clone::clone`, the `Ty` is the type `T` with the builtin `Clone` impl.
103 /// NB: the type must currently be monomorphic to avoid double substitution
104 /// problems with the MIR shim bodies. `Instance::resolve` enforces this.
105 // FIXME(#69925) support polymorphic MIR shim bodies properly instead.
106 CloneShim(DefId, Ty<'tcx>),
109 impl<'tcx> Instance<'tcx> {
110 /// Returns the `Ty` corresponding to this `Instance`, with generic substitutions applied and
111 /// lifetimes erased, allowing a `ParamEnv` to be specified for use during normalization.
112 pub fn ty(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Ty<'tcx> {
113 let ty = tcx.type_of(self.def.def_id());
114 tcx.subst_and_normalize_erasing_regions(self.substs, param_env, &ty)
117 /// Finds a crate that contains a monomorphization of this instance that
118 /// can be linked to from the local crate. A return value of `None` means
119 /// no upstream crate provides such an exported monomorphization.
121 /// This method already takes into account the global `-Zshare-generics`
122 /// setting, always returning `None` if `share-generics` is off.
123 pub fn upstream_monomorphization(&self, tcx: TyCtxt<'tcx>) -> Option<CrateNum> {
124 // If we are not in share generics mode, we don't link to upstream
125 // monomorphizations but always instantiate our own internal versions
127 if !tcx.sess.opts.share_generics() {
131 // If this is an item that is defined in the local crate, no upstream
132 // crate can know about it/provide a monomorphization.
133 if self.def_id().is_local() {
137 // If this a non-generic instance, it cannot be a shared monomorphization.
138 self.substs.non_erasable_generics().next()?;
141 InstanceDef::Item(def) => tcx
142 .upstream_monomorphizations_for(def.did)
143 .and_then(|monos| monos.get(&self.substs).cloned()),
144 InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.substs),
150 impl<'tcx> InstanceDef<'tcx> {
152 pub fn def_id(self) -> DefId {
154 InstanceDef::Item(def) => def.did,
155 InstanceDef::VtableShim(def_id)
156 | InstanceDef::ReifyShim(def_id)
157 | InstanceDef::FnPtrShim(def_id, _)
158 | InstanceDef::Virtual(def_id, _)
159 | InstanceDef::Intrinsic(def_id)
160 | InstanceDef::ClosureOnceShim { call_once: def_id }
161 | InstanceDef::DropGlue(def_id, _)
162 | InstanceDef::CloneShim(def_id, _) => def_id,
167 pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
169 InstanceDef::Item(def) => def,
170 InstanceDef::VtableShim(def_id)
171 | InstanceDef::ReifyShim(def_id)
172 | InstanceDef::FnPtrShim(def_id, _)
173 | InstanceDef::Virtual(def_id, _)
174 | InstanceDef::Intrinsic(def_id)
175 | InstanceDef::ClosureOnceShim { call_once: def_id }
176 | InstanceDef::DropGlue(def_id, _)
177 | InstanceDef::CloneShim(def_id, _) => ty::WithOptConstParam::unknown(def_id),
182 pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
183 tcx.get_attrs(self.def_id())
186 /// Returns `true` if the LLVM version of this instance is unconditionally
187 /// marked with `inline`. This implies that a copy of this instance is
188 /// generated in every codegen unit.
189 /// Note that this is only a hint. See the documentation for
190 /// `generates_cgu_internal_copy` for more information.
191 pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
192 use rustc_hir::definitions::DefPathData;
193 let def_id = match *self {
194 ty::InstanceDef::Item(def) => def.did,
195 ty::InstanceDef::DropGlue(_, Some(_)) => return false,
198 match tcx.def_key(def_id).disambiguated_data.data {
199 DefPathData::Ctor | DefPathData::ClosureExpr => true,
204 /// Returns `true` if the machine code for this instance is instantiated in
205 /// each codegen unit that references it.
206 /// Note that this is only a hint! The compiler can globally decide to *not*
207 /// do this in order to speed up compilation. CGU-internal copies are
208 /// only exist to enable inlining. If inlining is not performed (e.g. at
209 /// `-Copt-level=0`) then the time for generating them is wasted and it's
210 /// better to create a single copy with external linkage.
211 pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool {
212 if self.requires_inline(tcx) {
215 if let ty::InstanceDef::DropGlue(.., Some(ty)) = *self {
216 // Drop glue generally wants to be instantiated at every codegen
217 // unit, but without an #[inline] hint. We should make this
218 // available to normal end-users.
219 if tcx.sess.opts.incremental.is_none() {
222 // When compiling with incremental, we can generate a *lot* of
223 // codegen units. Including drop glue into all of them has a
224 // considerable compile time cost.
226 // We include enums without destructors to allow, say, optimizing
227 // drops of `Option::None` before LTO. We also respect the intent of
228 // `#[inline]` on `Drop::drop` implementations.
229 return ty.ty_adt_def().map_or(true, |adt_def| {
230 adt_def.destructor(tcx).map_or(adt_def.is_enum(), |dtor| {
231 tcx.codegen_fn_attrs(dtor.did).requests_inline()
235 tcx.codegen_fn_attrs(self.def_id()).requests_inline()
238 pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool {
240 InstanceDef::Item(def) => {
241 tcx.codegen_fn_attrs(def.did).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
248 impl<'tcx> fmt::Display for Instance<'tcx> {
249 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250 ty::tls::with(|tcx| {
251 let substs = tcx.lift(&self.substs).expect("could not lift for printing");
252 FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
253 .print_def_path(self.def_id(), substs)?;
258 InstanceDef::Item(_) => Ok(()),
259 InstanceDef::VtableShim(_) => write!(f, " - shim(vtable)"),
260 InstanceDef::ReifyShim(_) => write!(f, " - shim(reify)"),
261 InstanceDef::Intrinsic(_) => write!(f, " - intrinsic"),
262 InstanceDef::Virtual(_, num) => write!(f, " - virtual#{}", num),
263 InstanceDef::FnPtrShim(_, ty) => write!(f, " - shim({:?})", ty),
264 InstanceDef::ClosureOnceShim { .. } => write!(f, " - shim"),
265 InstanceDef::DropGlue(_, ty) => write!(f, " - shim({:?})", ty),
266 InstanceDef::CloneShim(_, ty) => write!(f, " - shim({:?})", ty),
271 impl<'tcx> Instance<'tcx> {
272 pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx> {
274 !substs.has_escaping_bound_vars(),
275 "substs of instance {:?} not normalized for codegen: {:?}",
279 Instance { def: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)), substs }
282 pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
283 Instance::new(def_id, tcx.empty_substs_for_def_id(def_id))
287 pub fn def_id(&self) -> DefId {
291 /// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly,
292 /// this is used to find the precise code that will run for a trait method invocation,
295 /// Returns `Ok(None)` if we cannot resolve `Instance` to a specific instance.
296 /// For example, in a context like this,
299 /// fn foo<T: Debug>(t: T) { ... }
302 /// trying to resolve `Debug::fmt` applied to `T` will yield `Ok(None)`, because we do not
303 /// know what code ought to run. (Note that this setting is also affected by the
304 /// `RevealMode` in the parameter environment.)
306 /// Presuming that coherence and type-check have succeeded, if this method is invoked
307 /// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
308 /// `Ok(Some(instance))`.
310 /// Returns `Err(ErrorReported)` when the `Instance` resolution process
311 /// couldn't complete due to errors elsewhere - this is distinct
312 /// from `Ok(None)` to avoid misleading diagnostics when an error
313 /// has already been/will be emitted, for the original cause
316 param_env: ty::ParamEnv<'tcx>,
318 substs: SubstsRef<'tcx>,
319 ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
320 Instance::resolve_opt_const_arg(
323 ty::WithOptConstParam::unknown(def_id),
328 // This should be kept up to date with `resolve`.
329 pub fn resolve_opt_const_arg(
331 param_env: ty::ParamEnv<'tcx>,
332 def: ty::WithOptConstParam<DefId>,
333 substs: SubstsRef<'tcx>,
334 ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
335 // All regions in the result of this query are erased, so it's
336 // fine to erase all of the input regions.
338 // HACK(eddyb) erase regions in `substs` first, so that `param_env.and(...)`
339 // below is more likely to ignore the bounds in scope (e.g. if the only
340 // generic parameters mentioned by `substs` were lifetime ones).
341 let substs = tcx.erase_regions(&substs);
343 // FIXME(eddyb) should this always use `param_env.with_reveal_all()`?
344 if let Some((did, param_did)) = def.as_const_arg() {
345 tcx.resolve_instance_of_const_arg(
346 tcx.erase_regions(¶m_env.and((did, param_did, substs))),
349 tcx.resolve_instance(tcx.erase_regions(¶m_env.and((def.did, substs))))
353 pub fn resolve_for_fn_ptr(
355 param_env: ty::ParamEnv<'tcx>,
357 substs: SubstsRef<'tcx>,
358 ) -> Option<Instance<'tcx>> {
359 debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
360 Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| {
362 InstanceDef::Item(def) if resolved.def.requires_caller_location(tcx) => {
363 debug!(" => fn pointer created for function with #[track_caller]");
364 resolved.def = InstanceDef::ReifyShim(def.did);
366 InstanceDef::Virtual(def_id, _) => {
367 debug!(" => fn pointer created for virtual call");
368 resolved.def = InstanceDef::ReifyShim(def_id);
377 pub fn resolve_for_vtable(
379 param_env: ty::ParamEnv<'tcx>,
381 substs: SubstsRef<'tcx>,
382 ) -> Option<Instance<'tcx>> {
383 debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
384 let fn_sig = tcx.fn_sig(def_id);
385 let is_vtable_shim = !fn_sig.inputs().skip_binder().is_empty()
386 && fn_sig.input(0).skip_binder().is_param(0)
387 && tcx.generics_of(def_id).has_self;
389 debug!(" => associated item with unsizeable self: Self");
390 Some(Instance { def: InstanceDef::VtableShim(def_id), substs })
392 Instance::resolve_for_fn_ptr(tcx, param_env, def_id, substs)
396 pub fn resolve_closure(
399 substs: ty::SubstsRef<'tcx>,
400 requested_kind: ty::ClosureKind,
401 ) -> Instance<'tcx> {
402 let actual_kind = substs.as_closure().kind();
404 match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
405 Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
406 _ => Instance::new(def_id, substs),
410 pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
411 let def_id = tcx.require_lang_item(DropInPlaceFnLangItem, None);
412 let substs = tcx.intern_substs(&[ty.into()]);
413 Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap().unwrap()
416 pub fn fn_once_adapter_instance(
419 substs: ty::SubstsRef<'tcx>,
420 ) -> Instance<'tcx> {
421 debug!("fn_once_adapter_shim({:?}, {:?})", closure_did, substs);
422 let fn_once = tcx.require_lang_item(FnOnceTraitLangItem, None);
424 .associated_items(fn_once)
425 .in_definition_order()
426 .find(|it| it.kind == ty::AssocKind::Fn)
429 let def = ty::InstanceDef::ClosureOnceShim { call_once };
431 let self_ty = tcx.mk_closure(closure_did, substs);
433 let sig = substs.as_closure().sig();
434 let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
435 assert_eq!(sig.inputs().len(), 1);
436 let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
438 debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
439 Instance { def, substs }
442 /// FIXME(#69925) Depending on the kind of `InstanceDef`, the MIR body associated with an
443 /// instance is expressed in terms of the generic parameters of `self.def_id()`, and in other
444 /// cases the MIR body is expressed in terms of the types found in the substitution array.
445 /// In the former case, we want to substitute those generic types and replace them with the
446 /// values from the substs when monomorphizing the function body. But in the latter case, we
447 /// don't want to do that substitution, since it has already been done effectively.
449 /// This function returns `Some(substs)` in the former case and None otherwise -- i.e., if
450 /// this function returns `None`, then the MIR body does not require substitution during
451 /// monomorphization.
452 pub fn substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>> {
454 InstanceDef::CloneShim(..)
455 | InstanceDef::DropGlue(_, Some(_)) => None,
456 InstanceDef::ClosureOnceShim { .. }
457 | InstanceDef::DropGlue(..)
458 // FIXME(#69925): `FnPtrShim` should be in the other branch.
459 | InstanceDef::FnPtrShim(..)
460 | InstanceDef::Item(_)
461 | InstanceDef::Intrinsic(..)
462 | InstanceDef::ReifyShim(..)
463 | InstanceDef::Virtual(..)
464 | InstanceDef::VtableShim(..) => Some(self.substs),
468 /// Returns a new `Instance` where generic parameters in `instance.substs` are replaced by
469 /// identify parameters if they are determined to be unused in `instance.def`.
470 pub fn polymorphize(self, tcx: TyCtxt<'tcx>) -> Self {
471 debug!("polymorphize: running polymorphization analysis");
472 if !tcx.sess.opts.debugging_opts.polymorphize {
476 if let InstanceDef::Item(def) = self.def {
477 let polymorphized_substs = polymorphize(tcx, def.did, self.substs);
478 debug!("polymorphize: self={:?} polymorphized_substs={:?}", self, polymorphized_substs);
479 Self { def: self.def, substs: polymorphized_substs }
486 fn polymorphize<'tcx>(
489 substs: SubstsRef<'tcx>,
490 ) -> SubstsRef<'tcx> {
491 debug!("polymorphize({:?}, {:?})", def_id, substs);
492 let unused = tcx.unused_generic_params(def_id);
493 debug!("polymorphize: unused={:?}", unused);
495 struct PolymorphizationFolder<'tcx> {
499 impl ty::TypeFolder<'tcx> for PolymorphizationFolder<'tcx> {
500 fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
504 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
505 debug!("fold_ty: ty={:?}", ty);
507 ty::Closure(def_id, substs) => {
508 let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
509 if substs == polymorphized_substs {
512 self.tcx.mk_closure(def_id, polymorphized_substs)
515 ty::FnDef(def_id, substs) => {
516 let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
517 if substs == polymorphized_substs {
520 self.tcx.mk_fn_def(def_id, polymorphized_substs)
523 ty::Generator(def_id, substs, movability) => {
524 let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
525 if substs == polymorphized_substs {
528 self.tcx.mk_generator(def_id, polymorphized_substs, movability)
531 _ => ty.super_fold_with(self),
536 InternalSubsts::for_item(tcx, def_id, |param, _| {
537 let is_unused = unused.contains(param.index).unwrap_or(false);
538 debug!("polymorphize: param={:?} is_unused={:?}", param, is_unused);
540 // If parameter is a const or type parameter..
541 ty::GenericParamDefKind::Const | ty::GenericParamDefKind::Type { .. } if
542 // ..and is within range and unused..
543 unused.contains(param.index).unwrap_or(false) =>
544 // ..then use the identity for this parameter.
545 tcx.mk_param_from_def(param),
547 // If the parameter does not contain any closures or generators, then use the
548 // substitution directly.
549 _ if !substs.may_polymorphize() => substs[param.index as usize],
551 // Otherwise, use the substitution after polymorphizing.
553 let arg = substs[param.index as usize];
554 let polymorphized_arg = arg.fold_with(&mut PolymorphizationFolder { tcx });
555 debug!("polymorphize: arg={:?} polymorphized_arg={:?}", arg, polymorphized_arg);
556 ty::GenericArg::from(polymorphized_arg)
562 fn needs_fn_once_adapter_shim(
563 actual_closure_kind: ty::ClosureKind,
564 trait_closure_kind: ty::ClosureKind,
565 ) -> Result<bool, ()> {
566 match (actual_closure_kind, trait_closure_kind) {
567 (ty::ClosureKind::Fn, ty::ClosureKind::Fn)
568 | (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut)
569 | (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
570 // No adapter needed.
573 (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
574 // The closure fn `llfn` is a `fn(&self, ...)`. We want a
575 // `fn(&mut self, ...)`. In fact, at codegen time, these are
576 // basically the same thing, so we can just return llfn.
579 (ty::ClosureKind::Fn | ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
580 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
581 // self, ...)`. We want a `fn(self, ...)`. We can produce
582 // this by doing something like:
584 // fn call_once(self, ...) { call_mut(&self, ...) }
585 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
587 // These are both the same at codegen time.
590 (ty::ClosureKind::FnMut | ty::ClosureKind::FnOnce, _) => Err(()),