about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJoshua Wong <joshuawong@anticentri.st>2024-05-19 19:13:52 -0500
committerJoshua Wong <joshuawong@anticentri.st>2024-05-19 19:13:52 -0500
commit5d7eda224e2c87b95a327f9279dbb25b89d24724 (patch)
treeb381ddb238f46fbe2c1d4aa52222adb59b75ce1b
parentd84b9037541f45dc2c52a41d723265af211c0497 (diff)
downloadrust-5d7eda224e2c87b95a327f9279dbb25b89d24724.tar.gz
rust-5d7eda224e2c87b95a327f9279dbb25b89d24724.zip
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.
-rw-r--r--compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs4
1 files changed, 4 insertions, 0 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
index 7802825a1b8..2f1b2ce9c4c 100644
--- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
+++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
@@ -3343,6 +3343,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
                 } else if string.starts_with("gen") {
                     // `gen` is 3 chars long
                     Some(3)
+                } else if string.starts_with("static") {
+                    // `static` is 6 chars long
+                    // This is used for `!Unpin` coroutines
+                    Some(6)
                 } else {
                     None
                 };