Error on using yield
without also using #[coroutine]
on the closure
And suggest adding the `#[coroutine]` to the closure
This commit is contained in:
parent
a589632dad
commit
aef0f4024a
279 changed files with 1290 additions and 886 deletions
|
@ -4,10 +4,10 @@ yield point.
|
|||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0626
|
||||
# #![feature(coroutines, coroutine_trait, pin)]
|
||||
# #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
|
||||
# use std::ops::Coroutine;
|
||||
# use std::pin::Pin;
|
||||
let mut b = || {
|
||||
let mut b = #[coroutine] || {
|
||||
let a = &String::new(); // <-- This borrow...
|
||||
yield (); // ...is still in scope here, when the yield occurs.
|
||||
println!("{}", a);
|
||||
|
@ -23,10 +23,10 @@ resolve the previous example by removing the borrow and just storing
|
|||
the integer by value:
|
||||
|
||||
```
|
||||
# #![feature(coroutines, coroutine_trait, pin)]
|
||||
# #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
|
||||
# use std::ops::Coroutine;
|
||||
# use std::pin::Pin;
|
||||
let mut b = || {
|
||||
let mut b = #[coroutine] || {
|
||||
let a = 3;
|
||||
yield ();
|
||||
println!("{}", a);
|
||||
|
@ -41,10 +41,10 @@ in those cases, something like the `Rc` or `Arc` types may be useful.
|
|||
This error also frequently arises with iteration:
|
||||
|
||||
```compile_fail,E0626
|
||||
# #![feature(coroutines, coroutine_trait, pin)]
|
||||
# #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
|
||||
# use std::ops::Coroutine;
|
||||
# use std::pin::Pin;
|
||||
let mut b = || {
|
||||
let mut b = #[coroutine] || {
|
||||
let v = vec![1,2,3];
|
||||
for &x in &v { // <-- borrow of `v` is still in scope...
|
||||
yield x; // ...when this yield occurs.
|
||||
|
@ -57,10 +57,10 @@ Such cases can sometimes be resolved by iterating "by value" (or using
|
|||
`into_iter()`) to avoid borrowing:
|
||||
|
||||
```
|
||||
# #![feature(coroutines, coroutine_trait, pin)]
|
||||
# #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
|
||||
# use std::ops::Coroutine;
|
||||
# use std::pin::Pin;
|
||||
let mut b = || {
|
||||
let mut b = #[coroutine] || {
|
||||
let v = vec![1,2,3];
|
||||
for x in v { // <-- Take ownership of the values instead!
|
||||
yield x; // <-- Now yield is OK.
|
||||
|
@ -72,10 +72,10 @@ Pin::new(&mut b).resume(());
|
|||
If taking ownership is not an option, using indices can work too:
|
||||
|
||||
```
|
||||
# #![feature(coroutines, coroutine_trait, pin)]
|
||||
# #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
|
||||
# use std::ops::Coroutine;
|
||||
# use std::pin::Pin;
|
||||
let mut b = || {
|
||||
let mut b = #[coroutine] || {
|
||||
let v = vec![1,2,3];
|
||||
let len = v.len(); // (*)
|
||||
for i in 0..len {
|
||||
|
|
|
@ -3,7 +3,7 @@ A yield expression was used outside of the coroutine literal.
|
|||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0627
|
||||
#![feature(coroutines, coroutine_trait)]
|
||||
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
|
||||
|
||||
fn fake_coroutine() -> &'static str {
|
||||
yield 1;
|
||||
|
@ -19,10 +19,10 @@ The error occurs because keyword `yield` can only be used inside the coroutine
|
|||
literal. This can be fixed by constructing the coroutine correctly.
|
||||
|
||||
```
|
||||
#![feature(coroutines, coroutine_trait)]
|
||||
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
|
||||
|
||||
fn main() {
|
||||
let mut coroutine = || {
|
||||
let mut coroutine = #[coroutine] || {
|
||||
yield 1;
|
||||
return "foo"
|
||||
};
|
||||
|
|
|
@ -3,10 +3,10 @@ More than one parameter was used for a coroutine.
|
|||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0628
|
||||
#![feature(coroutines, coroutine_trait)]
|
||||
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
|
||||
|
||||
fn main() {
|
||||
let coroutine = |a: i32, b: i32| {
|
||||
let coroutine = #[coroutine] |a: i32, b: i32| {
|
||||
// error: too many parameters for a coroutine
|
||||
// Allowed only 0 or 1 parameter
|
||||
yield a;
|
||||
|
@ -20,10 +20,10 @@ at most 1 parameter for the coroutine. For example, we might resolve
|
|||
the previous example by passing only one parameter.
|
||||
|
||||
```
|
||||
#![feature(coroutines, coroutine_trait)]
|
||||
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
|
||||
|
||||
fn main() {
|
||||
let coroutine = |a: i32| {
|
||||
let coroutine = #[coroutine] |a: i32| {
|
||||
yield a;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -3,10 +3,10 @@ A `yield` clause was used in an `async` context.
|
|||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0727,edition2018
|
||||
#![feature(coroutines)]
|
||||
#![feature(coroutines, stmt_expr_attributes)]
|
||||
|
||||
fn main() {
|
||||
let coroutine = || {
|
||||
let coroutine = #[coroutine] || {
|
||||
async {
|
||||
yield;
|
||||
}
|
||||
|
@ -20,10 +20,10 @@ which is not yet supported.
|
|||
To fix this error, you have to move `yield` out of the `async` block:
|
||||
|
||||
```edition2018
|
||||
#![feature(coroutines)]
|
||||
#![feature(coroutines, stmt_expr_attributes)]
|
||||
|
||||
fn main() {
|
||||
let coroutine = || {
|
||||
let coroutine = #[coroutine] || {
|
||||
yield;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue