]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_middle/src/ty/instance.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / compiler / rustc_middle / src / ty / instance.rs
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 },
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.
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.
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]
141 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 }
150 | InstanceDef::DropGlue(def_id, _)
151 | InstanceDef::CloneShim(def_id, _) => def_id,
152 }
153 }
154
155 #[inline]
156 pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
157 match self {
158 InstanceDef::Item(def) => def,
159 InstanceDef::VtableShim(def_id)
160 | InstanceDef::ReifyShim(def_id)
161 | InstanceDef::FnPtrShim(def_id, _)
162 | InstanceDef::Virtual(def_id, _)
163 | InstanceDef::Intrinsic(def_id)
164 | InstanceDef::ClosureOnceShim { call_once: def_id }
165 | InstanceDef::DropGlue(def_id, _)
166 | InstanceDef::CloneShim(def_id, _) => ty::WithOptConstParam::unknown(def_id),
167 }
168 }
169
170 #[inline]
171 pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
172 tcx.get_attrs(self.def_id())
173 }
174
175 /// Returns `true` if the LLVM version of this instance is unconditionally
176 /// marked with `inline`. This implies that a copy of this instance is
177 /// generated in every codegen unit.
178 /// Note that this is only a hint. See the documentation for
179 /// `generates_cgu_internal_copy` for more information.
180 pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
181 use rustc_hir::definitions::DefPathData;
182 let def_id = match *self {
183 ty::InstanceDef::Item(def) => def.did,
184 ty::InstanceDef::DropGlue(_, Some(_)) => return false,
185 _ => return true,
186 };
187 matches!(
188 tcx.def_key(def_id).disambiguated_data.data,
189 DefPathData::Ctor | DefPathData::ClosureExpr
190 )
191 }
192
193 /// Returns `true` if the machine code for this instance is instantiated in
194 /// each codegen unit that references it.
195 /// Note that this is only a hint! The compiler can globally decide to *not*
196 /// do this in order to speed up compilation. CGU-internal copies are
197 /// only exist to enable inlining. If inlining is not performed (e.g. at
198 /// `-Copt-level=0`) then the time for generating them is wasted and it's
199 /// better to create a single copy with external linkage.
200 pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool {
201 if self.requires_inline(tcx) {
202 return true;
203 }
204 if let ty::InstanceDef::DropGlue(.., Some(ty)) = *self {
205 // Drop glue generally wants to be instantiated at every codegen
206 // unit, but without an #[inline] hint. We should make this
207 // available to normal end-users.
208 if tcx.sess.opts.incremental.is_none() {
209 return true;
210 }
211 // When compiling with incremental, we can generate a *lot* of
212 // codegen units. Including drop glue into all of them has a
213 // considerable compile time cost.
214 //
215 // We include enums without destructors to allow, say, optimizing
216 // drops of `Option::None` before LTO. We also respect the intent of
217 // `#[inline]` on `Drop::drop` implementations.
218 return ty.ty_adt_def().map_or(true, |adt_def| {
219 adt_def.destructor(tcx).map_or_else(
220 || adt_def.is_enum(),
221 |dtor| tcx.codegen_fn_attrs(dtor.did).requests_inline(),
222 )
223 });
224 }
225 tcx.codegen_fn_attrs(self.def_id()).requests_inline()
226 }
227
228 pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool {
229 match *self {
230 InstanceDef::Item(ty::WithOptConstParam { did: def_id, .. })
231 | InstanceDef::Virtual(def_id, _) => {
232 tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
233 }
234 _ => false,
235 }
236 }
237
238 /// Returns `true` when the MIR body associated with this instance should be monomorphized
239 /// by its users (e.g. codegen or miri) by substituting the `substs` from `Instance` (see
240 /// `Instance::substs_for_mir_body`).
241 ///
242 /// Otherwise, returns `false` only for some kinds of shims where the construction of the MIR
243 /// body should perform necessary substitutions.
244 pub fn has_polymorphic_mir_body(&self) -> bool {
245 match *self {
246 InstanceDef::CloneShim(..)
247 | InstanceDef::FnPtrShim(..)
248 | InstanceDef::DropGlue(_, Some(_)) => false,
249 InstanceDef::ClosureOnceShim { .. }
250 | InstanceDef::DropGlue(..)
251 | InstanceDef::Item(_)
252 | InstanceDef::Intrinsic(..)
253 | InstanceDef::ReifyShim(..)
254 | InstanceDef::Virtual(..)
255 | InstanceDef::VtableShim(..) => true,
256 }
257 }
258 }
259
260 impl<'tcx> fmt::Display for Instance<'tcx> {
261 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
262 ty::tls::with(|tcx| {
263 let substs = tcx.lift(self.substs).expect("could not lift for printing");
264 FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS)
265 .print_def_path(self.def_id(), substs)?;
266 Ok(())
267 })?;
268
269 match self.def {
270 InstanceDef::Item(_) => Ok(()),
271 InstanceDef::VtableShim(_) => write!(f, " - shim(vtable)"),
272 InstanceDef::ReifyShim(_) => write!(f, " - shim(reify)"),
273 InstanceDef::Intrinsic(_) => write!(f, " - intrinsic"),
274 InstanceDef::Virtual(_, num) => write!(f, " - virtual#{}", num),
275 InstanceDef::FnPtrShim(_, ty) => write!(f, " - shim({})", ty),
276 InstanceDef::ClosureOnceShim { .. } => write!(f, " - shim"),
277 InstanceDef::DropGlue(_, None) => write!(f, " - shim(None)"),
278 InstanceDef::DropGlue(_, Some(ty)) => write!(f, " - shim(Some({}))", ty),
279 InstanceDef::CloneShim(_, ty) => write!(f, " - shim({})", ty),
280 }
281 }
282 }
283
284 impl<'tcx> Instance<'tcx> {
285 pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx> {
286 assert!(
287 !substs.has_escaping_bound_vars(),
288 "substs of instance {:?} not normalized for codegen: {:?}",
289 def_id,
290 substs
291 );
292 Instance { def: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)), substs }
293 }
294
295 pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
296 let substs = InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind {
297 ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
298 ty::GenericParamDefKind::Type { .. } => {
299 bug!("Instance::mono: {:?} has type parameters", def_id)
300 }
301 ty::GenericParamDefKind::Const { .. } => {
302 bug!("Instance::mono: {:?} has const parameters", def_id)
303 }
304 });
305
306 Instance::new(def_id, substs)
307 }
308
309 #[inline]
310 pub fn def_id(&self) -> DefId {
311 self.def.def_id()
312 }
313
314 /// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly,
315 /// this is used to find the precise code that will run for a trait method invocation,
316 /// if known.
317 ///
318 /// Returns `Ok(None)` if we cannot resolve `Instance` to a specific instance.
319 /// For example, in a context like this,
320 ///
321 /// ```
322 /// fn foo<T: Debug>(t: T) { ... }
323 /// ```
324 ///
325 /// trying to resolve `Debug::fmt` applied to `T` will yield `Ok(None)`, because we do not
326 /// know what code ought to run. (Note that this setting is also affected by the
327 /// `RevealMode` in the parameter environment.)
328 ///
329 /// Presuming that coherence and type-check have succeeded, if this method is invoked
330 /// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
331 /// `Ok(Some(instance))`.
332 ///
333 /// Returns `Err(ErrorReported)` when the `Instance` resolution process
334 /// couldn't complete due to errors elsewhere - this is distinct
335 /// from `Ok(None)` to avoid misleading diagnostics when an error
336 /// has already been/will be emitted, for the original cause
337 pub fn resolve(
338 tcx: TyCtxt<'tcx>,
339 param_env: ty::ParamEnv<'tcx>,
340 def_id: DefId,
341 substs: SubstsRef<'tcx>,
342 ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
343 Instance::resolve_opt_const_arg(
344 tcx,
345 param_env,
346 ty::WithOptConstParam::unknown(def_id),
347 substs,
348 )
349 }
350
351 // This should be kept up to date with `resolve`.
352 #[instrument(level = "debug", skip(tcx))]
353 pub fn resolve_opt_const_arg(
354 tcx: TyCtxt<'tcx>,
355 param_env: ty::ParamEnv<'tcx>,
356 def: ty::WithOptConstParam<DefId>,
357 substs: SubstsRef<'tcx>,
358 ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
359 // All regions in the result of this query are erased, so it's
360 // fine to erase all of the input regions.
361
362 // HACK(eddyb) erase regions in `substs` first, so that `param_env.and(...)`
363 // below is more likely to ignore the bounds in scope (e.g. if the only
364 // generic parameters mentioned by `substs` were lifetime ones).
365 let substs = tcx.erase_regions(substs);
366
367 // FIXME(eddyb) should this always use `param_env.with_reveal_all()`?
368 if let Some((did, param_did)) = def.as_const_arg() {
369 tcx.resolve_instance_of_const_arg(
370 tcx.erase_regions(param_env.and((did, param_did, substs))),
371 )
372 } else {
373 tcx.resolve_instance(tcx.erase_regions(param_env.and((def.did, substs))))
374 }
375 }
376
377 pub fn resolve_for_fn_ptr(
378 tcx: TyCtxt<'tcx>,
379 param_env: ty::ParamEnv<'tcx>,
380 def_id: DefId,
381 substs: SubstsRef<'tcx>,
382 ) -> Option<Instance<'tcx>> {
383 debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
384 Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| {
385 match resolved.def {
386 InstanceDef::Item(def) if resolved.def.requires_caller_location(tcx) => {
387 debug!(" => fn pointer created for function with #[track_caller]");
388 resolved.def = InstanceDef::ReifyShim(def.did);
389 }
390 InstanceDef::Virtual(def_id, _) => {
391 debug!(" => fn pointer created for virtual call");
392 resolved.def = InstanceDef::ReifyShim(def_id);
393 }
394 _ => {}
395 }
396
397 resolved
398 })
399 }
400
401 pub fn resolve_for_vtable(
402 tcx: TyCtxt<'tcx>,
403 param_env: ty::ParamEnv<'tcx>,
404 def_id: DefId,
405 substs: SubstsRef<'tcx>,
406 ) -> Option<Instance<'tcx>> {
407 debug!("resolve_for_vtable(def_id={:?}, substs={:?})", def_id, substs);
408 let fn_sig = tcx.fn_sig(def_id);
409 let is_vtable_shim = !fn_sig.inputs().skip_binder().is_empty()
410 && fn_sig.input(0).skip_binder().is_param(0)
411 && tcx.generics_of(def_id).has_self;
412 if is_vtable_shim {
413 debug!(" => associated item with unsizeable self: Self");
414 Some(Instance { def: InstanceDef::VtableShim(def_id), substs })
415 } else {
416 Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| {
417 match resolved.def {
418 InstanceDef::Item(def) => {
419 // We need to generate a shim when we cannot guarantee that
420 // the caller of a trait object method will be aware of
421 // `#[track_caller]` - this ensures that the caller
422 // and callee ABI will always match.
423 //
424 // The shim is generated when all of these conditions are met:
425 //
426 // 1) The underlying method expects a caller location parameter
427 // in the ABI
428 if resolved.def.requires_caller_location(tcx)
429 // 2) The caller location parameter comes from having `#[track_caller]`
430 // on the implementation, and *not* on the trait method.
431 && !tcx.should_inherit_track_caller(def.did)
432 // If the method implementation comes from the trait definition itself
433 // (e.g. `trait Foo { #[track_caller] my_fn() { /* impl */ } }`),
434 // then we don't need to generate a shim. This check is needed because
435 // `should_inherit_track_caller` returns `false` if our method
436 // implementation comes from the trait block, and not an impl block
437 && !matches!(
438 tcx.opt_associated_item(def.did),
439 Some(ty::AssocItem {
440 container: ty::AssocItemContainer::TraitContainer(_),
441 ..
442 })
443 )
444 {
445 debug!(
446 " => vtable fn pointer created for function with #[track_caller]"
447 );
448 resolved.def = InstanceDef::ReifyShim(def.did);
449 }
450 }
451 InstanceDef::Virtual(def_id, _) => {
452 debug!(" => vtable fn pointer created for virtual call");
453 resolved.def = InstanceDef::ReifyShim(def_id);
454 }
455 _ => {}
456 }
457
458 resolved
459 })
460 }
461 }
462
463 pub fn resolve_closure(
464 tcx: TyCtxt<'tcx>,
465 def_id: DefId,
466 substs: ty::SubstsRef<'tcx>,
467 requested_kind: ty::ClosureKind,
468 ) -> Instance<'tcx> {
469 let actual_kind = substs.as_closure().kind();
470
471 match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
472 Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
473 _ => Instance::new(def_id, substs),
474 }
475 }
476
477 pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
478 let def_id = tcx.require_lang_item(LangItem::DropInPlace, None);
479 let substs = tcx.intern_substs(&[ty.into()]);
480 Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap().unwrap()
481 }
482
483 pub fn fn_once_adapter_instance(
484 tcx: TyCtxt<'tcx>,
485 closure_did: DefId,
486 substs: ty::SubstsRef<'tcx>,
487 ) -> Instance<'tcx> {
488 debug!("fn_once_adapter_shim({:?}, {:?})", closure_did, substs);
489 let fn_once = tcx.require_lang_item(LangItem::FnOnce, None);
490 let call_once = tcx
491 .associated_items(fn_once)
492 .in_definition_order()
493 .find(|it| it.kind == ty::AssocKind::Fn)
494 .unwrap()
495 .def_id;
496 let def = ty::InstanceDef::ClosureOnceShim { call_once };
497
498 let self_ty = tcx.mk_closure(closure_did, substs);
499
500 let sig = substs.as_closure().sig();
501 let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), sig);
502 assert_eq!(sig.inputs().len(), 1);
503 let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
504
505 debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
506 Instance { def, substs }
507 }
508
509 /// Depending on the kind of `InstanceDef`, the MIR body associated with an
510 /// instance is expressed in terms of the generic parameters of `self.def_id()`, and in other
511 /// cases the MIR body is expressed in terms of the types found in the substitution array.
512 /// In the former case, we want to substitute those generic types and replace them with the
513 /// values from the substs when monomorphizing the function body. But in the latter case, we
514 /// don't want to do that substitution, since it has already been done effectively.
515 ///
516 /// This function returns `Some(substs)` in the former case and `None` otherwise -- i.e., if
517 /// this function returns `None`, then the MIR body does not require substitution during
518 /// codegen.
519 fn substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>> {
520 if self.def.has_polymorphic_mir_body() { Some(self.substs) } else { None }
521 }
522
523 pub fn subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: &T) -> T
524 where
525 T: TypeFoldable<'tcx> + Copy,
526 {
527 if let Some(substs) = self.substs_for_mir_body() { v.subst(tcx, substs) } else { *v }
528 }
529
530 #[inline(always)]
531 pub fn subst_mir_and_normalize_erasing_regions<T>(
532 &self,
533 tcx: TyCtxt<'tcx>,
534 param_env: ty::ParamEnv<'tcx>,
535 v: T,
536 ) -> T
537 where
538 T: TypeFoldable<'tcx> + Clone,
539 {
540 if let Some(substs) = self.substs_for_mir_body() {
541 tcx.subst_and_normalize_erasing_regions(substs, param_env, v)
542 } else {
543 tcx.normalize_erasing_regions(param_env, v)
544 }
545 }
546
547 /// Returns a new `Instance` where generic parameters in `instance.substs` are replaced by
548 /// identity parameters if they are determined to be unused in `instance.def`.
549 pub fn polymorphize(self, tcx: TyCtxt<'tcx>) -> Self {
550 debug!("polymorphize: running polymorphization analysis");
551 if !tcx.sess.opts.debugging_opts.polymorphize {
552 return self;
553 }
554
555 if let InstanceDef::Item(def) = self.def {
556 let polymorphized_substs = polymorphize(tcx, def.did, self.substs);
557 debug!("polymorphize: self={:?} polymorphized_substs={:?}", self, polymorphized_substs);
558 Self { def: self.def, substs: polymorphized_substs }
559 } else {
560 self
561 }
562 }
563 }
564
565 fn polymorphize<'tcx>(
566 tcx: TyCtxt<'tcx>,
567 def_id: DefId,
568 substs: SubstsRef<'tcx>,
569 ) -> SubstsRef<'tcx> {
570 debug!("polymorphize({:?}, {:?})", def_id, substs);
571 let unused = tcx.unused_generic_params(def_id);
572 debug!("polymorphize: unused={:?}", unused);
573
574 // If this is a closure or generator then we need to handle the case where another closure
575 // from the function is captured as an upvar and hasn't been polymorphized. In this case,
576 // the unpolymorphized upvar closure would result in a polymorphized closure producing
577 // multiple mono items (and eventually symbol clashes).
578 let upvars_ty = if tcx.is_closure(def_id) {
579 Some(substs.as_closure().tupled_upvars_ty())
580 } else if tcx.type_of(def_id).is_generator() {
581 Some(substs.as_generator().tupled_upvars_ty())
582 } else {
583 None
584 };
585 let has_upvars = upvars_ty.map_or(false, |ty| ty.tuple_fields().count() > 0);
586 debug!("polymorphize: upvars_ty={:?} has_upvars={:?}", upvars_ty, has_upvars);
587
588 struct PolymorphizationFolder<'tcx> {
589 tcx: TyCtxt<'tcx>,
590 }
591
592 impl ty::TypeFolder<'tcx> for PolymorphizationFolder<'tcx> {
593 fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
594 self.tcx
595 }
596
597 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
598 debug!("fold_ty: ty={:?}", ty);
599 match ty.kind {
600 ty::Closure(def_id, substs) => {
601 let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
602 if substs == polymorphized_substs {
603 ty
604 } else {
605 self.tcx.mk_closure(def_id, polymorphized_substs)
606 }
607 }
608 ty::Generator(def_id, substs, movability) => {
609 let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
610 if substs == polymorphized_substs {
611 ty
612 } else {
613 self.tcx.mk_generator(def_id, polymorphized_substs, movability)
614 }
615 }
616 _ => ty.super_fold_with(self),
617 }
618 }
619 }
620
621 InternalSubsts::for_item(tcx, def_id, |param, _| {
622 let is_unused = unused.contains(param.index).unwrap_or(false);
623 debug!("polymorphize: param={:?} is_unused={:?}", param, is_unused);
624 match param.kind {
625 // Upvar case: If parameter is a type parameter..
626 ty::GenericParamDefKind::Type { .. } if
627 // ..and has upvars..
628 has_upvars &&
629 // ..and this param has the same type as the tupled upvars..
630 upvars_ty == Some(substs[param.index as usize].expect_ty()) => {
631 // ..then double-check that polymorphization marked it used..
632 debug_assert!(!is_unused);
633 // ..and polymorphize any closures/generators captured as upvars.
634 let upvars_ty = upvars_ty.unwrap();
635 let polymorphized_upvars_ty = upvars_ty.fold_with(
636 &mut PolymorphizationFolder { tcx });
637 debug!("polymorphize: polymorphized_upvars_ty={:?}", polymorphized_upvars_ty);
638 ty::GenericArg::from(polymorphized_upvars_ty)
639 },
640
641 // Simple case: If parameter is a const or type parameter..
642 ty::GenericParamDefKind::Const { .. } | ty::GenericParamDefKind::Type { .. } if
643 // ..and is within range and unused..
644 unused.contains(param.index).unwrap_or(false) =>
645 // ..then use the identity for this parameter.
646 tcx.mk_param_from_def(param),
647
648 // Otherwise, use the parameter as before.
649 _ => substs[param.index as usize],
650 }
651 })
652 }
653
654 fn needs_fn_once_adapter_shim(
655 actual_closure_kind: ty::ClosureKind,
656 trait_closure_kind: ty::ClosureKind,
657 ) -> Result<bool, ()> {
658 match (actual_closure_kind, trait_closure_kind) {
659 (ty::ClosureKind::Fn, ty::ClosureKind::Fn)
660 | (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut)
661 | (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
662 // No adapter needed.
663 Ok(false)
664 }
665 (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
666 // The closure fn `llfn` is a `fn(&self, ...)`. We want a
667 // `fn(&mut self, ...)`. In fact, at codegen time, these are
668 // basically the same thing, so we can just return llfn.
669 Ok(false)
670 }
671 (ty::ClosureKind::Fn | ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
672 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
673 // self, ...)`. We want a `fn(self, ...)`. We can produce
674 // this by doing something like:
675 //
676 // fn call_once(self, ...) { call_mut(&self, ...) }
677 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
678 //
679 // These are both the same at codegen time.
680 Ok(true)
681 }
682 (ty::ClosureKind::FnMut | ty::ClosureKind::FnOnce, _) => Err(()),
683 }
684 }