]> git.lizzy.rs Git - rust.git/blob - src/test/ui/generator/issue-93161.rs
Auto merge of #98051 - davidtwco:split-dwarf-stabilization, r=wesleywiser
[rust.git] / src / test / ui / generator / issue-93161.rs
1 // edition:2021
2 // run-pass
3 // compile-flags: -Zdrop-tracking
4
5 #![feature(never_type)]
6
7 use std::future::Future;
8
9 // See if we can run a basic `async fn`
10 pub async fn foo(x: &u32, y: u32) -> u32 {
11     let y = &y;
12     let z = 9;
13     let z = &z;
14     let y = async { *y + *z }.await;
15     let a = 10;
16     let a = &a;
17     *x + y + *a
18 }
19
20 async fn add(x: u32, y: u32) -> u32 {
21     let a = async { x + y };
22     a.await
23 }
24
25 async fn build_aggregate(a: u32, b: u32, c: u32, d: u32) -> u32 {
26     let x = (add(a, b).await, add(c, d).await);
27     x.0 + x.1
28 }
29
30 enum Never {}
31 fn never() -> Never {
32     panic!()
33 }
34
35 async fn includes_never(crash: bool, x: u32) -> u32 {
36     let result = async { x * x }.await;
37     if !crash {
38         return result;
39     }
40     #[allow(unused)]
41     let bad = never();
42     result *= async { x + x }.await;
43     drop(bad);
44     result
45 }
46
47 async fn partial_init(x: u32) -> u32 {
48     #[allow(unreachable_code)]
49     let _x: (String, !) = (String::new(), return async { x + x }.await);
50 }
51
52 async fn read_exact(_from: &mut &[u8], _to: &mut [u8]) -> Option<()> {
53     Some(())
54 }
55
56 async fn hello_world() {
57     let data = [0u8; 1];
58     let mut reader = &data[..];
59
60     let mut marker = [0u8; 1];
61     read_exact(&mut reader, &mut marker).await.unwrap();
62 }
63
64 fn run_fut<T>(fut: impl Future<Output = T>) -> T {
65     use std::sync::Arc;
66     use std::task::{Context, Poll, Wake, Waker};
67
68     struct MyWaker;
69     impl Wake for MyWaker {
70         fn wake(self: Arc<Self>) {
71             unimplemented!()
72         }
73     }
74
75     let waker = Waker::from(Arc::new(MyWaker));
76     let mut context = Context::from_waker(&waker);
77
78     let mut pinned = Box::pin(fut);
79     loop {
80         match pinned.as_mut().poll(&mut context) {
81             Poll::Pending => continue,
82             Poll::Ready(v) => return v,
83         }
84     }
85 }
86
87 fn main() {
88     let x = 5;
89     assert_eq!(run_fut(foo(&x, 7)), 31);
90     assert_eq!(run_fut(build_aggregate(1, 2, 3, 4)), 10);
91     assert_eq!(run_fut(includes_never(false, 4)), 16);
92     assert_eq!(run_fut(partial_init(4)), 8);
93     run_fut(hello_world());
94 }