Skip to content

Commit 6f9ee8b

Browse files
authored
Merge pull request #536 from RalfJung/self-referential-generator
test self-referential generator
2 parents e6948fa + 1edba23 commit 6f9ee8b

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

tests/run-pass-fullmir/async-fn.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![feature(
2+
async_await,
3+
await_macro,
4+
futures_api,
5+
pin,
6+
)]
7+
8+
use std::{future::Future, pin::Pin, task::Poll};
9+
10+
// See if we can run a basic `async fn`
11+
pub async fn foo(x: &u32, y: u32) -> u32 {
12+
let y = &y;
13+
let z = 9;
14+
let z = &z;
15+
let y = await!(async { *y + *z });
16+
let a = 10;
17+
let a = &a;
18+
*x + y + *a
19+
}
20+
21+
fn main() {
22+
use std::{sync::Arc, task::{Wake, local_waker}};
23+
24+
struct NoWake;
25+
impl Wake for NoWake {
26+
fn wake(_arc_self: &Arc<Self>) {
27+
panic!();
28+
}
29+
}
30+
31+
let lw = unsafe { local_waker(Arc::new(NoWake)) };
32+
let x = 5;
33+
let mut fut = foo(&x, 7);
34+
assert_eq!(unsafe { Pin::new_unchecked(&mut fut) }.poll(&lw), Poll::Ready(31));
35+
}

tests/run-pass/generator_control_flow.rs renamed to tests/run-pass/generator.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
use std::ops::{GeneratorState, Generator};
1414

1515
fn finish<T>(mut amt: usize, mut t: T) -> T::Return
16-
where T: Generator<Yield = ()>
16+
where T: Generator<Yield = usize>
1717
{
1818
loop {
1919
match unsafe { t.resume() } {
20-
GeneratorState::Yielded(()) => amt -= 1,
20+
GeneratorState::Yielded(y) => amt -= y,
2121
GeneratorState::Complete(ret) => {
2222
assert_eq!(amt, 0);
2323
return ret
@@ -28,38 +28,50 @@ fn finish<T>(mut amt: usize, mut t: T) -> T::Return
2828
}
2929

3030
fn main() {
31-
finish(1, || yield);
31+
finish(1, || yield 1);
3232
finish(3, || {
3333
let mut x = 0;
34-
yield;
34+
yield 1;
3535
x += 1;
36-
yield;
36+
yield 1;
3737
x += 1;
38-
yield;
38+
yield 1;
3939
assert_eq!(x, 2);
4040
});
41-
finish(8, || {
42-
for _ in 0..8 {
43-
yield;
41+
finish(7*8/2, || {
42+
for i in 0..8 {
43+
yield i;
4444
}
4545
});
4646
finish(1, || {
4747
if true {
48-
yield;
48+
yield 1;
4949
} else {
5050
}
5151
});
5252
finish(1, || {
5353
if false {
5454
} else {
55-
yield;
55+
yield 1;
5656
}
5757
});
5858
finish(2, || {
59-
if { yield; false } {
60-
yield;
59+
if { yield 1; false } {
60+
yield 1;
6161
panic!()
6262
}
63-
yield
63+
yield 1;
6464
});
65+
// also test a self-referential generator
66+
assert_eq!(
67+
finish(5, || {
68+
let mut x = Box::new(5);
69+
let y = &mut *x;
70+
*y = 5;
71+
yield *y;
72+
*y = 10;
73+
*x
74+
}),
75+
10
76+
);
6577
}

0 commit comments

Comments
 (0)