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