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 43e9701aa25a..85ea70cefba3 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 604c47b430f8..4468975b2f5c 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 |