Skip to content

Commit 2252ff7

Browse files
Also support fnptr(): async Fn in codegen
1 parent 4c0016a commit 2252ff7

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

compiler/rustc_mir_transform/src/shim.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
3737
}
3838
ty::InstanceDef::FnPtrShim(def_id, ty) => {
3939
let trait_ = tcx.trait_of_item(def_id).unwrap();
40-
let adjustment = match tcx.fn_trait_kind_from_def_id(trait_) {
40+
// Supports `Fn` or `async Fn` traits.
41+
let adjustment = match tcx
42+
.fn_trait_kind_from_def_id(trait_)
43+
.or_else(|| tcx.async_fn_trait_kind_from_def_id(trait_))
44+
{
4145
Some(ty::ClosureKind::FnOnce) => Adjustment::Identity,
4246
Some(ty::ClosureKind::Fn) => Adjustment::Deref { source: DerefSource::ImmRef },
4347
Some(ty::ClosureKind::FnMut) => Adjustment::Deref { source: DerefSource::MutRef },
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@ aux-build:block-on.rs
2+
//@ edition:2018
3+
//@ revisions: current next
4+
//@[next] compile-flags: -Znext-solver
5+
//@ build-pass (since it ICEs during mono)
6+
7+
#![feature(async_closure)]
8+
9+
extern crate block_on;
10+
11+
use std::future::Future;
12+
13+
async fn f(arg: &i32) {}
14+
15+
async fn func<F>(f: F)
16+
where
17+
F: async for<'a> Fn(&'a i32),
18+
{
19+
let x: i32 = 0;
20+
f(&x).await;
21+
}
22+
23+
fn main() {
24+
block_on::block_on(async {
25+
// Function
26+
func(f).await;
27+
28+
// Regular closure (doesn't capture)
29+
func(|x: &i32| async {});
30+
});
31+
}

0 commit comments

Comments
 (0)