Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/run-pass-fullmir/async-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![feature(
async_await,
await_macro,
futures_api,
pin,
)]

use std::{future::Future, pin::Pin, task::Poll};

// See if we can run a basic `async fn`
pub async fn foo(x: &u32, y: u32) -> u32 {
let y = &y;
let z = 9;
let z = &z;
let y = await!(async { *y + *z });
let a = 10;
let a = &a;
*x + y + *a
}

fn main() {
use std::{sync::Arc, task::{Wake, local_waker}};

struct NoWake;
impl Wake for NoWake {
fn wake(_arc_self: &Arc<Self>) {
panic!();
}
}

let lw = unsafe { local_waker(Arc::new(NoWake)) };
let x = 5;
let mut fut = foo(&x, 7);
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&lw), Poll::Ready(31));
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
use std::ops::{GeneratorState, Generator};

fn finish<T>(mut amt: usize, mut t: T) -> T::Return
where T: Generator<Yield = ()>
where T: Generator<Yield = usize>
{
loop {
match unsafe { t.resume() } {
GeneratorState::Yielded(()) => amt -= 1,
GeneratorState::Yielded(y) => amt -= y,
GeneratorState::Complete(ret) => {
assert_eq!(amt, 0);
return ret
Expand All @@ -28,38 +28,50 @@ fn finish<T>(mut amt: usize, mut t: T) -> T::Return
}

fn main() {
finish(1, || yield);
finish(1, || yield 1);
finish(3, || {
let mut x = 0;
yield;
yield 1;
x += 1;
yield;
yield 1;
x += 1;
yield;
yield 1;
assert_eq!(x, 2);
});
finish(8, || {
for _ in 0..8 {
yield;
finish(7*8/2, || {
for i in 0..8 {
yield i;
}
});
finish(1, || {
if true {
yield;
yield 1;
} else {
}
});
finish(1, || {
if false {
} else {
yield;
yield 1;
}
});
finish(2, || {
if { yield; false } {
yield;
if { yield 1; false } {
yield 1;
panic!()
}
yield
yield 1;
});
// also test a self-referential generator
assert_eq!(
finish(5, || {
let mut x = Box::new(5);
let y = &mut *x;
*y = 5;
yield *y;
*y = 10;
*x
}),
10
);
}