Skip to content

Commit ff55294

Browse files
committed
Auto merge of #1203 - RalfJung:generator, r=RalfJung
try even harder to catch invalid generator fields
2 parents 4f43709 + 58519a7 commit ff55294

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

tests/run-pass/generator.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::ops::{GeneratorState::{self, *}, Generator};
44
use std::pin::Pin;
55
use std::sync::atomic::{AtomicUsize, Ordering};
66
use std::fmt::Debug;
7+
use std::mem::ManuallyDrop;
8+
use std::ptr;
79

810
fn basic() {
911
fn finish<T>(mut amt: usize, mut t: T) -> T::Return
@@ -12,8 +14,13 @@ fn basic() {
1214
// We are not moving the `t` around until it gets dropped, so this is okay.
1315
let mut t = unsafe { Pin::new_unchecked(&mut t) };
1416
loop {
15-
match t.as_mut().resume(()) {
16-
GeneratorState::Yielded(y) => amt -= y,
17+
let state = t.as_mut().resume(());
18+
// Test if the generator is valid (according to type invariants).
19+
let _ = unsafe { ManuallyDrop::new(ptr::read(t.as_mut().get_unchecked_mut())) };
20+
match state {
21+
GeneratorState::Yielded(y) => {
22+
amt -= y;
23+
}
1724
GeneratorState::Complete(ret) => {
1825
assert_eq!(amt, 0);
1926
return ret
@@ -109,6 +116,8 @@ fn smoke_resume_arg() {
109116

110117
for (input, out) in inout {
111118
assert_eq!(gen.as_mut().resume(input), out);
119+
// Test if the generator is valid (according to type invariants).
120+
let _ = unsafe { ManuallyDrop::new(ptr::read(gen.as_mut().get_unchecked_mut())) };
112121
}
113122
}
114123

0 commit comments

Comments
 (0)