2019-12-24 05:30:02 +01:00
|
|
|
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::ty::print::{FmtPrinter, Printer};
|
2020-06-22 13:57:03 +01:00
|
|
|
use crate::ty::subst::InternalSubsts;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeFoldable};
|
2020-04-10 05:13:29 +03:00
|
|
|
use rustc_errors::ErrorReported;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def::Namespace;
|
2020-01-22 12:17:21 +01:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId};
|
2020-08-18 11:47:27 +01:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2018-12-03 01:14:35 +01:00
|
|
|
use rustc_macros::HashStable;
|
2017-02-08 18:31:03 +01:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
2020-06-14 20:20:06 +02:00
|
|
|
/// A monomorphized `InstanceDef`.
|
|
|
|
///
|
|
|
|
/// Monomorphization happens on-the-fly and no monomorphized MIR is ever created. Instead, this type
|
|
|
|
/// simply couples a potentially generic `InstanceDef` with some substs, and codegen and const eval
|
|
|
|
/// will do all required substitution as they run.
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
|
2019-11-15 18:19:52 +01:00
|
|
|
#[derive(HashStable, Lift)]
|
2017-02-08 18:31:03 +01:00
|
|
|
pub struct Instance<'tcx> {
|
|
|
|
pub def: InstanceDef<'tcx>,
|
2019-02-09 22:11:53 +08:00
|
|
|
pub substs: SubstsRef<'tcx>,
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
2020-06-11 15:49:57 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable, HashStable)]
|
2017-02-08 18:31:03 +01:00
|
|
|
pub enum InstanceDef<'tcx> {
|
2020-06-14 20:20:06 +02:00
|
|
|
/// A user-defined callable item.
|
|
|
|
///
|
|
|
|
/// This includes:
|
|
|
|
/// - `fn` items
|
|
|
|
/// - closures
|
|
|
|
/// - generators
|
2020-07-15 10:50:54 +02:00
|
|
|
Item(ty::WithOptConstParam<DefId>),
|
2020-06-14 20:20:06 +02:00
|
|
|
|
|
|
|
/// An intrinsic `fn` item (with `"rust-intrinsic"` or `"platform-intrinsic"` ABI).
|
|
|
|
///
|
|
|
|
/// Alongside `Virtual`, this is the only `InstanceDef` that does not have its own callable MIR.
|
|
|
|
/// Instead, codegen and const eval "magically" evaluate calls to intrinsics purely in the
|
|
|
|
/// caller.
|
2017-03-08 01:41:26 +02:00
|
|
|
Intrinsic(DefId),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2020-06-14 20:20:06 +02:00
|
|
|
/// `<T as Trait>::method` where `method` receives unsizeable `self: Self` (part of the
|
|
|
|
/// `unsized_locals` feature).
|
|
|
|
///
|
|
|
|
/// The generated shim will take `Self` via `*mut Self` - conceptually this is `&owned Self` -
|
|
|
|
/// and dereference the argument to call the original function.
|
2018-09-10 22:54:48 +09:00
|
|
|
VtableShim(DefId),
|
|
|
|
|
2019-10-09 21:02:54 -07:00
|
|
|
/// `fn()` pointer where the function itself cannot be turned into a pointer.
|
|
|
|
///
|
2019-10-29 20:56:21 +02:00
|
|
|
/// One example is `<dyn Trait as Trait>::fn`, where the shim contains
|
|
|
|
/// a virtual call, which codegen supports only via a direct call to the
|
|
|
|
/// `<dyn Trait as Trait>::fn` instance (an `InstanceDef::Virtual`).
|
|
|
|
///
|
|
|
|
/// Another example is functions annotated with `#[track_caller]`, which
|
|
|
|
/// must have their implicit caller location argument populated for a call.
|
|
|
|
/// Because this is a required part of the function's ABI but can't be tracked
|
|
|
|
/// as a property of the function pointer, we use a single "caller location"
|
|
|
|
/// (the definition of the function itself).
|
2019-10-03 19:10:34 -07:00
|
|
|
ReifyShim(DefId),
|
|
|
|
|
2020-06-14 20:20:06 +02:00
|
|
|
/// `<fn() as FnTrait>::call_*` (generated `FnTrait` implementation for `fn()` pointers).
|
|
|
|
///
|
2019-05-17 02:20:14 +01:00
|
|
|
/// `DefId` is `FnTrait::call_*`.
|
2017-02-08 18:31:03 +01:00
|
|
|
FnPtrShim(DefId, Ty<'tcx>),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2020-06-14 20:20:06 +02:00
|
|
|
/// Dynamic dispatch to `<dyn Trait as Trait>::fn`.
|
2019-11-27 13:59:59 +02:00
|
|
|
///
|
2020-06-14 20:20:06 +02:00
|
|
|
/// This `InstanceDef` does not have callable MIR. Calls to `Virtual` instances must be
|
|
|
|
/// codegen'd as virtual calls through the vtable.
|
|
|
|
///
|
|
|
|
/// If this is reified to a `fn` pointer, a `ReifyShim` is used (see `ReifyShim` above for more
|
|
|
|
/// details on that).
|
2017-03-08 01:41:26 +02:00
|
|
|
Virtual(DefId, usize),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2020-06-14 20:20:06 +02:00
|
|
|
/// `<[FnMut closure] as FnOnce>::call_once`.
|
|
|
|
///
|
|
|
|
/// The `DefId` is the ID of the `call_once` method in `FnOnce`.
|
|
|
|
ClosureOnceShim { call_once: DefId },
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2019-12-15 16:42:30 +00:00
|
|
|
/// `core::ptr::drop_in_place::<T>`.
|
2020-06-14 20:20:06 +02:00
|
|
|
///
|
2019-12-15 16:42:30 +00:00
|
|
|
/// The `DefId` is for `core::ptr::drop_in_place`.
|
|
|
|
/// The `Option<Ty<'tcx>>` is either `Some(T)`, or `None` for empty drop
|
|
|
|
/// glue.
|
2017-03-14 01:08:21 +02:00
|
|
|
DropGlue(DefId, Option<Ty<'tcx>>),
|
2017-08-04 14:44:12 +02:00
|
|
|
|
2020-06-14 20:20:06 +02:00
|
|
|
/// Compiler-generated `<T as Clone>::clone` implementation.
|
|
|
|
///
|
|
|
|
/// For all types that automatically implement `Copy`, a trivial `Clone` impl is provided too.
|
|
|
|
/// Additionally, arrays, tuples, and closures get a `Clone` shim even if they aren't `Copy`.
|
|
|
|
///
|
|
|
|
/// The `DefId` is for `Clone::clone`, the `Ty` is the type `T` with the builtin `Clone` impl.
|
2017-08-07 16:21:08 +02:00
|
|
|
CloneShim(DefId, Ty<'tcx>),
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 23:35:39 +03:00
|
|
|
impl<'tcx> Instance<'tcx> {
|
2020-06-22 13:57:03 +01:00
|
|
|
/// Returns the `Ty` corresponding to this `Instance`, with generic substitutions applied and
|
|
|
|
/// lifetimes erased, allowing a `ParamEnv` to be specified for use during normalization.
|
|
|
|
pub fn ty(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Ty<'tcx> {
|
2020-01-02 00:42:31 -05:00
|
|
|
let ty = tcx.type_of(self.def.def_id());
|
|
|
|
tcx.subst_and_normalize_erasing_regions(self.substs, param_env, &ty)
|
|
|
|
}
|
2020-01-22 12:17:21 +01:00
|
|
|
|
|
|
|
/// Finds a crate that contains a monomorphization of this instance that
|
|
|
|
/// can be linked to from the local crate. A return value of `None` means
|
|
|
|
/// no upstream crate provides such an exported monomorphization.
|
|
|
|
///
|
|
|
|
/// This method already takes into account the global `-Zshare-generics`
|
|
|
|
/// setting, always returning `None` if `share-generics` is off.
|
|
|
|
pub fn upstream_monomorphization(&self, tcx: TyCtxt<'tcx>) -> Option<CrateNum> {
|
|
|
|
// If we are not in share generics mode, we don't link to upstream
|
|
|
|
// monomorphizations but always instantiate our own internal versions
|
|
|
|
// instead.
|
|
|
|
if !tcx.sess.opts.share_generics() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is an item that is defined in the local crate, no upstream
|
|
|
|
// crate can know about it/provide a monomorphization.
|
|
|
|
if self.def_id().is_local() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this a non-generic instance, it cannot be a shared monomorphization.
|
2020-03-01 22:04:42 +01:00
|
|
|
self.substs.non_erasable_generics().next()?;
|
2020-01-22 12:17:21 +01:00
|
|
|
|
|
|
|
match self.def {
|
2020-07-03 19:13:39 +02:00
|
|
|
InstanceDef::Item(def) => tcx
|
|
|
|
.upstream_monomorphizations_for(def.did)
|
2020-01-22 12:17:21 +01:00
|
|
|
.and_then(|monos| monos.get(&self.substs).cloned()),
|
|
|
|
InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.substs),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 17:27:27 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
impl<'tcx> InstanceDef<'tcx> {
|
|
|
|
#[inline]
|
2020-07-03 19:13:39 +02:00
|
|
|
pub fn def_id(self) -> DefId {
|
|
|
|
match self {
|
|
|
|
InstanceDef::Item(def) => def.did,
|
|
|
|
InstanceDef::VtableShim(def_id)
|
2019-12-22 17:42:04 -05:00
|
|
|
| InstanceDef::ReifyShim(def_id)
|
|
|
|
| InstanceDef::FnPtrShim(def_id, _)
|
|
|
|
| InstanceDef::Virtual(def_id, _)
|
|
|
|
| InstanceDef::Intrinsic(def_id)
|
|
|
|
| InstanceDef::ClosureOnceShim { call_once: def_id }
|
|
|
|
| InstanceDef::DropGlue(def_id, _)
|
|
|
|
| InstanceDef::CloneShim(def_id, _) => def_id,
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:13:39 +02:00
|
|
|
#[inline]
|
2020-07-15 10:50:54 +02:00
|
|
|
pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
|
2020-07-03 19:13:39 +02:00
|
|
|
match self {
|
|
|
|
InstanceDef::Item(def) => def,
|
|
|
|
InstanceDef::VtableShim(def_id)
|
|
|
|
| InstanceDef::ReifyShim(def_id)
|
|
|
|
| InstanceDef::FnPtrShim(def_id, _)
|
|
|
|
| InstanceDef::Virtual(def_id, _)
|
|
|
|
| InstanceDef::Intrinsic(def_id)
|
|
|
|
| InstanceDef::ClosureOnceShim { call_once: def_id }
|
|
|
|
| InstanceDef::DropGlue(def_id, _)
|
2020-07-15 10:55:41 +02:00
|
|
|
| InstanceDef::CloneShim(def_id, _) => ty::WithOptConstParam::unknown(def_id),
|
2020-07-03 19:13:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
#[inline]
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
|
2017-02-08 18:31:03 +01:00
|
|
|
tcx.get_attrs(self.def_id())
|
|
|
|
}
|
2017-10-27 11:27:05 +02:00
|
|
|
|
2020-01-20 15:54:40 +01:00
|
|
|
/// Returns `true` if the LLVM version of this instance is unconditionally
|
|
|
|
/// marked with `inline`. This implies that a copy of this instance is
|
|
|
|
/// generated in every codegen unit.
|
|
|
|
/// Note that this is only a hint. See the documentation for
|
|
|
|
/// `generates_cgu_internal_copy` for more information.
|
|
|
|
pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
|
2020-03-24 09:09:42 +01:00
|
|
|
use rustc_hir::definitions::DefPathData;
|
2017-10-27 11:27:05 +02:00
|
|
|
let def_id = match *self {
|
2020-07-03 19:13:39 +02:00
|
|
|
ty::InstanceDef::Item(def) => def.did,
|
2017-10-27 11:27:05 +02:00
|
|
|
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => return true,
|
2017-10-27 11:27:05 +02:00
|
|
|
};
|
|
|
|
match tcx.def_key(def_id).disambiguated_data.data {
|
2019-03-24 17:49:58 +03:00
|
|
|
DefPathData::Ctor | DefPathData::ClosureExpr => true,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => false,
|
2017-10-27 11:27:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 15:54:40 +01:00
|
|
|
/// Returns `true` if the machine code for this instance is instantiated in
|
|
|
|
/// each codegen unit that references it.
|
|
|
|
/// Note that this is only a hint! The compiler can globally decide to *not*
|
|
|
|
/// do this in order to speed up compilation. CGU-internal copies are
|
|
|
|
/// only exist to enable inlining. If inlining is not performed (e.g. at
|
|
|
|
/// `-Copt-level=0`) then the time for generating them is wasted and it's
|
|
|
|
/// better to create a single copy with external linkage.
|
|
|
|
pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool {
|
|
|
|
if self.requires_inline(tcx) {
|
2019-12-22 17:42:04 -05:00
|
|
|
return true;
|
2017-10-27 11:27:05 +02:00
|
|
|
}
|
2019-12-15 16:42:30 +00:00
|
|
|
if let ty::InstanceDef::DropGlue(.., Some(ty)) = *self {
|
|
|
|
// Drop glue generally wants to be instantiated at every codegen
|
2017-10-27 11:27:05 +02:00
|
|
|
// unit, but without an #[inline] hint. We should make this
|
|
|
|
// available to normal end-users.
|
2019-12-15 16:42:30 +00:00
|
|
|
if tcx.sess.opts.incremental.is_none() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// When compiling with incremental, we can generate a *lot* of
|
|
|
|
// codegen units. Including drop glue into all of them has a
|
|
|
|
// considerable compile time cost.
|
|
|
|
//
|
|
|
|
// We include enums without destructors to allow, say, optimizing
|
|
|
|
// drops of `Option::None` before LTO. We also respect the intent of
|
|
|
|
// `#[inline]` on `Drop::drop` implementations.
|
|
|
|
return ty.ty_adt_def().map_or(true, |adt_def| {
|
|
|
|
adt_def.destructor(tcx).map_or(adt_def.is_enum(), |dtor| {
|
|
|
|
tcx.codegen_fn_attrs(dtor.did).requests_inline()
|
|
|
|
})
|
|
|
|
});
|
2017-10-27 11:27:05 +02:00
|
|
|
}
|
2018-11-17 15:36:06 +01:00
|
|
|
tcx.codegen_fn_attrs(self.def_id()).requests_inline()
|
2017-10-27 11:27:05 +02:00
|
|
|
}
|
2019-11-11 08:45:52 -08:00
|
|
|
|
|
|
|
pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool {
|
2020-01-16 19:23:45 -08:00
|
|
|
match *self {
|
2020-07-03 19:13:39 +02:00
|
|
|
InstanceDef::Item(def) => {
|
|
|
|
tcx.codegen_fn_attrs(def.did).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
|
2020-01-16 19:23:45 -08:00
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
2019-11-11 08:45:52 -08:00
|
|
|
}
|
2020-08-09 20:08:45 +01:00
|
|
|
|
|
|
|
/// Returns `true` when the MIR body associated with this instance should be monomorphized
|
|
|
|
/// by its users (e.g. codegen or miri) by substituting the `substs` from `Instance` (see
|
|
|
|
/// `Instance::substs_for_mir_body`).
|
|
|
|
///
|
|
|
|
/// Otherwise, returns `false` only for some kinds of shims where the construction of the MIR
|
|
|
|
/// body should perform necessary substitutions.
|
|
|
|
pub fn has_polymorphic_mir_body(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
InstanceDef::CloneShim(..)
|
|
|
|
| InstanceDef::FnPtrShim(..)
|
|
|
|
| InstanceDef::DropGlue(_, Some(_)) => false,
|
|
|
|
InstanceDef::ClosureOnceShim { .. }
|
|
|
|
| InstanceDef::DropGlue(..)
|
|
|
|
| InstanceDef::Item(_)
|
|
|
|
| InstanceDef::Intrinsic(..)
|
|
|
|
| InstanceDef::ReifyShim(..)
|
|
|
|
| InstanceDef::Virtual(..)
|
|
|
|
| InstanceDef::VtableShim(..) => true,
|
|
|
|
}
|
|
|
|
}
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> fmt::Display for Instance<'tcx> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-25 12:11:50 +02:00
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
let substs = tcx.lift(&self.substs).expect("could not lift for printing");
|
|
|
|
FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
|
2019-01-29 07:21:11 +02:00
|
|
|
.print_def_path(self.def_id(), substs)?;
|
2019-01-18 22:14:01 +02:00
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
match self.def {
|
2017-03-08 01:41:26 +02:00
|
|
|
InstanceDef::Item(_) => Ok(()),
|
2019-12-22 17:42:04 -05:00
|
|
|
InstanceDef::VtableShim(_) => write!(f, " - shim(vtable)"),
|
|
|
|
InstanceDef::ReifyShim(_) => write!(f, " - shim(reify)"),
|
|
|
|
InstanceDef::Intrinsic(_) => write!(f, " - intrinsic"),
|
|
|
|
InstanceDef::Virtual(_, num) => write!(f, " - virtual#{}", num),
|
2020-08-28 13:38:43 +03:00
|
|
|
InstanceDef::FnPtrShim(_, ty) => write!(f, " - shim({})", ty),
|
2019-12-22 17:42:04 -05:00
|
|
|
InstanceDef::ClosureOnceShim { .. } => write!(f, " - shim"),
|
2020-08-28 13:38:43 +03:00
|
|
|
InstanceDef::DropGlue(_, None) => write!(f, " - shim(None)"),
|
|
|
|
InstanceDef::DropGlue(_, Some(ty)) => write!(f, " - shim(Some({}))", ty),
|
|
|
|
InstanceDef::CloneShim(_, ty) => write!(f, " - shim({})", ty),
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> Instance<'tcx> {
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx> {
|
|
|
|
assert!(
|
|
|
|
!substs.has_escaping_bound_vars(),
|
|
|
|
"substs of instance {:?} not normalized for codegen: {:?}",
|
|
|
|
def_id,
|
|
|
|
substs
|
|
|
|
);
|
2020-07-15 10:55:41 +02:00
|
|
|
Instance { def: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)), substs }
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
|
2019-09-25 15:36:14 -04:00
|
|
|
Instance::new(def_id, tcx.empty_substs_for_def_id(def_id))
|
2017-02-08 18:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn def_id(&self) -> DefId {
|
|
|
|
self.def.def_id()
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly,
|
2017-12-06 09:25:29 +01:00
|
|
|
/// this is used to find the precise code that will run for a trait method invocation,
|
|
|
|
/// if known.
|
|
|
|
///
|
2020-04-10 05:13:29 +03:00
|
|
|
/// Returns `Ok(None)` if we cannot resolve `Instance` to a specific instance.
|
2017-12-06 09:25:29 +01:00
|
|
|
/// For example, in a context like this,
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn foo<T: Debug>(t: T) { ... }
|
|
|
|
/// ```
|
|
|
|
///
|
2020-04-10 05:13:29 +03:00
|
|
|
/// trying to resolve `Debug::fmt` applied to `T` will yield `Ok(None)`, because we do not
|
2017-12-06 09:25:29 +01:00
|
|
|
/// know what code ought to run. (Note that this setting is also affected by the
|
|
|
|
/// `RevealMode` in the parameter environment.)
|
|
|
|
///
|
|
|
|
/// Presuming that coherence and type-check have succeeded, if this method is invoked
|
2018-05-08 16:10:16 +03:00
|
|
|
/// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
|
2020-04-10 05:13:29 +03:00
|
|
|
/// `Ok(Some(instance))`.
|
|
|
|
///
|
|
|
|
/// Returns `Err(ErrorReported)` when the `Instance` resolution process
|
|
|
|
/// couldn't complete due to errors elsewhere - this is distinct
|
|
|
|
/// from `Ok(None)` to avoid misleading diagnostics when an error
|
|
|
|
/// has already been/will be emitted, for the original cause
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn resolve(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
2020-04-10 05:13:29 +03:00
|
|
|
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
2020-07-15 11:45:01 +02:00
|
|
|
Instance::resolve_opt_const_arg(
|
|
|
|
tcx,
|
|
|
|
param_env,
|
|
|
|
ty::WithOptConstParam::unknown(def_id),
|
|
|
|
substs,
|
|
|
|
)
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
|
2020-07-02 23:56:17 +02:00
|
|
|
// This should be kept up to date with `resolve`.
|
2020-07-15 11:45:01 +02:00
|
|
|
pub fn resolve_opt_const_arg(
|
2020-07-02 23:56:17 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2020-07-15 10:50:54 +02:00
|
|
|
def: ty::WithOptConstParam<DefId>,
|
2020-07-02 23:56:17 +02:00
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
2020-07-15 11:45:01 +02:00
|
|
|
// All regions in the result of this query are erased, so it's
|
|
|
|
// fine to erase all of the input regions.
|
|
|
|
|
|
|
|
// HACK(eddyb) erase regions in `substs` first, so that `param_env.and(...)`
|
|
|
|
// below is more likely to ignore the bounds in scope (e.g. if the only
|
|
|
|
// generic parameters mentioned by `substs` were lifetime ones).
|
2020-07-02 23:56:17 +02:00
|
|
|
let substs = tcx.erase_regions(&substs);
|
2020-07-08 01:03:19 +02:00
|
|
|
|
2020-07-15 11:45:01 +02:00
|
|
|
// FIXME(eddyb) should this always use `param_env.with_reveal_all()`?
|
2020-07-08 01:03:19 +02:00
|
|
|
if let Some((did, param_did)) = def.as_const_arg() {
|
|
|
|
tcx.resolve_instance_of_const_arg(
|
|
|
|
tcx.erase_regions(¶m_env.and((did, param_did, substs))),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
tcx.resolve_instance(tcx.erase_regions(¶m_env.and((def.did, substs))))
|
|
|
|
}
|
2020-07-02 23:56:17 +02:00
|
|
|
}
|
|
|
|
|
2019-10-05 14:24:07 -07:00
|
|
|
pub fn resolve_for_fn_ptr(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
) -> Option<Instance<'tcx>> {
|
|
|
|
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
|
2020-04-10 05:13:29 +03:00
|
|
|
Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| {
|
2019-10-11 07:44:01 -07:00
|
|
|
match resolved.def {
|
2020-07-03 19:13:39 +02:00
|
|
|
InstanceDef::Item(def) if resolved.def.requires_caller_location(tcx) => {
|
2019-10-11 07:44:01 -07:00
|
|
|
debug!(" => fn pointer created for function with #[track_caller]");
|
2020-07-03 19:13:39 +02:00
|
|
|
resolved.def = InstanceDef::ReifyShim(def.did);
|
2019-10-29 20:56:21 +02:00
|
|
|
}
|
|
|
|
InstanceDef::Virtual(def_id, _) => {
|
|
|
|
debug!(" => fn pointer created for virtual call");
|
|
|
|
resolved.def = InstanceDef::ReifyShim(def_id);
|
|
|
|
}
|
|
|
|
_ => {}
|
2019-10-10 07:31:22 -07:00
|
|
|
}
|
2019-10-29 20:56:21 +02:00
|
|
|
|
|
|
|
resolved
|
2019-10-10 07:31:22 -07:00
|
|
|
})
|
2019-10-05 14:24:07 -07:00
|
|
|
}
|
|
|
|
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn resolve_for_vtable(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
) -> Option<Instance<'tcx>> {
|
2018-09-10 23:00:50 +09:00
|
|
|
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
|
|
|
|
let fn_sig = tcx.fn_sig(def_id);
|
2020-02-28 14:20:33 +01:00
|
|
|
let is_vtable_shim = !fn_sig.inputs().skip_binder().is_empty()
|
2019-07-07 16:34:06 +01:00
|
|
|
&& fn_sig.input(0).skip_binder().is_param(0)
|
|
|
|
&& tcx.generics_of(def_id).has_self;
|
2018-09-10 23:00:50 +09:00
|
|
|
if is_vtable_shim {
|
|
|
|
debug!(" => associated item with unsizeable self: Self");
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(Instance { def: InstanceDef::VtableShim(def_id), substs })
|
2018-09-10 23:00:50 +09:00
|
|
|
} else {
|
2020-07-26 09:53:24 -07:00
|
|
|
Instance::resolve_for_fn_ptr(tcx, param_env, def_id, substs)
|
2018-09-10 23:00:50 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 09:25:29 +01:00
|
|
|
pub fn resolve_closure(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-10-02 10:52:43 +02:00
|
|
|
def_id: DefId,
|
2019-09-26 17:30:44 +00:00
|
|
|
substs: ty::SubstsRef<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
requested_kind: ty::ClosureKind,
|
|
|
|
) -> Instance<'tcx> {
|
2020-03-13 03:23:38 +02:00
|
|
|
let actual_kind = substs.as_closure().kind();
|
2017-12-06 09:25:29 +01:00
|
|
|
|
|
|
|
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
|
2019-09-26 17:30:44 +00:00
|
|
|
Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => Instance::new(def_id, substs),
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 23:00:50 +09:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
|
2020-08-18 11:47:27 +01:00
|
|
|
let def_id = tcx.require_lang_item(LangItem::DropInPlace, None);
|
2019-05-23 12:45:22 -05:00
|
|
|
let substs = tcx.intern_substs(&[ty.into()]);
|
2020-04-10 05:13:29 +03:00
|
|
|
Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap().unwrap()
|
2019-05-23 12:45:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fn_once_adapter_instance(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-05-23 12:45:22 -05:00
|
|
|
closure_did: DefId,
|
2019-09-26 16:09:51 +00:00
|
|
|
substs: ty::SubstsRef<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
) -> Instance<'tcx> {
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!("fn_once_adapter_shim({:?}, {:?})", closure_did, substs);
|
2020-08-18 11:47:27 +01:00
|
|
|
let fn_once = tcx.require_lang_item(LangItem::FnOnce, None);
|
2019-12-22 17:42:04 -05:00
|
|
|
let call_once = tcx
|
|
|
|
.associated_items(fn_once)
|
2020-02-17 13:09:01 -08:00
|
|
|
.in_definition_order()
|
2020-04-01 10:09:50 +08:00
|
|
|
.find(|it| it.kind == ty::AssocKind::Fn)
|
2019-12-22 17:42:04 -05:00
|
|
|
.unwrap()
|
|
|
|
.def_id;
|
2019-05-23 12:45:22 -05:00
|
|
|
let def = ty::InstanceDef::ClosureOnceShim { call_once };
|
|
|
|
|
|
|
|
let self_ty = tcx.mk_closure(closure_did, substs);
|
|
|
|
|
2020-03-13 03:23:38 +02:00
|
|
|
let sig = substs.as_closure().sig();
|
2019-05-23 12:45:22 -05:00
|
|
|
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
|
|
|
|
assert_eq!(sig.inputs().len(), 1);
|
|
|
|
let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
|
|
|
|
|
|
|
|
debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
|
|
|
|
Instance { def, substs }
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:08:45 +01:00
|
|
|
/// Depending on the kind of `InstanceDef`, the MIR body associated with an
|
2020-03-11 19:36:07 +00:00
|
|
|
/// instance is expressed in terms of the generic parameters of `self.def_id()`, and in other
|
|
|
|
/// cases the MIR body is expressed in terms of the types found in the substitution array.
|
|
|
|
/// In the former case, we want to substitute those generic types and replace them with the
|
|
|
|
/// values from the substs when monomorphizing the function body. But in the latter case, we
|
|
|
|
/// don't want to do that substitution, since it has already been done effectively.
|
|
|
|
///
|
2020-08-09 20:08:45 +01:00
|
|
|
/// This function returns `Some(substs)` in the former case and `None` otherwise -- i.e., if
|
2020-03-11 19:36:07 +00:00
|
|
|
/// this function returns `None`, then the MIR body does not require substitution during
|
2020-08-09 20:08:45 +01:00
|
|
|
/// codegen.
|
2020-03-11 19:36:07 +00:00
|
|
|
pub fn substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>> {
|
2020-08-09 20:08:45 +01:00
|
|
|
if self.def.has_polymorphic_mir_body() { Some(self.substs) } else { None }
|
2020-03-11 19:36:07 +00:00
|
|
|
}
|
2020-06-22 13:57:03 +01:00
|
|
|
|
|
|
|
/// Returns a new `Instance` where generic parameters in `instance.substs` are replaced by
|
|
|
|
/// identify parameters if they are determined to be unused in `instance.def`.
|
|
|
|
pub fn polymorphize(self, tcx: TyCtxt<'tcx>) -> Self {
|
|
|
|
debug!("polymorphize: running polymorphization analysis");
|
|
|
|
if !tcx.sess.opts.debugging_opts.polymorphize {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let InstanceDef::Item(def) = self.def {
|
2020-08-07 12:28:52 +01:00
|
|
|
let polymorphized_substs = polymorphize(tcx, def.did, self.substs);
|
2020-06-22 13:57:03 +01:00
|
|
|
debug!("polymorphize: self={:?} polymorphized_substs={:?}", self, polymorphized_substs);
|
|
|
|
Self { def: self.def, substs: polymorphized_substs }
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
|
2020-08-07 12:28:52 +01:00
|
|
|
fn polymorphize<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
) -> SubstsRef<'tcx> {
|
|
|
|
debug!("polymorphize({:?}, {:?})", def_id, substs);
|
|
|
|
let unused = tcx.unused_generic_params(def_id);
|
|
|
|
debug!("polymorphize: unused={:?}", unused);
|
|
|
|
|
2020-08-09 14:53:33 +01:00
|
|
|
// If this is a closure or generator then we need to handle the case where another closure
|
|
|
|
// from the function is captured as an upvar and hasn't been polymorphized. In this case,
|
|
|
|
// the unpolymorphized upvar closure would result in a polymorphized closure producing
|
|
|
|
// multiple mono items (and eventually symbol clashes).
|
|
|
|
let upvars_ty = if tcx.is_closure(def_id) {
|
|
|
|
Some(substs.as_closure().tupled_upvars_ty())
|
|
|
|
} else if tcx.type_of(def_id).is_generator() {
|
|
|
|
Some(substs.as_generator().tupled_upvars_ty())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let has_upvars = upvars_ty.map(|ty| ty.tuple_fields().count() > 0).unwrap_or(false);
|
|
|
|
debug!("polymorphize: upvars_ty={:?} has_upvars={:?}", upvars_ty, has_upvars);
|
|
|
|
|
2020-08-07 12:28:52 +01:00
|
|
|
struct PolymorphizationFolder<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
};
|
|
|
|
|
|
|
|
impl ty::TypeFolder<'tcx> for PolymorphizationFolder<'tcx> {
|
|
|
|
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
debug!("fold_ty: ty={:?}", ty);
|
|
|
|
match ty.kind {
|
|
|
|
ty::Closure(def_id, substs) => {
|
|
|
|
let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
|
2020-08-07 17:51:40 +01:00
|
|
|
if substs == polymorphized_substs {
|
|
|
|
ty
|
|
|
|
} else {
|
|
|
|
self.tcx.mk_closure(def_id, polymorphized_substs)
|
|
|
|
}
|
2020-08-07 12:28:52 +01:00
|
|
|
}
|
|
|
|
ty::Generator(def_id, substs, movability) => {
|
|
|
|
let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
|
2020-08-07 17:51:40 +01:00
|
|
|
if substs == polymorphized_substs {
|
|
|
|
ty
|
|
|
|
} else {
|
|
|
|
self.tcx.mk_generator(def_id, polymorphized_substs, movability)
|
|
|
|
}
|
2020-08-07 12:28:52 +01:00
|
|
|
}
|
|
|
|
_ => ty.super_fold_with(self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InternalSubsts::for_item(tcx, def_id, |param, _| {
|
|
|
|
let is_unused = unused.contains(param.index).unwrap_or(false);
|
|
|
|
debug!("polymorphize: param={:?} is_unused={:?}", param, is_unused);
|
|
|
|
match param.kind {
|
2020-08-09 14:53:33 +01:00
|
|
|
// Upvar case: If parameter is a type parameter..
|
|
|
|
ty::GenericParamDefKind::Type { .. } if
|
|
|
|
// ..and has upvars..
|
|
|
|
has_upvars &&
|
|
|
|
// ..and this param has the same type as the tupled upvars..
|
|
|
|
upvars_ty == Some(substs[param.index as usize].expect_ty()) => {
|
|
|
|
// ..then double-check that polymorphization marked it used..
|
|
|
|
debug_assert!(!is_unused);
|
|
|
|
// ..and polymorphize any closures/generators captured as upvars.
|
|
|
|
let upvars_ty = upvars_ty.unwrap();
|
|
|
|
let polymorphized_upvars_ty = upvars_ty.fold_with(
|
|
|
|
&mut PolymorphizationFolder { tcx });
|
|
|
|
debug!("polymorphize: polymorphized_upvars_ty={:?}", polymorphized_upvars_ty);
|
|
|
|
ty::GenericArg::from(polymorphized_upvars_ty)
|
|
|
|
},
|
|
|
|
|
|
|
|
// Simple case: If parameter is a const or type parameter..
|
2020-08-07 12:28:52 +01:00
|
|
|
ty::GenericParamDefKind::Const | ty::GenericParamDefKind::Type { .. } if
|
|
|
|
// ..and is within range and unused..
|
|
|
|
unused.contains(param.index).unwrap_or(false) =>
|
|
|
|
// ..then use the identity for this parameter.
|
|
|
|
tcx.mk_param_from_def(param),
|
|
|
|
|
2020-08-09 14:53:33 +01:00
|
|
|
// Otherwise, use the parameter as before.
|
|
|
|
_ => substs[param.index as usize],
|
2020-08-07 12:28:52 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-11 13:20:33 +03:00
|
|
|
fn needs_fn_once_adapter_shim(
|
|
|
|
actual_closure_kind: ty::ClosureKind,
|
|
|
|
trait_closure_kind: ty::ClosureKind,
|
|
|
|
) -> Result<bool, ()> {
|
2017-12-06 09:25:29 +01:00
|
|
|
match (actual_closure_kind, trait_closure_kind) {
|
2019-12-22 17:42:04 -05:00
|
|
|
(ty::ClosureKind::Fn, ty::ClosureKind::Fn)
|
|
|
|
| (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut)
|
|
|
|
| (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
|
|
|
|
// No adapter needed.
|
|
|
|
Ok(false)
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
(ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
|
|
|
|
// The closure fn `llfn` is a `fn(&self, ...)`. We want a
|
2018-05-08 16:10:16 +03:00
|
|
|
// `fn(&mut self, ...)`. In fact, at codegen time, these are
|
2017-12-06 09:25:29 +01:00
|
|
|
// basically the same thing, so we can just return llfn.
|
|
|
|
Ok(false)
|
|
|
|
}
|
2020-04-16 17:38:52 -07:00
|
|
|
(ty::ClosureKind::Fn | ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
// The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
|
|
|
|
// self, ...)`. We want a `fn(self, ...)`. We can produce
|
|
|
|
// this by doing something like:
|
|
|
|
//
|
|
|
|
// fn call_once(self, ...) { call_mut(&self, ...) }
|
|
|
|
// fn call_once(mut self, ...) { call_mut(&mut self, ...) }
|
|
|
|
//
|
|
|
|
// These are both the same at codegen time.
|
|
|
|
Ok(true)
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
2020-04-16 17:38:52 -07:00
|
|
|
(ty::ClosureKind::FnMut | ty::ClosureKind::FnOnce, _) => Err(()),
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
}
|