|
10 | 10 | use rustc_data_structures::base_n;
|
11 | 11 | use rustc_data_structures::fx::FxHashMap;
|
12 | 12 | use rustc_hir as hir;
|
| 13 | +use rustc_hir::lang_items::LangItem; |
13 | 14 | use rustc_middle::ty::layout::IntegerExt;
|
14 | 15 | use rustc_middle::ty::TypeVisitableExt;
|
15 | 16 | use rustc_middle::ty::{
|
@@ -1123,6 +1124,14 @@ pub fn typeid_for_instance<'tcx>(
|
1123 | 1124 | ) -> String {
|
1124 | 1125 | if matches!(instance.def, ty::InstanceDef::Virtual(..)) {
|
1125 | 1126 | instance.args = strip_receiver_auto(tcx, instance.args)
|
| 1127 | + } else if let ty::InstanceDef::VTableShim(def_id) = instance.def |
| 1128 | + && let Some(trait_id) = tcx.trait_of_item(def_id) |
| 1129 | + { |
| 1130 | + // VTableShims may have a trait method, but a concrete Self. This is not suitable for a vtable, |
| 1131 | + // as the caller will not know the concrete Self. |
| 1132 | + let trait_ref = ty::TraitRef::new(tcx, trait_id, instance.args); |
| 1133 | + let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref)); |
| 1134 | + instance.args = tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1)); |
1126 | 1135 | }
|
1127 | 1136 |
|
1128 | 1137 | if let Some(impl_id) = tcx.impl_of_method(instance.def_id())
|
@@ -1160,6 +1169,45 @@ pub fn typeid_for_instance<'tcx>(
|
1160 | 1169 | tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1));
|
1161 | 1170 | instance.args = instance.args.rebase_onto(tcx, impl_id, abstract_trait_args);
|
1162 | 1171 | }
|
| 1172 | + } else if tcx.is_closure_like(instance.def_id()) { |
| 1173 | + // We're either a closure or a coroutine. Our goal is to find the trait we're defined on, |
| 1174 | + // instantiate it, and take the type of its only method as our own. |
| 1175 | + let closure_ty = instance.ty(tcx, ty::ParamEnv::reveal_all()); |
| 1176 | + let (trait_id, inputs) = match closure_ty.kind() { |
| 1177 | + ty::Closure(..) => { |
| 1178 | + let closure_args = instance.args.as_closure(); |
| 1179 | + let trait_id = tcx.fn_trait_kind_to_def_id(closure_args.kind()).unwrap(); |
| 1180 | + let tuple_args = |
| 1181 | + tcx.instantiate_bound_regions_with_erased(closure_args.sig()).inputs()[0]; |
| 1182 | + (trait_id, tuple_args) |
| 1183 | + } |
| 1184 | + ty::Coroutine(..) => ( |
| 1185 | + tcx.require_lang_item(LangItem::Coroutine, None), |
| 1186 | + instance.args.as_coroutine().resume_ty(), |
| 1187 | + ), |
| 1188 | + ty::CoroutineClosure(..) => ( |
| 1189 | + tcx.require_lang_item(LangItem::FnOnce, None), |
| 1190 | + tcx.instantiate_bound_regions_with_erased( |
| 1191 | + instance.args.as_coroutine_closure().coroutine_closure_sig(), |
| 1192 | + ) |
| 1193 | + .tupled_inputs_ty, |
| 1194 | + ), |
| 1195 | + x => bug!("Unexpected type kind for closure-like: {x:?}"), |
| 1196 | + }; |
| 1197 | + let trait_ref = ty::TraitRef::new(tcx, trait_id, [closure_ty, inputs]); |
| 1198 | + let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref)); |
| 1199 | + let abstract_args = tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1)); |
| 1200 | + // There should be exactly one method on this trait, and it should be the one we're |
| 1201 | + // defining. |
| 1202 | + let call = tcx |
| 1203 | + .associated_items(trait_id) |
| 1204 | + .in_definition_order() |
| 1205 | + .find(|it| it.kind == ty::AssocKind::Fn) |
| 1206 | + .expect("No call-family function on closure-like Fn trait?") |
| 1207 | + .def_id; |
| 1208 | + |
| 1209 | + instance.def = ty::InstanceDef::Virtual(call, 0); |
| 1210 | + instance.args = abstract_args; |
1163 | 1211 | }
|
1164 | 1212 |
|
1165 | 1213 | let fn_abi = tcx
|
|
0 commit comments