Skip to content

Commit 199d3bf

Browse files
authored
Rollup merge of rust-lang#125301 - jwong101:fix-static-coro-suggest, r=compiler-errors
fix suggestion in E0373 for !Unpin coroutines Coroutines can be prefixed with the `static` keyword to make them `!Unpin`. However, given the following function: ```rust fn check() -> impl Sized { let x = 0; #[coroutine] static || { yield; x } } ``` We currently suggest prefixing `move` before `static`, which is syntactically incorrect: ``` error[E0373]: coroutine may outlive the current function, but it borrows ... --> src/main.rs:6:5 | 6 | static || { | ^^^^^^^^^ may outlive borrowed value `x` 7 | yield; 8 | x | - `x` is borrowed here | note: coroutine is returned here --> src/main.rs:6:5 | 6 | / static || { 7 | | yield; 8 | | x 9 | | } | |_____^ help: to force the coroutine to take ownership of `x` (and any other referenced variables), use the `move` keyword | // this is syntactically incorrect, it should be `static move ||` 6 | move static || { | ++++ ``` This PR suggests adding `move` after `static` for these coroutines. I also added a UI test for this case.
2 parents 8855261 + 371de04 commit 199d3bf

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3343,6 +3343,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
33433343
} else if string.starts_with("gen") {
33443344
// `gen` is 3 chars long
33453345
Some(3)
3346+
} else if string.starts_with("static") {
3347+
// `static` is 6 chars long
3348+
// This is used for `!Unpin` coroutines
3349+
Some(6)
33463350
} else {
33473351
None
33483352
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ run-rustfix
2+
3+
// check to make sure that we suggest adding `move` after `static`
4+
5+
#![feature(coroutines)]
6+
7+
fn check() -> impl Sized {
8+
let x = 0;
9+
#[coroutine]
10+
static move || {
11+
//~^ ERROR E0373
12+
yield;
13+
x
14+
}
15+
}
16+
17+
fn main() {
18+
check();
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ run-rustfix
2+
3+
// check to make sure that we suggest adding `move` after `static`
4+
5+
#![feature(coroutines)]
6+
7+
fn check() -> impl Sized {
8+
let x = 0;
9+
#[coroutine]
10+
static || {
11+
//~^ ERROR E0373
12+
yield;
13+
x
14+
}
15+
}
16+
17+
fn main() {
18+
check();
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0373]: coroutine may outlive the current function, but it borrows `x`, which is owned by the current function
2+
--> $DIR/static-move-suggestion.rs:10:5
3+
|
4+
LL | static || {
5+
| ^^^^^^^^^ may outlive borrowed value `x`
6+
...
7+
LL | x
8+
| - `x` is borrowed here
9+
|
10+
note: coroutine is returned here
11+
--> $DIR/static-move-suggestion.rs:10:5
12+
|
13+
LL | / static || {
14+
LL | |
15+
LL | | yield;
16+
LL | | x
17+
LL | | }
18+
| |_____^
19+
help: to force the coroutine to take ownership of `x` (and any other referenced variables), use the `move` keyword
20+
|
21+
LL | static move || {
22+
| ++++
23+
24+
error: aborting due to 1 previous error
25+
26+
For more information about this error, try `rustc --explain E0373`.

0 commit comments

Comments
 (0)