From 5d7eda224e2c87b95a327f9279dbb25b89d24724 Mon Sep 17 00:00:00 2001 From: Joshua Wong Date: Sun, 19 May 2024 19:13:52 -0500 Subject: [PATCH 1/2] 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. --- compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 7802825a1b809..2f1b2ce9c4c7a 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 }; From 371de042d90f40fe5ec9a172971660da363ff08c Mon Sep 17 00:00:00 2001 From: Joshua Wong Date: Sun, 19 May 2024 19:23:38 -0500 Subject: [PATCH 2/2] add ui tests for E0373 suggestion --- .../ui/coroutine/static-move-suggestion.fixed | 19 ++++++++++++++ tests/ui/coroutine/static-move-suggestion.rs | 19 ++++++++++++++ .../coroutine/static-move-suggestion.stderr | 26 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/ui/coroutine/static-move-suggestion.fixed create mode 100644 tests/ui/coroutine/static-move-suggestion.rs create mode 100644 tests/ui/coroutine/static-move-suggestion.stderr diff --git a/tests/ui/coroutine/static-move-suggestion.fixed b/tests/ui/coroutine/static-move-suggestion.fixed new file mode 100644 index 0000000000000..56445be4715ff --- /dev/null +++ b/tests/ui/coroutine/static-move-suggestion.fixed @@ -0,0 +1,19 @@ +//@ run-rustfix + +// check to make sure that we suggest adding `move` after `static` + +#![feature(coroutines)] + +fn check() -> impl Sized { + let x = 0; + #[coroutine] + static move || { + //~^ ERROR E0373 + yield; + x + } +} + +fn main() { + check(); +} diff --git a/tests/ui/coroutine/static-move-suggestion.rs b/tests/ui/coroutine/static-move-suggestion.rs new file mode 100644 index 0000000000000..1d6e4a6288311 --- /dev/null +++ b/tests/ui/coroutine/static-move-suggestion.rs @@ -0,0 +1,19 @@ +//@ run-rustfix + +// check to make sure that we suggest adding `move` after `static` + +#![feature(coroutines)] + +fn check() -> impl Sized { + let x = 0; + #[coroutine] + static || { + //~^ ERROR E0373 + yield; + x + } +} + +fn main() { + check(); +} diff --git a/tests/ui/coroutine/static-move-suggestion.stderr b/tests/ui/coroutine/static-move-suggestion.stderr new file mode 100644 index 0000000000000..6d890468b322c --- /dev/null +++ b/tests/ui/coroutine/static-move-suggestion.stderr @@ -0,0 +1,26 @@ +error[E0373]: coroutine may outlive the current function, but it borrows `x`, which is owned by the current function + --> $DIR/static-move-suggestion.rs:10:5 + | +LL | static || { + | ^^^^^^^^^ may outlive borrowed value `x` +... +LL | x + | - `x` is borrowed here + | +note: coroutine is returned here + --> $DIR/static-move-suggestion.rs:10:5 + | +LL | / static || { +LL | | +LL | | yield; +LL | | x +LL | | } + | |_____^ +help: to force the coroutine to take ownership of `x` (and any other referenced variables), use the `move` keyword + | +LL | static move || { + | ++++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0373`.