|
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::{
|
@@ -1150,6 +1151,37 @@ pub fn typeid_for_instance<'tcx>(
|
1150 | 1151 | tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1));
|
1151 | 1152 | instance.args = instance.args.rebase_onto(tcx, impl_id, abstract_trait_args);
|
1152 | 1153 | }
|
| 1154 | + } else if tcx.is_closure_like(instance.def_id()) { |
| 1155 | + // We're either a closure or a coroutine. Our goal is to find the trait we're defined on, |
| 1156 | + // instantiate it, and take the type of its only method as our own. |
| 1157 | + let closure_ty = instance.ty(tcx, ty::ParamEnv::reveal_all()); |
| 1158 | + let (trait_id, tuple_args) = match closure_ty.kind() { |
| 1159 | + ty::Closure(_def_id, args) => { |
| 1160 | + let closure_args = ty::ClosureArgs { args }; |
| 1161 | + let trait_id = tcx.fn_trait_kind_to_def_id(closure_args.kind()).unwrap(); |
| 1162 | + let tuple_args = closure_args.sig().inputs().no_bound_vars().unwrap()[0]; |
| 1163 | + (trait_id, tuple_args) |
| 1164 | + } |
| 1165 | + ty::Coroutine(_def_id, args) => ( |
| 1166 | + tcx.require_lang_item(LangItem::Coroutine, None), |
| 1167 | + ty::CoroutineArgs { args }.resume_ty(), |
| 1168 | + ), |
| 1169 | + x => bug!("Unexpected type kind for closure-like: {x:?}"), |
| 1170 | + }; |
| 1171 | + let trait_ref = ty::TraitRef::new(tcx, trait_id, [closure_ty, tuple_args]); |
| 1172 | + let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref)); |
| 1173 | + let abstract_args = tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1)); |
| 1174 | + // There should be exactly one method on this trait, and it should be the one we're |
| 1175 | + // defining. |
| 1176 | + let call = tcx |
| 1177 | + .associated_items(trait_id) |
| 1178 | + .in_definition_order() |
| 1179 | + .find(|it| it.kind == ty::AssocKind::Fn) |
| 1180 | + .expect("No call-family function on closure-like Fn trait?") |
| 1181 | + .def_id; |
| 1182 | + |
| 1183 | + instance.def = ty::InstanceDef::Virtual(call, 0); |
| 1184 | + instance.args = abstract_args; |
1153 | 1185 | }
|
1154 | 1186 |
|
1155 | 1187 | let fn_abi = tcx
|
|
0 commit comments