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