diff options
| author | 1000teslas <47207223+1000teslas@users.noreply.github.com> | 2021-01-09 19:46:19 +1100 |
|---|---|---|
| committer | 1000teslas <47207223+1000teslas@users.noreply.github.com> | 2021-01-10 16:47:41 +1100 |
| commit | 9e345a58936edc81acf09d89675f71711f3d2439 (patch) | |
| tree | 422c4739be6cd461aa1ed1122f5d43e0ba09b5cf | |
| parent | 2b9c8ff6b37e0fd9143ba0f5a1fd11057880cebc (diff) | |
| download | rust-9e345a58936edc81acf09d89675f71711f3d2439.tar.gz rust-9e345a58936edc81acf09d89675f71711f3d2439.zip | |
Revise async block error message
| -rw-r--r-- | compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs | 31 | ||||
| -rw-r--r-- | src/test/ui/async-await/issues/issue-78938-async-block.stderr | 10 |
2 files changed, 16 insertions, 25 deletions
diff --git a/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs b/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs index 43e9701aa25..85ea70cefba 100644 --- a/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs +++ b/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs @@ -1324,33 +1324,32 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { Applicability::MachineApplicable, ); - let msg = match category { + match category { ConstraintCategory::Return(_) | ConstraintCategory::OpaqueType => { - format!("{} is returned here", kind) + let msg = format!("{} is returned here", kind); + err.span_note(constraint_span, &msg); } ConstraintCategory::CallArgument => { fr_name.highlight_region_name(&mut err); - format!("function requires argument type to outlive `{}`", fr_name) + if matches!(use_span.generator_kind(), Some(generator_kind) + if matches!(generator_kind, GeneratorKind::Async(_))) + { + err.note("async blocks are not executed immediately and either must take a \ + reference or ownership of outside variables they use"); + err.help("see https://rust-lang.github.io/async-book/03_async_await/01_chapter.html#awaiting-on-a-multithreaded-executor \ + for more information"); + } else { + let msg = format!("function requires argument type to outlive `{}`", fr_name); + err.span_note(constraint_span, &msg); + } } _ => bug!( "report_escaping_closure_capture called with unexpected constraint \ category: `{:?}`", category ), - }; - err.span_note(constraint_span, &msg); - if let ConstraintCategory::CallArgument = category { - if let Some(generator_kind) = use_span.generator_kind() { - if let GeneratorKind::Async(_) = generator_kind { - err.note( - "borrows cannot be held across a yield point, because the stack \ - space of the current function is not preserved", - ); - err.help("see https://rust-lang.github.io/async-book/03_async_await/01_chapter.html#awaiting-on-a-multithreaded-executor \ - for more information"); - } - } } + err } diff --git a/src/test/ui/async-await/issues/issue-78938-async-block.stderr b/src/test/ui/async-await/issues/issue-78938-async-block.stderr index 604c47b430f..4468975b2f5 100644 --- a/src/test/ui/async-await/issues/issue-78938-async-block.stderr +++ b/src/test/ui/async-await/issues/issue-78938-async-block.stderr @@ -8,15 +8,7 @@ LL | | game_loop(Arc::clone(&room_ref)) LL | | }); | |_____^ may outlive borrowed value `room_ref` | -note: function requires argument type to outlive `'static` - --> $DIR/issue-78938-async-block.rs:8:33 - | -LL | let gameloop_handle = spawn(async { - | _________________________________^ -LL | | game_loop(Arc::clone(&room_ref)) -LL | | }); - | |_____^ - = note: borrows cannot be held across a yield point, because the stack space of the current function is not preserved + = note: async blocks are not executed immediately and either must take a reference or ownership of outside variables they use = help: see https://rust-lang.github.io/async-book/03_async_await/01_chapter.html#awaiting-on-a-multithreaded-executor for more information help: to force the async block to take ownership of `room_ref` (and any other referenced variables), use the `move` keyword | |
