]>
Commit | Line | Data |
---|---|---|
e74abb32 | 1 | use crate::hir::CodegenFnAttrFlags; |
9fa01778 | 2 | use crate::hir::Unsafety; |
532ac7d7 | 3 | use crate::hir::def::Namespace; |
9fa01778 | 4 | use crate::hir::def_id::DefId; |
532ac7d7 XL |
5 | use crate::ty::{self, Ty, PolyFnSig, TypeFoldable, SubstsRef, TyCtxt}; |
6 | use crate::ty::print::{FmtPrinter, Printer}; | |
9fa01778 | 7 | use crate::traits; |
dc9dc135 | 8 | use crate::middle::lang_items::DropInPlaceFnLangItem; |
83c7162d | 9 | use rustc_target::spec::abi::Abi; |
532ac7d7 | 10 | use rustc_macros::HashStable; |
cc61c64b XL |
11 | |
12 | use std::fmt; | |
a1dfa0c6 | 13 | use std::iter; |
cc61c64b | 14 | |
532ac7d7 | 15 | #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)] |
cc61c64b XL |
16 | pub struct Instance<'tcx> { |
17 | pub def: InstanceDef<'tcx>, | |
532ac7d7 | 18 | pub substs: SubstsRef<'tcx>, |
cc61c64b XL |
19 | } |
20 | ||
532ac7d7 | 21 | #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)] |
cc61c64b XL |
22 | pub enum InstanceDef<'tcx> { |
23 | Item(DefId), | |
24 | Intrinsic(DefId), | |
3b2f2976 | 25 | |
a1dfa0c6 XL |
26 | /// `<T as Trait>::method` where `method` receives unsizeable `self: Self`. |
27 | VtableShim(DefId), | |
28 | ||
e74abb32 XL |
29 | /// `fn()` pointer where the function itself cannot be turned into a pointer. |
30 | /// | |
31 | /// One example in the compiler today is functions annotated with `#[track_caller]`, which | |
32 | /// must have their implicit caller location argument populated for a call. Because this is a | |
33 | /// required part of the function's ABI but can't be tracked as a property of the function | |
34 | /// pointer, we create a single "caller location" at the site where the function is reified. | |
35 | ReifyShim(DefId), | |
36 | ||
9fa01778 XL |
37 | /// `<fn() as FnTrait>::call_*` |
38 | /// `DefId` is `FnTrait::call_*` | |
cc61c64b | 39 | FnPtrShim(DefId, Ty<'tcx>), |
3b2f2976 | 40 | |
9fa01778 | 41 | /// `<Trait as Trait>::fn` |
cc61c64b | 42 | Virtual(DefId, usize), |
3b2f2976 | 43 | |
9fa01778 | 44 | /// `<[mut closure] as FnOnce>::call_once` |
cc61c64b | 45 | ClosureOnceShim { call_once: DefId }, |
3b2f2976 | 46 | |
9fa01778 | 47 | /// `drop_in_place::<T>; None` for empty drop glue. |
cc61c64b | 48 | DropGlue(DefId, Option<Ty<'tcx>>), |
3b2f2976 | 49 | |
ea8adc8c | 50 | ///`<T as Clone>::clone` shim. |
3b2f2976 | 51 | CloneShim(DefId, Ty<'tcx>), |
cc61c64b XL |
52 | } |
53 | ||
dc9dc135 XL |
54 | impl<'tcx> Instance<'tcx> { |
55 | pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { | |
ff7c6d11 | 56 | let ty = tcx.type_of(self.def.def_id()); |
0531ce1d XL |
57 | tcx.subst_and_normalize_erasing_regions( |
58 | self.substs, | |
59 | ty::ParamEnv::reveal_all(), | |
60 | &ty, | |
61 | ) | |
ff7c6d11 | 62 | } |
a1dfa0c6 | 63 | |
dc9dc135 | 64 | fn fn_sig_noadjust(&self, tcx: TyCtxt<'tcx>) -> PolyFnSig<'tcx> { |
a1dfa0c6 | 65 | let ty = self.ty(tcx); |
e74abb32 | 66 | match ty.kind { |
a1dfa0c6 XL |
67 | ty::FnDef(..) | |
68 | // Shims currently have type FnPtr. Not sure this should remain. | |
69 | ty::FnPtr(_) => ty.fn_sig(tcx), | |
70 | ty::Closure(def_id, substs) => { | |
e74abb32 | 71 | let sig = substs.as_closure().sig(def_id, tcx); |
a1dfa0c6 XL |
72 | |
73 | let env_ty = tcx.closure_env_ty(def_id, substs).unwrap(); | |
74 | sig.map_bound(|sig| tcx.mk_fn_sig( | |
75 | iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()), | |
76 | sig.output(), | |
532ac7d7 | 77 | sig.c_variadic, |
a1dfa0c6 XL |
78 | sig.unsafety, |
79 | sig.abi | |
80 | )) | |
81 | } | |
82 | ty::Generator(def_id, substs, _) => { | |
e74abb32 | 83 | let sig = substs.as_generator().poly_sig(def_id, tcx); |
a1dfa0c6 XL |
84 | |
85 | let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv); | |
86 | let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty); | |
87 | ||
9fa01778 XL |
88 | let pin_did = tcx.lang_items().pin_type().unwrap(); |
89 | let pin_adt_ref = tcx.adt_def(pin_did); | |
90 | let pin_substs = tcx.intern_substs(&[env_ty.into()]); | |
91 | let env_ty = tcx.mk_adt(pin_adt_ref, pin_substs); | |
92 | ||
a1dfa0c6 XL |
93 | sig.map_bound(|sig| { |
94 | let state_did = tcx.lang_items().gen_state().unwrap(); | |
95 | let state_adt_ref = tcx.adt_def(state_did); | |
96 | let state_substs = tcx.intern_substs(&[ | |
97 | sig.yield_ty.into(), | |
98 | sig.return_ty.into(), | |
99 | ]); | |
100 | let ret_ty = tcx.mk_adt(state_adt_ref, state_substs); | |
101 | ||
102 | tcx.mk_fn_sig(iter::once(env_ty), | |
103 | ret_ty, | |
104 | false, | |
105 | Unsafety::Normal, | |
106 | Abi::Rust | |
107 | ) | |
108 | }) | |
109 | } | |
110 | _ => bug!("unexpected type {:?} in Instance::fn_sig_noadjust", ty) | |
111 | } | |
112 | } | |
113 | ||
dc9dc135 | 114 | pub fn fn_sig(&self, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> { |
a1dfa0c6 XL |
115 | let mut fn_sig = self.fn_sig_noadjust(tcx); |
116 | if let InstanceDef::VtableShim(..) = self.def { | |
117 | // Modify fn(self, ...) to fn(self: *mut Self, ...) | |
118 | fn_sig = fn_sig.map_bound(|mut fn_sig| { | |
119 | let mut inputs_and_output = fn_sig.inputs_and_output.to_vec(); | |
120 | inputs_and_output[0] = tcx.mk_mut_ptr(inputs_and_output[0]); | |
121 | fn_sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output); | |
122 | fn_sig | |
123 | }); | |
124 | } | |
125 | fn_sig | |
126 | } | |
ff7c6d11 XL |
127 | } |
128 | ||
cc61c64b XL |
129 | impl<'tcx> InstanceDef<'tcx> { |
130 | #[inline] | |
131 | pub fn def_id(&self) -> DefId { | |
132 | match *self { | |
133 | InstanceDef::Item(def_id) | | |
a1dfa0c6 | 134 | InstanceDef::VtableShim(def_id) | |
e74abb32 | 135 | InstanceDef::ReifyShim(def_id) | |
cc61c64b XL |
136 | InstanceDef::FnPtrShim(def_id, _) | |
137 | InstanceDef::Virtual(def_id, _) | | |
138 | InstanceDef::Intrinsic(def_id, ) | | |
3b2f2976 XL |
139 | InstanceDef::ClosureOnceShim { call_once: def_id } | |
140 | InstanceDef::DropGlue(def_id, _) | | |
141 | InstanceDef::CloneShim(def_id, _) => def_id | |
cc61c64b XL |
142 | } |
143 | } | |
144 | ||
cc61c64b | 145 | #[inline] |
dc9dc135 | 146 | pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> { |
cc61c64b XL |
147 | tcx.get_attrs(self.def_id()) |
148 | } | |
ff7c6d11 | 149 | |
dc9dc135 | 150 | pub fn is_inline(&self, tcx: TyCtxt<'tcx>) -> bool { |
9fa01778 | 151 | use crate::hir::map::DefPathData; |
ff7c6d11 XL |
152 | let def_id = match *self { |
153 | ty::InstanceDef::Item(def_id) => def_id, | |
154 | ty::InstanceDef::DropGlue(_, Some(_)) => return false, | |
155 | _ => return true | |
156 | }; | |
157 | match tcx.def_key(def_id).disambiguated_data.data { | |
532ac7d7 | 158 | DefPathData::Ctor | DefPathData::ClosureExpr => true, |
ff7c6d11 XL |
159 | _ => false |
160 | } | |
161 | } | |
162 | ||
dc9dc135 | 163 | pub fn requires_local(&self, tcx: TyCtxt<'tcx>) -> bool { |
ff7c6d11 XL |
164 | if self.is_inline(tcx) { |
165 | return true | |
166 | } | |
167 | if let ty::InstanceDef::DropGlue(..) = *self { | |
94b46f34 | 168 | // Drop glue wants to be instantiated at every codegen |
ff7c6d11 XL |
169 | // unit, but without an #[inline] hint. We should make this |
170 | // available to normal end-users. | |
171 | return true | |
172 | } | |
a1dfa0c6 | 173 | tcx.codegen_fn_attrs(self.def_id()).requests_inline() |
ff7c6d11 | 174 | } |
cc61c64b XL |
175 | } |
176 | ||
177 | impl<'tcx> fmt::Display for Instance<'tcx> { | |
0bf4aa26 | 178 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
532ac7d7 XL |
179 | ty::tls::with(|tcx| { |
180 | let substs = tcx.lift(&self.substs).expect("could not lift for printing"); | |
181 | FmtPrinter::new(tcx, &mut *f, Namespace::ValueNS) | |
182 | .print_def_path(self.def_id(), substs)?; | |
183 | Ok(()) | |
184 | })?; | |
185 | ||
cc61c64b XL |
186 | match self.def { |
187 | InstanceDef::Item(_) => Ok(()), | |
a1dfa0c6 XL |
188 | InstanceDef::VtableShim(_) => { |
189 | write!(f, " - shim(vtable)") | |
190 | } | |
e74abb32 XL |
191 | InstanceDef::ReifyShim(_) => { |
192 | write!(f, " - shim(reify)") | |
193 | } | |
cc61c64b XL |
194 | InstanceDef::Intrinsic(_) => { |
195 | write!(f, " - intrinsic") | |
196 | } | |
197 | InstanceDef::Virtual(_, num) => { | |
198 | write!(f, " - shim(#{})", num) | |
199 | } | |
200 | InstanceDef::FnPtrShim(_, ty) => { | |
201 | write!(f, " - shim({:?})", ty) | |
202 | } | |
203 | InstanceDef::ClosureOnceShim { .. } => { | |
204 | write!(f, " - shim") | |
205 | } | |
206 | InstanceDef::DropGlue(_, ty) => { | |
207 | write!(f, " - shim({:?})", ty) | |
208 | } | |
3b2f2976 XL |
209 | InstanceDef::CloneShim(_, ty) => { |
210 | write!(f, " - shim({:?})", ty) | |
211 | } | |
cc61c64b XL |
212 | } |
213 | } | |
214 | } | |
215 | ||
dc9dc135 | 216 | impl<'tcx> Instance<'tcx> { |
532ac7d7 | 217 | pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) |
cc61c64b | 218 | -> Instance<'tcx> { |
a1dfa0c6 | 219 | assert!(!substs.has_escaping_bound_vars(), |
94b46f34 | 220 | "substs of instance {:?} not normalized for codegen: {:?}", |
cc61c64b XL |
221 | def_id, substs); |
222 | Instance { def: InstanceDef::Item(def_id), substs: substs } | |
223 | } | |
224 | ||
dc9dc135 | 225 | pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> { |
e74abb32 | 226 | Instance::new(def_id, tcx.empty_substs_for_def_id(def_id)) |
cc61c64b XL |
227 | } |
228 | ||
229 | #[inline] | |
230 | pub fn def_id(&self) -> DefId { | |
231 | self.def.def_id() | |
232 | } | |
ea8adc8c | 233 | |
9fa01778 | 234 | /// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly, |
ea8adc8c XL |
235 | /// this is used to find the precise code that will run for a trait method invocation, |
236 | /// if known. | |
237 | /// | |
238 | /// Returns `None` if we cannot resolve `Instance` to a specific instance. | |
239 | /// For example, in a context like this, | |
240 | /// | |
241 | /// ``` | |
242 | /// fn foo<T: Debug>(t: T) { ... } | |
243 | /// ``` | |
244 | /// | |
245 | /// trying to resolve `Debug::fmt` applied to `T` will yield `None`, because we do not | |
246 | /// know what code ought to run. (Note that this setting is also affected by the | |
247 | /// `RevealMode` in the parameter environment.) | |
248 | /// | |
249 | /// Presuming that coherence and type-check have succeeded, if this method is invoked | |
94b46f34 | 250 | /// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return |
ea8adc8c | 251 | /// `Some`. |
dc9dc135 XL |
252 | pub fn resolve( |
253 | tcx: TyCtxt<'tcx>, | |
254 | param_env: ty::ParamEnv<'tcx>, | |
255 | def_id: DefId, | |
256 | substs: SubstsRef<'tcx>, | |
257 | ) -> Option<Instance<'tcx>> { | |
ea8adc8c XL |
258 | debug!("resolve(def_id={:?}, substs={:?})", def_id, substs); |
259 | let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) { | |
abe05a73 | 260 | debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env); |
ea8adc8c XL |
261 | let item = tcx.associated_item(def_id); |
262 | resolve_associated_item(tcx, &item, param_env, trait_def_id, substs) | |
263 | } else { | |
264 | let ty = tcx.type_of(def_id); | |
0531ce1d XL |
265 | let item_type = tcx.subst_and_normalize_erasing_regions( |
266 | substs, | |
267 | param_env, | |
268 | &ty, | |
269 | ); | |
ea8adc8c | 270 | |
e74abb32 | 271 | let def = match item_type.kind { |
b7449926 | 272 | ty::FnDef(..) if { |
ea8adc8c XL |
273 | let f = item_type.fn_sig(tcx); |
274 | f.abi() == Abi::RustIntrinsic || | |
275 | f.abi() == Abi::PlatformIntrinsic | |
276 | } => | |
277 | { | |
278 | debug!(" => intrinsic"); | |
279 | ty::InstanceDef::Intrinsic(def_id) | |
280 | } | |
281 | _ => { | |
282 | if Some(def_id) == tcx.lang_items().drop_in_place_fn() { | |
283 | let ty = substs.type_at(0); | |
0531ce1d | 284 | if ty.needs_drop(tcx, ty::ParamEnv::reveal_all()) { |
ea8adc8c XL |
285 | debug!(" => nontrivial drop glue"); |
286 | ty::InstanceDef::DropGlue(def_id, Some(ty)) | |
287 | } else { | |
288 | debug!(" => trivial drop glue"); | |
289 | ty::InstanceDef::DropGlue(def_id, None) | |
290 | } | |
291 | } else { | |
292 | debug!(" => free item"); | |
293 | ty::InstanceDef::Item(def_id) | |
294 | } | |
295 | } | |
296 | }; | |
297 | Some(Instance { | |
298 | def: def, | |
299 | substs: substs | |
300 | }) | |
301 | }; | |
302 | debug!("resolve(def_id={:?}, substs={:?}) = {:?}", def_id, substs, result); | |
303 | result | |
304 | } | |
ea8adc8c | 305 | |
e74abb32 XL |
306 | pub fn resolve_for_fn_ptr( |
307 | tcx: TyCtxt<'tcx>, | |
308 | param_env: ty::ParamEnv<'tcx>, | |
309 | def_id: DefId, | |
310 | substs: SubstsRef<'tcx>, | |
311 | ) -> Option<Instance<'tcx>> { | |
312 | debug!("resolve(def_id={:?}, substs={:?})", def_id, substs); | |
313 | Instance::resolve(tcx, param_env, def_id, substs).map(|resolved| { | |
314 | let has_track_caller = |def| tcx.codegen_fn_attrs(def).flags | |
315 | .contains(CodegenFnAttrFlags::TRACK_CALLER); | |
316 | ||
317 | match resolved.def { | |
318 | InstanceDef::Item(def_id) if has_track_caller(def_id) => { | |
319 | debug!(" => fn pointer created for function with #[track_caller]"); | |
320 | Instance { | |
321 | def: InstanceDef::ReifyShim(def_id), | |
322 | substs, | |
323 | } | |
324 | }, | |
325 | _ => resolved, | |
326 | } | |
327 | }) | |
328 | } | |
329 | ||
dc9dc135 XL |
330 | pub fn resolve_for_vtable( |
331 | tcx: TyCtxt<'tcx>, | |
332 | param_env: ty::ParamEnv<'tcx>, | |
333 | def_id: DefId, | |
334 | substs: SubstsRef<'tcx>, | |
335 | ) -> Option<Instance<'tcx>> { | |
a1dfa0c6 XL |
336 | debug!("resolve(def_id={:?}, substs={:?})", def_id, substs); |
337 | let fn_sig = tcx.fn_sig(def_id); | |
e1599b0c XL |
338 | let is_vtable_shim = fn_sig.inputs().skip_binder().len() > 0 |
339 | && fn_sig.input(0).skip_binder().is_param(0) | |
340 | && tcx.generics_of(def_id).has_self; | |
a1dfa0c6 XL |
341 | if is_vtable_shim { |
342 | debug!(" => associated item with unsizeable self: Self"); | |
343 | Some(Instance { | |
344 | def: InstanceDef::VtableShim(def_id), | |
345 | substs, | |
346 | }) | |
347 | } else { | |
348 | Instance::resolve(tcx, param_env, def_id, substs) | |
349 | } | |
350 | } | |
351 | ||
ff7c6d11 | 352 | pub fn resolve_closure( |
dc9dc135 | 353 | tcx: TyCtxt<'tcx>, |
0bf4aa26 | 354 | def_id: DefId, |
e74abb32 | 355 | substs: ty::SubstsRef<'tcx>, |
dc9dc135 XL |
356 | requested_kind: ty::ClosureKind, |
357 | ) -> Instance<'tcx> { | |
e74abb32 | 358 | let actual_kind = substs.as_closure().kind(def_id, tcx); |
ea8adc8c | 359 | |
ff7c6d11 | 360 | match needs_fn_once_adapter_shim(actual_kind, requested_kind) { |
dc9dc135 | 361 | Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs), |
e74abb32 | 362 | _ => Instance::new(def_id, substs) |
ff7c6d11 | 363 | } |
ea8adc8c | 364 | } |
a1dfa0c6 | 365 | |
dc9dc135 | 366 | pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> { |
e1599b0c | 367 | let def_id = tcx.require_lang_item(DropInPlaceFnLangItem, None); |
dc9dc135 XL |
368 | let substs = tcx.intern_substs(&[ty.into()]); |
369 | Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap() | |
370 | } | |
371 | ||
372 | pub fn fn_once_adapter_instance( | |
373 | tcx: TyCtxt<'tcx>, | |
374 | closure_did: DefId, | |
e74abb32 | 375 | substs: ty::SubstsRef<'tcx>, |
dc9dc135 XL |
376 | ) -> Instance<'tcx> { |
377 | debug!("fn_once_adapter_shim({:?}, {:?})", | |
378 | closure_did, | |
379 | substs); | |
380 | let fn_once = tcx.lang_items().fn_once_trait().unwrap(); | |
381 | let call_once = tcx.associated_items(fn_once) | |
382 | .find(|it| it.kind == ty::AssocKind::Method) | |
383 | .unwrap().def_id; | |
384 | let def = ty::InstanceDef::ClosureOnceShim { call_once }; | |
385 | ||
386 | let self_ty = tcx.mk_closure(closure_did, substs); | |
387 | ||
e74abb32 | 388 | let sig = substs.as_closure().sig(closure_did, tcx); |
dc9dc135 XL |
389 | let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig); |
390 | assert_eq!(sig.inputs().len(), 1); | |
391 | let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]); | |
392 | ||
393 | debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig); | |
394 | Instance { def, substs } | |
395 | } | |
396 | ||
a1dfa0c6 XL |
397 | pub fn is_vtable_shim(&self) -> bool { |
398 | if let InstanceDef::VtableShim(..) = self.def { | |
399 | true | |
400 | } else { | |
401 | false | |
402 | } | |
403 | } | |
ea8adc8c XL |
404 | } |
405 | ||
dc9dc135 XL |
406 | fn resolve_associated_item<'tcx>( |
407 | tcx: TyCtxt<'tcx>, | |
408 | trait_item: &ty::AssocItem, | |
ea8adc8c XL |
409 | param_env: ty::ParamEnv<'tcx>, |
410 | trait_id: DefId, | |
532ac7d7 | 411 | rcvr_substs: SubstsRef<'tcx>, |
ff7c6d11 | 412 | ) -> Option<Instance<'tcx>> { |
ea8adc8c XL |
413 | let def_id = trait_item.def_id; |
414 | debug!("resolve_associated_item(trait_item={:?}, \ | |
0731742a | 415 | param_env={:?}, \ |
0bf4aa26 XL |
416 | trait_id={:?}, \ |
417 | rcvr_substs={:?})", | |
0731742a | 418 | def_id, param_env, trait_id, rcvr_substs); |
ea8adc8c XL |
419 | |
420 | let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs); | |
94b46f34 | 421 | let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref))); |
ea8adc8c XL |
422 | |
423 | // Now that we know which impl is being used, we can dispatch to | |
424 | // the actual function: | |
425 | match vtbl { | |
426 | traits::VtableImpl(impl_data) => { | |
427 | let (def_id, substs) = traits::find_associated_item( | |
0731742a | 428 | tcx, param_env, trait_item, rcvr_substs, &impl_data); |
ea8adc8c XL |
429 | let substs = tcx.erase_regions(&substs); |
430 | Some(ty::Instance::new(def_id, substs)) | |
431 | } | |
94b46f34 | 432 | traits::VtableGenerator(generator_data) => { |
ea8adc8c | 433 | Some(Instance { |
94b46f34 | 434 | def: ty::InstanceDef::Item(generator_data.generator_def_id), |
e74abb32 | 435 | substs: generator_data.substs |
ea8adc8c XL |
436 | }) |
437 | } | |
438 | traits::VtableClosure(closure_data) => { | |
439 | let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap(); | |
ff7c6d11 | 440 | Some(Instance::resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs, |
0bf4aa26 | 441 | trait_closure_kind)) |
ea8adc8c XL |
442 | } |
443 | traits::VtableFnPointer(ref data) => { | |
444 | Some(Instance { | |
445 | def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty), | |
446 | substs: rcvr_substs | |
447 | }) | |
448 | } | |
449 | traits::VtableObject(ref data) => { | |
450 | let index = tcx.get_vtable_index_of_object_method(data, def_id); | |
451 | Some(Instance { | |
452 | def: ty::InstanceDef::Virtual(def_id, index), | |
453 | substs: rcvr_substs | |
454 | }) | |
455 | } | |
456 | traits::VtableBuiltin(..) => { | |
b7449926 | 457 | if tcx.lang_items().clone_trait().is_some() { |
ea8adc8c XL |
458 | Some(Instance { |
459 | def: ty::InstanceDef::CloneShim(def_id, trait_ref.self_ty()), | |
460 | substs: rcvr_substs | |
461 | }) | |
462 | } else { | |
463 | None | |
464 | } | |
465 | } | |
a1dfa0c6 XL |
466 | traits::VtableAutoImpl(..) | |
467 | traits::VtableParam(..) | | |
468 | traits::VtableTraitAlias(..) => None | |
ea8adc8c XL |
469 | } |
470 | } | |
471 | ||
dc9dc135 XL |
472 | fn needs_fn_once_adapter_shim( |
473 | actual_closure_kind: ty::ClosureKind, | |
474 | trait_closure_kind: ty::ClosureKind, | |
475 | ) -> Result<bool, ()> { | |
ea8adc8c XL |
476 | match (actual_closure_kind, trait_closure_kind) { |
477 | (ty::ClosureKind::Fn, ty::ClosureKind::Fn) | | |
478 | (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) | | |
479 | (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => { | |
480 | // No adapter needed. | |
481 | Ok(false) | |
482 | } | |
483 | (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => { | |
484 | // The closure fn `llfn` is a `fn(&self, ...)`. We want a | |
94b46f34 | 485 | // `fn(&mut self, ...)`. In fact, at codegen time, these are |
ea8adc8c XL |
486 | // basically the same thing, so we can just return llfn. |
487 | Ok(false) | |
488 | } | |
489 | (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) | | |
490 | (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => { | |
491 | // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut | |
492 | // self, ...)`. We want a `fn(self, ...)`. We can produce | |
493 | // this by doing something like: | |
494 | // | |
495 | // fn call_once(self, ...) { call_mut(&self, ...) } | |
496 | // fn call_once(mut self, ...) { call_mut(&mut self, ...) } | |
497 | // | |
94b46f34 | 498 | // These are both the same at codegen time. |
ea8adc8c XL |
499 | Ok(true) |
500 | } | |
501 | (ty::ClosureKind::FnMut, _) | | |
502 | (ty::ClosureKind::FnOnce, _) => Err(()) | |
503 | } | |
504 | } |