]> git.lizzy.rs Git - rust.git/blob - src/test/ui/async-await/issue-68112.rs
Auto merge of #103217 - mejrs:track, r=eholk
[rust.git] / src / test / ui / async-await / issue-68112.rs
1 // edition:2018
2 // revisions: no_drop_tracking drop_tracking
3 // [drop_tracking] compile-flags: -Zdrop-tracking=yes
4 // [no_drop_tracking] compile-flags: -Zdrop-tracking=no
5
6 use std::{
7     cell::RefCell,
8     future::Future,
9     pin::Pin,
10     sync::Arc,
11     task::{Context, Poll},
12 };
13
14 fn require_send(_: impl Send) {}
15
16 struct Ready<T>(Option<T>);
17 impl<T> Future for Ready<T> {
18     type Output = T;
19     fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
20         Poll::Ready(self.0.take().unwrap())
21     }
22 }
23 fn ready<T>(t: T) -> Ready<T> {
24     Ready(Some(t))
25 }
26
27 fn make_non_send_future1() -> impl Future<Output = Arc<RefCell<i32>>> {
28     ready(Arc::new(RefCell::new(0)))
29 }
30
31 fn test1() {
32     let send_fut = async {
33         let non_send_fut = make_non_send_future1();
34         let _ = non_send_fut.await;
35         ready(0).await;
36     };
37     require_send(send_fut);
38     //~^ ERROR future cannot be sent between threads
39 }
40
41 fn test1_no_let() {
42     let send_fut = async {
43         let _ = make_non_send_future1().await;
44         ready(0).await;
45     };
46     require_send(send_fut);
47     //~^ ERROR future cannot be sent between threads
48 }
49
50 async fn ready2<T>(t: T) -> T {
51     t
52 }
53 fn make_non_send_future2() -> impl Future<Output = Arc<RefCell<i32>>> {
54     ready2(Arc::new(RefCell::new(0)))
55 }
56
57 // Ideally this test would have diagnostics similar to the test above, but right
58 // now it doesn't.
59 fn test2() {
60     let send_fut = async {
61         let non_send_fut = make_non_send_future2();
62         let _ = non_send_fut.await;
63         ready(0).await;
64     };
65     require_send(send_fut);
66     //~^ ERROR `RefCell<i32>` cannot be shared between threads safely
67 }
68
69 fn main() {}