]> git.proxmox.com Git - rustc.git/blob - src/librustc_middle/ty/instance.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_middle / ty / instance.rs
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::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, Hash, Debug, TyEncodable, TyDecodable, HashStable)]
26 pub enum InstanceDef<'tcx> {
27 /// A user-defined callable item.
28 ///
29 /// This includes:
30 /// - `fn` items
31 /// - closures
32 /// - generators
33 Item(ty::WithOptConstParam<DefId>),
34
35 /// An intrinsic `fn` item (with `"rust-intrinsic"` or `"platform-intrinsic"` ABI).
36 ///
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
39 /// caller.
40 Intrinsic(DefId),
41
42 /// `<T as Trait>::method` where `method` receives unsizeable `self: Self` (part of the
43 /// `unsized_locals` feature).
44 ///
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.
47 VtableShim(DefId),
48
49 /// `fn()` pointer where the function itself cannot be turned into a pointer.
50 ///
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`).
54 ///
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).
60 ReifyShim(DefId),
61
62 /// `<fn() as FnTrait>::call_*` (generated `FnTrait` implementation for `fn()` pointers).
63 ///
64 /// `DefId` is `FnTrait::call_*`.
65 ///
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>),
70
71 /// Dynamic dispatch to `<dyn Trait as Trait>::fn`.
72 ///
73 /// This `InstanceDef` does not have callable MIR. Calls to `Virtual` instances must be
74 /// codegen'd as virtual calls through the vtable.
75 ///
76 /// If this is reified to a `fn` pointer, a `ReifyShim` is used (see `ReifyShim` above for more
77 /// details on that).
78 Virtual(DefId, usize),
79
80 /// `<[FnMut closure] as FnOnce>::call_once`.
81 ///
82 /// The `DefId` is the ID of the `call_once` method in `FnOnce`.
83 ClosureOnceShim { call_once: DefId },
84
85 /// `core::ptr::drop_in_place::<T>`.
86 ///
87 /// The `DefId` is for `core::ptr::drop_in_place`.
88 /// The `Option<Ty<'tcx>>` is either `Some(T)`, or `None` for empty drop
89 /// glue.
90 ///
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>>),
95
96 /// Compiler-generated `<T as Clone>::clone` implementation.
97 ///
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`.
100 ///
101 /// The `DefId` is for `Clone::clone`, the `Ty` is the type `T` with the builtin `Clone` impl.
102 ///
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>),
107 }
108
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)
115 }
116
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.
120 ///
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
126 // instead.
127 if !tcx.sess.opts.share_generics() {
128 return None;
129 }
130
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() {
134 return None;
135 }
136
137 // If this a non-generic instance, it cannot be a shared monomorphization.
138 self.substs.non_erasable_generics().next()?;
139
140 match self.def {
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),
145 _ => None,
146 }
147 }
148 }
149
150 impl<'tcx> InstanceDef<'tcx> {
151 #[inline]
152 pub fn def_id(self) -> DefId {
153 match self {
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,
163 }
164 }
165
166 #[inline]
167 pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> {
168 match self {
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),
178 }
179 }
180
181 #[inline]
182 pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
183 tcx.get_attrs(self.def_id())
184 }
185
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,
196 _ => return true,
197 };
198 match tcx.def_key(def_id).disambiguated_data.data {
199 DefPathData::Ctor | DefPathData::ClosureExpr => true,
200 _ => false,
201 }
202 }
203
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) {
213 return true;
214 }
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() {
220 return true;
221 }
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.
225 //
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()
232 })
233 });
234 }
235 tcx.codegen_fn_attrs(self.def_id()).requests_inline()
236 }
237
238 pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool {
239 match *self {
240 InstanceDef::Item(def) => {
241 tcx.codegen_fn_attrs(def.did).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
242 }
243 _ => false,
244 }
245 }
246 }
247
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)?;
254 Ok(())
255 })?;
256
257 match self.def {
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),
267 }
268 }
269 }
270
271 impl<'tcx> Instance<'tcx> {
272 pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx> {
273 assert!(
274 !substs.has_escaping_bound_vars(),
275 "substs of instance {:?} not normalized for codegen: {:?}",
276 def_id,
277 substs
278 );
279 Instance { def: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)), substs }
280 }
281
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))
284 }
285
286 #[inline]
287 pub fn def_id(&self) -> DefId {
288 self.def.def_id()
289 }
290
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,
293 /// if known.
294 ///
295 /// Returns `Ok(None)` if we cannot resolve `Instance` to a specific instance.
296 /// For example, in a context like this,
297 ///
298 /// ```
299 /// fn foo<T: Debug>(t: T) { ... }
300 /// ```
301 ///
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.)
305 ///
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))`.
309 ///
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
314 pub fn resolve(
315 tcx: TyCtxt<'tcx>,
316 param_env: ty::ParamEnv<'tcx>,
317 def_id: DefId,
318 substs: SubstsRef<'tcx>,
319 ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
320 Instance::resolve_opt_const_arg(
321 tcx,
322 param_env,
323 ty::WithOptConstParam::unknown(def_id),
324 substs,
325 )
326 }
327
328 // This should be kept up to date with `resolve`.
329 pub fn resolve_opt_const_arg(
330 tcx: TyCtxt<'tcx>,
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.
337
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);
342
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(&param_env.and((did, param_did, substs))),
347 )
348 } else {
349 tcx.resolve_instance(tcx.erase_regions(&param_env.and((def.did, substs))))
350 }
351 }
352
353 pub fn resolve_for_fn_ptr(
354 tcx: TyCtxt<'tcx>,
355 param_env: ty::ParamEnv<'tcx>,
356 def_id: DefId,
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| {
361 match resolved.def {
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);
365 }
366 InstanceDef::Virtual(def_id, _) => {
367 debug!(" => fn pointer created for virtual call");
368 resolved.def = InstanceDef::ReifyShim(def_id);
369 }
370 _ => {}
371 }
372
373 resolved
374 })
375 }
376
377 pub fn resolve_for_vtable(
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 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;
388 if is_vtable_shim {
389 debug!(" => associated item with unsizeable self: Self");
390 Some(Instance { def: InstanceDef::VtableShim(def_id), substs })
391 } else {
392 Instance::resolve_for_fn_ptr(tcx, param_env, def_id, substs)
393 }
394 }
395
396 pub fn resolve_closure(
397 tcx: TyCtxt<'tcx>,
398 def_id: DefId,
399 substs: ty::SubstsRef<'tcx>,
400 requested_kind: ty::ClosureKind,
401 ) -> Instance<'tcx> {
402 let actual_kind = substs.as_closure().kind();
403
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),
407 }
408 }
409
410 pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
411 let def_id = tcx.require_lang_item(LangItem::DropInPlace, None);
412 let substs = tcx.intern_substs(&[ty.into()]);
413 Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap().unwrap()
414 }
415
416 pub fn fn_once_adapter_instance(
417 tcx: TyCtxt<'tcx>,
418 closure_did: DefId,
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(LangItem::FnOnce, None);
423 let call_once = tcx
424 .associated_items(fn_once)
425 .in_definition_order()
426 .find(|it| it.kind == ty::AssocKind::Fn)
427 .unwrap()
428 .def_id;
429 let def = ty::InstanceDef::ClosureOnceShim { call_once };
430
431 let self_ty = tcx.mk_closure(closure_did, substs);
432
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()]);
437
438 debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
439 Instance { def, substs }
440 }
441
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.
448 ///
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>> {
453 match self.def {
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),
465 }
466 }
467
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 {
473 return self;
474 }
475
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 }
480 } else {
481 self
482 }
483 }
484 }
485
486 fn polymorphize<'tcx>(
487 tcx: TyCtxt<'tcx>,
488 def_id: DefId,
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);
494
495 // If this is a closure or generator then we need to handle the case where another closure
496 // from the function is captured as an upvar and hasn't been polymorphized. In this case,
497 // the unpolymorphized upvar closure would result in a polymorphized closure producing
498 // multiple mono items (and eventually symbol clashes).
499 let upvars_ty = if tcx.is_closure(def_id) {
500 Some(substs.as_closure().tupled_upvars_ty())
501 } else if tcx.type_of(def_id).is_generator() {
502 Some(substs.as_generator().tupled_upvars_ty())
503 } else {
504 None
505 };
506 let has_upvars = upvars_ty.map(|ty| ty.tuple_fields().count() > 0).unwrap_or(false);
507 debug!("polymorphize: upvars_ty={:?} has_upvars={:?}", upvars_ty, has_upvars);
508
509 struct PolymorphizationFolder<'tcx> {
510 tcx: TyCtxt<'tcx>,
511 };
512
513 impl ty::TypeFolder<'tcx> for PolymorphizationFolder<'tcx> {
514 fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
515 self.tcx
516 }
517
518 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
519 debug!("fold_ty: ty={:?}", ty);
520 match ty.kind {
521 ty::Closure(def_id, substs) => {
522 let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
523 if substs == polymorphized_substs {
524 ty
525 } else {
526 self.tcx.mk_closure(def_id, polymorphized_substs)
527 }
528 }
529 ty::Generator(def_id, substs, movability) => {
530 let polymorphized_substs = polymorphize(self.tcx, def_id, substs);
531 if substs == polymorphized_substs {
532 ty
533 } else {
534 self.tcx.mk_generator(def_id, polymorphized_substs, movability)
535 }
536 }
537 _ => ty.super_fold_with(self),
538 }
539 }
540 }
541
542 InternalSubsts::for_item(tcx, def_id, |param, _| {
543 let is_unused = unused.contains(param.index).unwrap_or(false);
544 debug!("polymorphize: param={:?} is_unused={:?}", param, is_unused);
545 match param.kind {
546 // Upvar case: If parameter is a type parameter..
547 ty::GenericParamDefKind::Type { .. } if
548 // ..and has upvars..
549 has_upvars &&
550 // ..and this param has the same type as the tupled upvars..
551 upvars_ty == Some(substs[param.index as usize].expect_ty()) => {
552 // ..then double-check that polymorphization marked it used..
553 debug_assert!(!is_unused);
554 // ..and polymorphize any closures/generators captured as upvars.
555 let upvars_ty = upvars_ty.unwrap();
556 let polymorphized_upvars_ty = upvars_ty.fold_with(
557 &mut PolymorphizationFolder { tcx });
558 debug!("polymorphize: polymorphized_upvars_ty={:?}", polymorphized_upvars_ty);
559 ty::GenericArg::from(polymorphized_upvars_ty)
560 },
561
562 // Simple case: If parameter is a const or type parameter..
563 ty::GenericParamDefKind::Const | ty::GenericParamDefKind::Type { .. } if
564 // ..and is within range and unused..
565 unused.contains(param.index).unwrap_or(false) =>
566 // ..then use the identity for this parameter.
567 tcx.mk_param_from_def(param),
568
569 // Otherwise, use the parameter as before.
570 _ => substs[param.index as usize],
571 }
572 })
573 }
574
575 fn needs_fn_once_adapter_shim(
576 actual_closure_kind: ty::ClosureKind,
577 trait_closure_kind: ty::ClosureKind,
578 ) -> Result<bool, ()> {
579 match (actual_closure_kind, trait_closure_kind) {
580 (ty::ClosureKind::Fn, ty::ClosureKind::Fn)
581 | (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut)
582 | (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
583 // No adapter needed.
584 Ok(false)
585 }
586 (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
587 // The closure fn `llfn` is a `fn(&self, ...)`. We want a
588 // `fn(&mut self, ...)`. In fact, at codegen time, these are
589 // basically the same thing, so we can just return llfn.
590 Ok(false)
591 }
592 (ty::ClosureKind::Fn | ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
593 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
594 // self, ...)`. We want a `fn(self, ...)`. We can produce
595 // this by doing something like:
596 //
597 // fn call_once(self, ...) { call_mut(&self, ...) }
598 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
599 //
600 // These are both the same at codegen time.
601 Ok(true)
602 }
603 (ty::ClosureKind::FnMut | ty::ClosureKind::FnOnce, _) => Err(()),
604 }
605 }