Skip to content

Commit 5839756

Browse files
committed
Auto merge of #123402 - workingjubilee:rollup-0j5ihn6, r=workingjubilee
Rollup of 4 pull requests Successful merges: - #122411 ( Provide cabi_realloc on wasm32-wasip2 by default ) - #123349 (Fix capture analysis for by-move closure bodies) - #123359 (Link against libc++abi and libunwind as well when building LLVM wrappers on AIX) - #123388 (use a consistent style for links) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5b74593 + dd865ae commit 5839756

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

tests/pass/async-closure-captures.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Same as rustc's `tests/ui/async-await/async-closures/captures.rs`, keep in sync
2+
3+
#![feature(async_closure, noop_waker)]
4+
5+
use std::future::Future;
6+
use std::pin::pin;
7+
use std::task::*;
8+
9+
pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
10+
let mut fut = pin!(fut);
11+
let ctx = &mut Context::from_waker(Waker::noop());
12+
13+
loop {
14+
match fut.as_mut().poll(ctx) {
15+
Poll::Pending => {}
16+
Poll::Ready(t) => break t,
17+
}
18+
}
19+
}
20+
21+
fn main() {
22+
block_on(async_main());
23+
}
24+
25+
async fn call<T>(f: &impl async Fn() -> T) -> T {
26+
f().await
27+
}
28+
29+
async fn call_once<T>(f: impl async FnOnce() -> T) -> T {
30+
f().await
31+
}
32+
33+
#[derive(Debug)]
34+
#[allow(unused)]
35+
struct Hello(i32);
36+
37+
async fn async_main() {
38+
// Capture something by-ref
39+
{
40+
let x = Hello(0);
41+
let c = async || {
42+
println!("{x:?}");
43+
};
44+
call(&c).await;
45+
call_once(c).await;
46+
47+
let x = &Hello(1);
48+
let c = async || {
49+
println!("{x:?}");
50+
};
51+
call(&c).await;
52+
call_once(c).await;
53+
}
54+
55+
// Capture something and consume it (force to `AsyncFnOnce`)
56+
{
57+
let x = Hello(2);
58+
let c = async || {
59+
println!("{x:?}");
60+
drop(x);
61+
};
62+
call_once(c).await;
63+
}
64+
65+
// Capture something with `move`, don't consume it
66+
{
67+
let x = Hello(3);
68+
let c = async move || {
69+
println!("{x:?}");
70+
};
71+
call(&c).await;
72+
call_once(c).await;
73+
74+
let x = &Hello(4);
75+
let c = async move || {
76+
println!("{x:?}");
77+
};
78+
call(&c).await;
79+
call_once(c).await;
80+
}
81+
82+
// Capture something with `move`, also consume it (so `AsyncFnOnce`)
83+
{
84+
let x = Hello(5);
85+
let c = async move || {
86+
println!("{x:?}");
87+
drop(x);
88+
};
89+
call_once(c).await;
90+
}
91+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Hello(0)
2+
Hello(0)
3+
Hello(1)
4+
Hello(1)
5+
Hello(2)
6+
Hello(3)
7+
Hello(3)
8+
Hello(4)
9+
Hello(4)
10+
Hello(5)

0 commit comments

Comments
 (0)