Skip to content

Commit 9e345a5

Browse files
Revise async block error message
1 parent 2b9c8ff commit 9e345a5

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,33 +1324,32 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13241324
Applicability::MachineApplicable,
13251325
);
13261326

1327-
let msg = match category {
1327+
match category {
13281328
ConstraintCategory::Return(_) | ConstraintCategory::OpaqueType => {
1329-
format!("{} is returned here", kind)
1329+
let msg = format!("{} is returned here", kind);
1330+
err.span_note(constraint_span, &msg);
13301331
}
13311332
ConstraintCategory::CallArgument => {
13321333
fr_name.highlight_region_name(&mut err);
1333-
format!("function requires argument type to outlive `{}`", fr_name)
1334+
if matches!(use_span.generator_kind(), Some(generator_kind)
1335+
if matches!(generator_kind, GeneratorKind::Async(_)))
1336+
{
1337+
err.note("async blocks are not executed immediately and either must take a \
1338+
reference or ownership of outside variables they use");
1339+
err.help("see https://rust-lang.github.io/async-book/03_async_await/01_chapter.html#awaiting-on-a-multithreaded-executor \
1340+
for more information");
1341+
} else {
1342+
let msg = format!("function requires argument type to outlive `{}`", fr_name);
1343+
err.span_note(constraint_span, &msg);
1344+
}
13341345
}
13351346
_ => bug!(
13361347
"report_escaping_closure_capture called with unexpected constraint \
13371348
category: `{:?}`",
13381349
category
13391350
),
1340-
};
1341-
err.span_note(constraint_span, &msg);
1342-
if let ConstraintCategory::CallArgument = category {
1343-
if let Some(generator_kind) = use_span.generator_kind() {
1344-
if let GeneratorKind::Async(_) = generator_kind {
1345-
err.note(
1346-
"borrows cannot be held across a yield point, because the stack \
1347-
space of the current function is not preserved",
1348-
);
1349-
err.help("see https://rust-lang.github.io/async-book/03_async_await/01_chapter.html#awaiting-on-a-multithreaded-executor \
1350-
for more information");
1351-
}
1352-
}
13531351
}
1352+
13541353
err
13551354
}
13561355

src/test/ui/async-await/issues/issue-78938-async-block.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@ LL | | game_loop(Arc::clone(&room_ref))
88
LL | | });
99
| |_____^ may outlive borrowed value `room_ref`
1010
|
11-
note: function requires argument type to outlive `'static`
12-
--> $DIR/issue-78938-async-block.rs:8:33
13-
|
14-
LL | let gameloop_handle = spawn(async {
15-
| _________________________________^
16-
LL | | game_loop(Arc::clone(&room_ref))
17-
LL | | });
18-
| |_____^
19-
= note: borrows cannot be held across a yield point, because the stack space of the current function is not preserved
11+
= note: async blocks are not executed immediately and either must take a reference or ownership of outside variables they use
2012
= help: see https://rust-lang.github.io/async-book/03_async_await/01_chapter.html#awaiting-on-a-multithreaded-executor for more information
2113
help: to force the async block to take ownership of `room_ref` (and any other referenced variables), use the `move` keyword
2214
|

0 commit comments

Comments
 (0)