Skip to content

Commit 6dfc9f8

Browse files
Explain that coroutine can be marked static
And also point out the def span of the coroutine
1 parent 023b583 commit 6dfc9f8

9 files changed

+91
-3
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#![allow(rustc::diagnostic_outside_of_impl)]
22
#![allow(rustc::untranslatable_diagnostic)]
33

4+
use rustc_errors::Applicability;
45
use rustc_errors::{codes::*, struct_span_code_err, Diag, DiagCtxtHandle};
6+
use rustc_hir as hir;
57
use rustc_middle::span_bug;
68
use rustc_middle::ty::{self, Ty, TyCtxt};
79
use rustc_span::Span;
@@ -382,13 +384,35 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
382384
yield_span: Span,
383385
) -> Diag<'infcx> {
384386
let coroutine_kind = self.body.coroutine.as_ref().unwrap().coroutine_kind;
385-
struct_span_code_err!(
387+
let mut diag = struct_span_code_err!(
386388
self.dcx(),
387389
span,
388390
E0626,
389391
"borrow may still be in use when {coroutine_kind:#} yields",
390-
)
391-
.with_span_label(yield_span, "possible yield occurs here")
392+
);
393+
diag.span_label(
394+
self.infcx.tcx.def_span(self.body.source.def_id()),
395+
format!("within this {coroutine_kind:#}"),
396+
);
397+
diag.span_label(yield_span, "possible yield occurs here");
398+
if matches!(coroutine_kind, hir::CoroutineKind::Coroutine(_)) {
399+
let hir::Closure { capture_clause, fn_decl_span, .. } = self
400+
.infcx
401+
.tcx
402+
.hir_node_by_def_id(self.body.source.def_id().expect_local())
403+
.expect_closure();
404+
let span = match capture_clause {
405+
rustc_hir::CaptureBy::Value { move_kw } => move_kw.shrink_to_lo(),
406+
rustc_hir::CaptureBy::Ref => fn_decl_span.shrink_to_lo(),
407+
};
408+
diag.span_suggestion_verbose(
409+
span,
410+
"add `static` to mark this coroutine as unmovable",
411+
"static ",
412+
Applicability::MaybeIncorrect,
413+
);
414+
}
415+
diag
392416
}
393417

394418
pub(crate) fn cannot_borrow_across_destructor(&self, borrow_span: Span) -> Diag<'infcx> {

tests/ui/coroutine/coroutine-with-nll.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
error[E0626]: borrow may still be in use when coroutine yields
22
--> $DIR/coroutine-with-nll.rs:8:17
33
|
4+
LL | || {
5+
| -- within this coroutine
6+
...
47
LL | let b = &mut true;
58
| ^^^^^^^^^
69
LL |
710
LL | yield ();
811
| -------- possible yield occurs here
12+
|
13+
help: add `static` to mark this coroutine as unmovable
14+
|
15+
LL | static || {
16+
| ++++++
917

1018
error: aborting due to 1 previous error
1119

tests/ui/coroutine/issue-48048.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
error[E0626]: borrow may still be in use when coroutine yields
22
--> $DIR/issue-48048.rs:9:9
33
|
4+
LL | #[coroutine] || {
5+
| -- within this coroutine
6+
...
47
LL | x.0({
58
| ^^^
69
LL | yield;
710
| ----- possible yield occurs here
11+
|
12+
help: add `static` to mark this coroutine as unmovable
13+
|
14+
LL | #[coroutine] static || {
15+
| ++++++
816

917
error: aborting due to 1 previous error
1018

tests/ui/coroutine/pattern-borrow.stderr

+7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
error[E0626]: borrow may still be in use when coroutine yields
22
--> $DIR/pattern-borrow.rs:9:24
33
|
4+
LL | #[coroutine] move || {
5+
| ------- within this coroutine
46
LL | if let Test::A(ref _a) = test {
57
| ^^^^^^
68
LL | yield ();
79
| -------- possible yield occurs here
10+
|
11+
help: add `static` to mark this coroutine as unmovable
12+
|
13+
LL | #[coroutine] static move || {
14+
| ++++++
815

916
error: aborting due to 1 previous error
1017

tests/ui/coroutine/self_referential_gen_block.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0626]: borrow may still be in use when `gen` block yields
22
--> $DIR/self_referential_gen_block.rs:9:21
33
|
4+
LL | let mut x = gen {
5+
| --- within this `gen` block
6+
LL | let y = 42;
47
LL | let z = &y;
58
| ^^
69
LL | yield 43;

tests/ui/coroutine/yield-in-args.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
error[E0626]: borrow may still be in use when coroutine yields
22
--> $DIR/yield-in-args.rs:9:13
33
|
4+
LL | || {
5+
| -- within this coroutine
6+
LL | let b = true;
47
LL | foo(&b, yield);
58
| ^^ ----- possible yield occurs here
9+
|
10+
help: add `static` to mark this coroutine as unmovable
11+
|
12+
LL | static || {
13+
| ++++++
614

715
error: aborting due to 1 previous error
816

tests/ui/coroutine/yield-while-iterating.stderr

+7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
error[E0626]: borrow may still be in use when coroutine yields
22
--> $DIR/yield-while-iterating.rs:13:18
33
|
4+
LL | let _b =#[coroutine] move || {
5+
| ------- within this coroutine
46
LL | for p in &x {
57
| ^^
68
LL | yield();
79
| ------- possible yield occurs here
10+
|
11+
help: add `static` to mark this coroutine as unmovable
12+
|
13+
LL | let _b =#[coroutine] static move || {
14+
| ++++++
815

916
error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
1017
--> $DIR/yield-while-iterating.rs:58:20

tests/ui/coroutine/yield-while-local-borrowed.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
error[E0626]: borrow may still be in use when coroutine yields
22
--> $DIR/yield-while-local-borrowed.rs:13:17
33
|
4+
LL | let mut b = #[coroutine] move || {
5+
| ------- within this coroutine
46
LL | let a = &mut 3;
57
| ^^^^^^
68
LL |
79
LL | yield ();
810
| -------- possible yield occurs here
11+
|
12+
help: add `static` to mark this coroutine as unmovable
13+
|
14+
LL | let mut b = #[coroutine] static move || {
15+
| ++++++
916

1017
error[E0626]: borrow may still be in use when coroutine yields
1118
--> $DIR/yield-while-local-borrowed.rs:40:21
1219
|
20+
LL | let mut b = #[coroutine] move || {
21+
| ------- within this coroutine
22+
...
1323
LL | let b = &a;
1424
| ^^
1525
LL |
1626
LL | yield ();
1727
| -------- possible yield occurs here
28+
|
29+
help: add `static` to mark this coroutine as unmovable
30+
|
31+
LL | let mut b = #[coroutine] static move || {
32+
| ++++++
1833

1934
error: aborting due to 2 previous errors
2035

tests/ui/nll/issue-55850.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ LL | yield &s[..]
1010
error[E0626]: borrow may still be in use when coroutine yields
1111
--> $DIR/issue-55850.rs:28:16
1212
|
13+
LL | GenIter(#[coroutine] move || {
14+
| ------- within this coroutine
15+
LL | let mut s = String::new();
1316
LL | yield &s[..]
1417
| -------^---- possible yield occurs here
18+
|
19+
help: add `static` to mark this coroutine as unmovable
20+
|
21+
LL | GenIter(#[coroutine] static move || {
22+
| ++++++
1523

1624
error: aborting due to 2 previous errors
1725

0 commit comments

Comments
 (0)