-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Explain why borrows can't be held across yield point in async blocks #80614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
5ccef56
12f1795
2b9c8ff
9e345a5
757bd23
3ee3071
7f41465
f5c4287
a9ead34
174135f
63deae5
5468d98
3e9c95b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1324,21 +1324,34 @@ 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 \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm worried this link can go stale. It seems like using the error index would be better, we can update this link there if it ever changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this page the error index? If so, which error are you referring to? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is the error index. I believe @tmandry's idea is to modify https://github.com/rust-lang/rust/blob/9e5f7d5631b8f4009ac1c693e585d4b7108d4275/compiler/rustc_error_codes/src/error_codes/E0373.md to include information from and/or a link to chapter 1 of the async book. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've moved the link to error index in my latest commit. |
||
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); | ||
} | ||
|
||
err | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// edition:2018 | ||
|
||
use std::{sync::Arc, future::Future, pin::Pin, task::{Context,Poll}}; | ||
|
||
async fn f() { | ||
let room_ref = Arc::new(Vec::new()); | ||
|
||
let gameloop_handle = spawn(async { //~ ERROR E0373 | ||
game_loop(Arc::clone(&room_ref)) | ||
}); | ||
gameloop_handle.await; | ||
} | ||
|
||
fn game_loop(v: Arc<Vec<usize>>) {} | ||
|
||
fn spawn<F>(future: F) -> JoinHandle | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send + 'static, | ||
{ | ||
loop {} | ||
} | ||
|
||
struct JoinHandle; | ||
|
||
impl Future for JoinHandle { | ||
type Output = (); | ||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
loop {} | ||
} | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||||||||||||||||||||||||||||||
error[E0373]: async block may outlive the current function, but it borrows `room_ref`, which is owned by the current function | ||||||||||||||||||||||||||||||||
--> $DIR/issue-78938-async-block.rs:8:39 | ||||||||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||||||||
LL | let gameloop_handle = spawn(async { | ||||||||||||||||||||||||||||||||
| _______________________________________^ | ||||||||||||||||||||||||||||||||
LL | | game_loop(Arc::clone(&room_ref)) | ||||||||||||||||||||||||||||||||
| | -------- `room_ref` is borrowed here | ||||||||||||||||||||||||||||||||
LL | | }); | ||||||||||||||||||||||||||||||||
| |_____^ may outlive borrowed value `room_ref` | ||||||||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||||||||
= note: async blocks are not executed immediately and either must take a reference or ownership of outside variables they use | ||||||||||||||||||||||||||||||||
sledgehammervampire marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
= 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 | ||||||||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||||||||
LL | let gameloop_handle = spawn(async move { | ||||||||||||||||||||||||||||||||
LL | game_loop(Arc::clone(&room_ref)) | ||||||||||||||||||||||||||||||||
LL | }); | ||||||||||||||||||||||||||||||||
Comment on lines
+12
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll need to check the suggestion that generates this becuase it seems to me we don't really need to show the whole block for this and we could make it less verbose. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps show only the line with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixing that requires "only" to modify the following rust/compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs Lines 1287 to 1301 in afa7408
The If you wish to tackle this in this PR, it'd be great, but I don't consider it a must have yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I'll leave that for later or someone else. |
||||||||||||||||||||||||||||||||
| | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
error: aborting due to previous error | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
For more information about this error, try `rustc --explain E0373`. |
Uh oh!
There was an error while loading. Please reload this page.