Skip to content

Commit c111936

Browse files
committed
Auto merge of #116958 - oli-obk:coro, r=pnkfelix
rename Generator to Coroutine implements rust-lang/compiler-team#682 While I did an automated replacement, I went through all changes manually to avoid renaming things like "id generators", "code generator", ... I renamed files where that was necessary due to the contents referring to the crate name itself (mir opt, codegen or debuginfo tests), or required by tidy (feature gate docs) * [x] rename various remaining abbreviated references to generators. * [x] rename files * [x] rename folders * [x] add renamed feature: `generators`, ... r? `@ghost`
2 parents 616946c + f4f4d03 commit c111936

10 files changed

+99
-99
lines changed

src/helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
492492
// `Variants::Multiple`.
493493
match v.layout.variants {
494494
Variants::Multiple { .. } => {
495-
// A multi-variant enum, or generator, or so.
495+
// A multi-variant enum, or coroutine, or so.
496496
// Treat this like a union: without reading from memory,
497497
// we cannot determine the variant we are in. Reading from
498498
// memory would be subject to Stacked Borrows rules, leading

tests/fail/coroutine-pinned-moved.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
2+
#![feature(coroutines, coroutine_trait)]
3+
4+
use std::{
5+
ops::{Coroutine, CoroutineState},
6+
pin::Pin,
7+
};
8+
9+
fn firstn() -> impl Coroutine<Yield = u64, Return = ()> {
10+
static move || {
11+
let mut num = 0;
12+
let num = &mut num;
13+
*num += 0;
14+
15+
yield *num;
16+
*num += 1; //~ERROR: has been freed
17+
}
18+
}
19+
20+
struct CoroutineIteratorAdapter<G>(G);
21+
22+
impl<G> Iterator for CoroutineIteratorAdapter<G>
23+
where
24+
G: Coroutine<Return = ()>,
25+
{
26+
type Item = G::Yield;
27+
28+
fn next(&mut self) -> Option<Self::Item> {
29+
let me = unsafe { Pin::new_unchecked(&mut self.0) };
30+
match me.resume(()) {
31+
CoroutineState::Yielded(x) => Some(x),
32+
CoroutineState::Complete(_) => None,
33+
}
34+
}
35+
}
36+
37+
fn main() {
38+
let mut coroutine_iterator_2 = {
39+
let mut coroutine_iterator = Box::new(CoroutineIteratorAdapter(firstn()));
40+
coroutine_iterator.next(); // pin it
41+
42+
Box::new(*coroutine_iterator) // move it
43+
}; // *deallocate* coroutine_iterator
44+
45+
coroutine_iterator_2.next(); // and use moved value
46+
}

tests/fail/generator-pinned-moved.stderr renamed to tests/fail/coroutine-pinned-moved.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
error: Undefined Behavior: memory access failed: ALLOC has been freed, so this pointer is dangling
2-
--> $DIR/generator-pinned-moved.rs:LL:CC
2+
--> $DIR/coroutine-pinned-moved.rs:LL:CC
33
|
44
LL | *num += 1;
55
| ^^^^^^^^^ memory access failed: ALLOC has been freed, so this pointer is dangling
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
help: ALLOC was allocated here:
10-
--> $DIR/generator-pinned-moved.rs:LL:CC
10+
--> $DIR/coroutine-pinned-moved.rs:LL:CC
1111
|
12-
LL | let mut generator_iterator = Box::new(GeneratorIteratorAdapter(firstn()));
12+
LL | let mut coroutine_iterator = Box::new(CoroutineIteratorAdapter(firstn()));
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414
help: ALLOC was deallocated here:
15-
--> $DIR/generator-pinned-moved.rs:LL:CC
15+
--> $DIR/coroutine-pinned-moved.rs:LL:CC
1616
|
17-
LL | }; // *deallocate* generator_iterator
17+
LL | }; // *deallocate* coroutine_iterator
1818
| ^
1919
= note: BACKTRACE (of the first span):
20-
= note: inside closure at $DIR/generator-pinned-moved.rs:LL:CC
21-
note: inside `<GeneratorIteratorAdapter<{static generator@$DIR/generator-pinned-moved.rs:LL:CC}> as std::iter::Iterator>::next`
22-
--> $DIR/generator-pinned-moved.rs:LL:CC
20+
= note: inside closure at $DIR/coroutine-pinned-moved.rs:LL:CC
21+
note: inside `<CoroutineIteratorAdapter<{static coroutine@$DIR/coroutine-pinned-moved.rs:LL:CC}> as std::iter::Iterator>::next`
22+
--> $DIR/coroutine-pinned-moved.rs:LL:CC
2323
|
2424
LL | match me.resume(()) {
2525
| ^^^^^^^^^^^^^
26-
= note: inside `<std::boxed::Box<GeneratorIteratorAdapter<{static generator@$DIR/generator-pinned-moved.rs:LL:CC}>> as std::iter::Iterator>::next` at RUSTLIB/alloc/src/boxed.rs:LL:CC
26+
= note: inside `<std::boxed::Box<CoroutineIteratorAdapter<{static coroutine@$DIR/coroutine-pinned-moved.rs:LL:CC}>> as std::iter::Iterator>::next` at RUSTLIB/alloc/src/boxed.rs:LL:CC
2727
note: inside `main`
28-
--> $DIR/generator-pinned-moved.rs:LL:CC
28+
--> $DIR/coroutine-pinned-moved.rs:LL:CC
2929
|
30-
LL | generator_iterator_2.next(); // and use moved value
30+
LL | coroutine_iterator_2.next(); // and use moved value
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

3333
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

tests/fail/generator-pinned-moved.rs

-46
This file was deleted.

tests/pass/generator.rs renamed to tests/pass/coroutine.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//@revisions: stack tree
22
//@[tree]compile-flags: -Zmiri-tree-borrows
3-
#![feature(generators, generator_trait, never_type)]
3+
#![feature(coroutines, coroutine_trait, never_type)]
44

55
use std::fmt::Debug;
66
use std::mem::ManuallyDrop;
77
use std::ops::{
8-
Generator,
9-
GeneratorState::{self, *},
8+
Coroutine,
9+
CoroutineState::{self, *},
1010
};
1111
use std::pin::Pin;
1212
use std::ptr;
@@ -15,22 +15,22 @@ use std::sync::atomic::{AtomicUsize, Ordering};
1515
fn basic() {
1616
fn finish<T>(mut amt: usize, self_referential: bool, mut t: T) -> T::Return
1717
where
18-
T: Generator<Yield = usize>,
18+
T: Coroutine<Yield = usize>,
1919
{
2020
// We are not moving the `t` around until it gets dropped, so this is okay.
2121
let mut t = unsafe { Pin::new_unchecked(&mut t) };
2222
loop {
2323
let state = t.as_mut().resume(());
24-
// Test if the generator is valid (according to type invariants).
25-
// For self-referential generators however this is UB!
24+
// Test if the coroutine is valid (according to type invariants).
25+
// For self-referential coroutines however this is UB!
2626
if !self_referential {
2727
let _ = unsafe { ManuallyDrop::new(ptr::read(t.as_mut().get_unchecked_mut())) };
2828
}
2929
match state {
30-
GeneratorState::Yielded(y) => {
30+
CoroutineState::Yielded(y) => {
3131
amt -= y;
3232
}
33-
GeneratorState::Complete(ret) => {
33+
CoroutineState::Complete(ret) => {
3434
assert_eq!(amt, 0);
3535
return ret;
3636
}
@@ -86,7 +86,7 @@ fn basic() {
8686
yield 1;
8787
});
8888

89-
// also test self-referential generators
89+
// also test self-referential coroutines
9090
assert_eq!(
9191
finish(5, true, static || {
9292
let mut x = 5;
@@ -134,9 +134,9 @@ fn basic() {
134134
}
135135

136136
fn smoke_resume_arg() {
137-
fn drain<G: Generator<R, Yield = Y> + Unpin, R, Y>(
137+
fn drain<G: Coroutine<R, Yield = Y> + Unpin, R, Y>(
138138
gen: &mut G,
139-
inout: Vec<(R, GeneratorState<Y, G::Return>)>,
139+
inout: Vec<(R, CoroutineState<Y, G::Return>)>,
140140
) where
141141
Y: Debug + PartialEq,
142142
G::Return: Debug + PartialEq,
@@ -145,7 +145,7 @@ fn smoke_resume_arg() {
145145

146146
for (input, out) in inout {
147147
assert_eq!(gen.as_mut().resume(input), out);
148-
// Test if the generator is valid (according to type invariants).
148+
// Test if the coroutine is valid (according to type invariants).
149149
let _ = unsafe { ManuallyDrop::new(ptr::read(gen.as_mut().get_unchecked_mut())) };
150150
}
151151
}

tests/pass/move-data-across-await-point.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async fn data_moved_async() {
1515
// `raw_pointer` points to the original location where the Vec was stored in the caller.
1616
// `data` is where that Vec (to be precise, its ptr+capacity+len on-stack data)
1717
// got moved to. Those will usually not be the same since the Vec got moved twice
18-
// (into the function call, and then into the generator upvar).
18+
// (into the function call, and then into the coroutine upvar).
1919
assert_ne!(raw_pointer, raw_pointer2);
2020
unsafe {
2121
// This writes into the `x` in `data_moved_async`, re-initializing it.

tests/pass/stacked-borrows/generators-self-referential.rs renamed to tests/pass/stacked-borrows/coroutine-self-referential.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// See https://github.com/rust-lang/unsafe-code-guidelines/issues/148:
22
// this fails when Stacked Borrows is strictly applied even to `!Unpin` types.
3-
#![feature(generators, generator_trait)]
3+
#![feature(coroutines, coroutine_trait)]
44

55
use std::{
6-
ops::{Generator, GeneratorState},
6+
ops::{Coroutine, CoroutineState},
77
pin::Pin,
88
};
99

10-
fn firstn() -> impl Generator<Yield = u64, Return = ()> {
10+
fn firstn() -> impl Coroutine<Yield = u64, Return = ()> {
1111
static move || {
1212
let mut num = 0;
1313
let num = &mut num;
@@ -24,10 +24,10 @@ fn firstn() -> impl Generator<Yield = u64, Return = ()> {
2424
}
2525

2626
fn main() {
27-
let mut generator_iterator = firstn();
28-
let mut pin = unsafe { Pin::new_unchecked(&mut generator_iterator) };
27+
let mut coroutine_iterator = firstn();
28+
let mut pin = unsafe { Pin::new_unchecked(&mut coroutine_iterator) };
2929
let mut sum = 0;
30-
while let GeneratorState::Yielded(x) = pin.as_mut().resume(()) {
30+
while let CoroutineState::Yielded(x) = pin.as_mut().resume(()) {
3131
sum += x;
3232
}
3333
assert_eq!(sum, 3);

tests/pass/stacked-borrows/stacked-borrows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ fn wide_raw_ptr_in_tuple() {
223223
fn not_unpin_not_protected() {
224224
// `&mut !Unpin`, at least for now, does not get `noalias` nor `dereferenceable`, so we also
225225
// don't add protectors. (We could, but until we have a better idea for where we want to go with
226-
// the self-referential-generator situation, it does not seem worth the potential trouble.)
226+
// the self-referential-coroutine situation, it does not seem worth the potential trouble.)
227227
use std::marker::PhantomPinned;
228228

229229
pub struct NotUnpin(i32, PhantomPinned);

tests/pass/track-caller-attribute.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#![feature(core_intrinsics)]
22
#![feature(stmt_expr_attributes)]
33
#![feature(closure_track_caller)]
4-
#![feature(generator_trait)]
5-
#![feature(generators)]
4+
#![feature(coroutine_trait)]
5+
#![feature(coroutines)]
66

7-
use std::ops::{Generator, GeneratorState};
7+
use std::ops::{Coroutine, CoroutineState};
88
use std::panic::Location;
99
use std::pin::Pin;
1010

@@ -210,55 +210,55 @@ fn test_closure() {
210210
assert_eq!(non_tracked_loc.column(), 33);
211211
}
212212

213-
fn test_generator() {
213+
fn test_coroutine() {
214214
#[track_caller]
215-
fn mono_generator<F: Generator<String, Yield = (&'static str, String, Loc), Return = ()>>(
215+
fn mono_coroutine<F: Coroutine<String, Yield = (&'static str, String, Loc), Return = ()>>(
216216
val: Pin<&mut F>,
217217
) -> (&'static str, String, Loc) {
218218
match val.resume("Mono".to_string()) {
219-
GeneratorState::Yielded(val) => val,
219+
CoroutineState::Yielded(val) => val,
220220
_ => unreachable!(),
221221
}
222222
}
223223

224224
#[track_caller]
225-
fn dyn_generator(
226-
val: Pin<&mut dyn Generator<String, Yield = (&'static str, String, Loc), Return = ()>>,
225+
fn dyn_coroutine(
226+
val: Pin<&mut dyn Coroutine<String, Yield = (&'static str, String, Loc), Return = ()>>,
227227
) -> (&'static str, String, Loc) {
228228
match val.resume("Dyn".to_string()) {
229-
GeneratorState::Yielded(val) => val,
229+
CoroutineState::Yielded(val) => val,
230230
_ => unreachable!(),
231231
}
232232
}
233233

234234
#[rustfmt::skip]
235-
let generator = #[track_caller] |arg: String| {
235+
let coroutine = #[track_caller] |arg: String| {
236236
yield ("first", arg.clone(), Location::caller());
237237
yield ("second", arg.clone(), Location::caller());
238238
};
239239

240-
let mut pinned = Box::pin(generator);
241-
let (dyn_ret, dyn_arg, dyn_loc) = dyn_generator(pinned.as_mut());
240+
let mut pinned = Box::pin(coroutine);
241+
let (dyn_ret, dyn_arg, dyn_loc) = dyn_coroutine(pinned.as_mut());
242242
assert_eq!(dyn_ret, "first");
243243
assert_eq!(dyn_arg, "Dyn".to_string());
244-
// The `Generator` trait does not have `#[track_caller]` on `resume`, so
244+
// The `Coroutine` trait does not have `#[track_caller]` on `resume`, so
245245
// this will not match.
246246
assert_ne!(dyn_loc.file(), file!());
247247

248-
let (mono_ret, mono_arg, mono_loc) = mono_generator(pinned.as_mut());
248+
let (mono_ret, mono_arg, mono_loc) = mono_coroutine(pinned.as_mut());
249249
let mono_line = line!() - 1;
250250
assert_eq!(mono_ret, "second");
251-
// The generator ignores the argument to the second `resume` call
251+
// The coroutine ignores the argument to the second `resume` call
252252
assert_eq!(mono_arg, "Dyn".to_string());
253253
assert_eq!(mono_loc.file(), file!());
254254
assert_eq!(mono_loc.line(), mono_line);
255255
assert_eq!(mono_loc.column(), 42);
256256

257257
#[rustfmt::skip]
258-
let non_tracked_generator = || { yield Location::caller(); };
259-
let non_tracked_line = line!() - 1; // This is the line of the generator, not its caller
260-
let non_tracked_loc = match Box::pin(non_tracked_generator).as_mut().resume(()) {
261-
GeneratorState::Yielded(val) => val,
258+
let non_tracked_coroutine = || { yield Location::caller(); };
259+
let non_tracked_line = line!() - 1; // This is the line of the coroutine, not its caller
260+
let non_tracked_loc = match Box::pin(non_tracked_coroutine).as_mut().resume(()) {
261+
CoroutineState::Yielded(val) => val,
262262
_ => unreachable!(),
263263
};
264264
assert_eq!(non_tracked_loc.file(), file!());
@@ -272,5 +272,5 @@ fn main() {
272272
test_trait_obj();
273273
test_trait_obj2();
274274
test_closure();
275-
test_generator();
275+
test_coroutine();
276276
}

tests/pass/tree_borrows/tree-borrows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ fn wide_raw_ptr_in_tuple() {
315315
fn not_unpin_not_protected() {
316316
// `&mut !Unpin`, at least for now, does not get `noalias` nor `dereferenceable`, so we also
317317
// don't add protectors. (We could, but until we have a better idea for where we want to go with
318-
// the self-referential-generator situation, it does not seem worth the potential trouble.)
318+
// the self-referential-coroutine situation, it does not seem worth the potential trouble.)
319319
use std::marker::PhantomPinned;
320320

321321
pub struct NotUnpin(i32, PhantomPinned);

0 commit comments

Comments
 (0)