summaryrefslogtreecommitdiffstats
path: root/src/test/ui/async-await/issue-68112.rs
blob: 9c705137a10564580ae16023f193c355ee6672d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// edition:2018
// revisions: no_drop_tracking drop_tracking
// [drop_tracking] compile-flags: -Zdrop-tracking=yes
// [no_drop_tracking] compile-flags: -Zdrop-tracking=no

use std::{
    cell::RefCell,
    future::Future,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

fn require_send(_: impl Send) {}

struct Ready<T>(Option<T>);
impl<T> Future for Ready<T> {
    type Output = T;
    fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
        Poll::Ready(self.0.take().unwrap())
    }
}
fn ready<T>(t: T) -> Ready<T> {
    Ready(Some(t))
}

fn make_non_send_future1() -> impl Future<Output = Arc<RefCell<i32>>> {
    ready(Arc::new(RefCell::new(0)))
}

fn test1() {
    let send_fut = async {
        let non_send_fut = make_non_send_future1();
        let _ = non_send_fut.await;
        ready(0).await;
    };
    require_send(send_fut);
    //~^ ERROR future cannot be sent between threads
}

fn test1_no_let() {
    let send_fut = async {
        let _ = make_non_send_future1().await;
        ready(0).await;
    };
    require_send(send_fut);
    //~^ ERROR future cannot be sent between threads
}

async fn ready2<T>(t: T) -> T {
    t
}
fn make_non_send_future2() -> impl Future<Output = Arc<RefCell<i32>>> {
    ready2(Arc::new(RefCell::new(0)))
}

// Ideally this test would have diagnostics similar to the test above, but right
// now it doesn't.
fn test2() {
    let send_fut = async {
        let non_send_fut = make_non_send_future2();
        let _ = non_send_fut.await;
        ready(0).await;
    };
    require_send(send_fut);
    //~^ ERROR `RefCell<i32>` cannot be shared between threads safely
}

fn main() {}