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