about summary refs log tree commit diff
path: root/tests/ui/async-await/async-drop/live-dead-storage4.rs
blob: d927cb96674fc3ec477f7fc241d322adc4027c79 (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
// ex-ice: #141409
//@ compile-flags: -Zmir-enable-passes=+Inline -Zvalidate-mir -Zlint-mir --crate-type lib
//@ edition:2024
//@ check-pass

#![feature(async_drop)]
#![allow(incomplete_features)]
#![allow(non_snake_case)]

use std::mem::ManuallyDrop;
use std::{
    future::{async_drop_in_place, Future},
    pin::{pin, Pin},
    sync::{mpsc, Arc},
    task::{Context, Poll, Wake, Waker},
};
fn main() {
    block_on(bar(0))
}
async fn baz(ident_base: usize) {}
async fn bar(ident_base: usize) {
    baz(1).await
}
fn block_on<F>(fut_unpin: F) -> F::Output
where
    F: Future,
{
    let fut_pin = pin!(ManuallyDrop::new(fut_unpin));
    let mut fut = unsafe { Pin::map_unchecked_mut(fut_pin, |x| &mut **x) };
    let (waker, rx) = simple_waker();
    let mut context = Context::from_waker(&waker);
    let rv = loop {
        match fut.as_mut().poll(&mut context) {
            Poll::Ready(out) => break out,
            PollPending => (),
        }
    };
    let drop_fut_unpin = unsafe { async_drop_in_place(fut.get_unchecked_mut()) };
    let drop_fut = pin!(drop_fut_unpin);
    loop {
        match drop_fut.poll(&mut context) {
            Poll => break,
        }
    }
    rv
}
fn simple_waker() -> (Waker, mpsc::Receiver<()>) {
    struct SimpleWaker {
        tx: mpsc::Sender<()>,
    }
    impl Wake for SimpleWaker {
        fn wake(self: Arc<Self>) {}
    }
    let (tx, rx) = mpsc::channel();
    (Waker::from(Arc::new(SimpleWaker { tx })), rx)
}