Skip to content
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

Fix suggestion from incorrect move async to async move. #63699

Merged
merged 1 commit into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/librustc_mir/borrow_check/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
);

let suggestion = match tcx.sess.source_map().span_to_snippet(args_span) {
Ok(string) => format!("move {}", string),
Ok(mut string) => {
if string.starts_with("async ") {
Copy link
Contributor

@jakubadamw jakubadamw Aug 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a rather error-prone way of checking this that will fail with:

async
|x| {}

or

async\t|x|

I don't think this should be looking at the code snippet but rather trace with self.tcx whether this closure was an async one before lowering. I sadly cannot tell you off the top of my head how to do it and cannot look at the code at this very moment but maybe someone else can chim in. @estebank?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true. Ideally you'd want to get the original node, but I don't think you can. An alternative that keeps operating on the string (less than ideal, as already established) would be to use string.split_whitespace().next() which would yield Some("async") for the cases we care about in a pretty foolproof way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String operations for these checks is not ideal, but sometimes we have no way around them unless we track more things from the AST onwards.

string.insert_str(6, "move ");
} else if string.starts_with("async|") {
string.insert_str(5, " move");
} else {
string.insert_str(0, "move ");
};
string
},
Err(_) => "move |<args>| <body>".to_string()
};

Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/async-await/async-borrowck-escaping-closure-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// edition:2018
#![feature(async_closure,async_await)]
fn foo() -> Box<dyn std::future::Future<Output = u32>> {
let x = 0u32;
Box::new((async || x)())
//~^ ERROR E0373
}

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
--> $DIR/async-borrowck-escaping-closure-error.rs:5:15
|
LL | Box::new((async || x)())
| ^^^^^^^^ - `x` is borrowed here
| |
| may outlive borrowed value `x`
|
note: closure is returned here
--> $DIR/async-borrowck-escaping-closure-error.rs:5:5
|
LL | Box::new((async || x)())
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `x` (and any other referenced variables), use the `move` keyword
|
LL | Box::new((async move || x)())
| ^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0373`.