1
Fork 0

Build a shim to call async closures with different AsyncFn trait kinds

This commit is contained in:
Michael Goulet 2024-01-25 00:30:55 +00:00
parent a82bae2172
commit fc4fff4038
13 changed files with 175 additions and 11 deletions

View file

@ -111,11 +111,14 @@ fn fn_sig_for_fn_abi<'tcx>(
kind: ty::BoundRegionKind::BrEnv,
};
let env_region = ty::Region::new_bound(tcx, ty::INNERMOST, br);
let env_ty = tcx.closure_env_ty(
Ty::new_coroutine_closure(tcx, def_id, args),
args.as_coroutine_closure().kind(),
env_region,
);
let mut kind = args.as_coroutine_closure().kind();
if let InstanceDef::ConstructCoroutineInClosureShim { target_kind, .. } = instance.def {
kind = target_kind;
}
let env_ty =
tcx.closure_env_ty(Ty::new_coroutine_closure(tcx, def_id, args), kind, env_region);
let sig = sig.skip_binder();
ty::Binder::bind_with_vars(
@ -125,7 +128,7 @@ fn fn_sig_for_fn_abi<'tcx>(
tcx,
args.as_coroutine_closure().parent_args(),
tcx.coroutine_for_closure(def_id),
args.as_coroutine_closure().kind(),
kind,
env_region,
args.as_coroutine_closure().tupled_upvars_ty(),
args.as_coroutine_closure().coroutine_captures_by_ref_ty(),

View file

@ -283,10 +283,21 @@ fn resolve_associated_item<'tcx>(
tcx.item_name(trait_item_id)
),
}
} else if tcx.async_fn_trait_kind_from_def_id(trait_ref.def_id).is_some() {
} else if let Some(target_kind) = tcx.async_fn_trait_kind_from_def_id(trait_ref.def_id)
{
match *rcvr_args.type_at(0).kind() {
ty::CoroutineClosure(closure_def_id, args) => {
Some(Instance::new(closure_def_id, args))
ty::CoroutineClosure(coroutine_closure_def_id, args) => {
if target_kind > args.as_coroutine_closure().kind() {
Some(Instance {
def: ty::InstanceDef::ConstructCoroutineInClosureShim {
coroutine_closure_def_id,
target_kind,
},
args,
})
} else {
Some(Instance::new(coroutine_closure_def_id, args))
}
}
_ => bug!(
"no built-in definition for `{trait_ref}::{}` for non-lending-closure type",