about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-19 22:48:57 +0200
committerGitHub <noreply@github.com>2019-08-19 22:48:57 +0200
commit2c0f05a04f60bcfc006cd4f24e501eafe879a076 (patch)
treef6593dc21d33594f1c00665ba7a442b7d227c63e /src
parenta2080a60aca2caab01903573ab0730660d7cb8e5 (diff)
parentef3e66d69f18574b3f32fca76d4324c486afa874 (diff)
downloadrust-2c0f05a04f60bcfc006cd4f24e501eafe879a076.tar.gz
rust-2c0f05a04f60bcfc006cd4f24e501eafe879a076.zip
Rollup merge of #63699 - gilescope:async-move-diagnostic, r=estebank
Fix suggestion from incorrect `move async` to `async move`.

PR for #61920. Happy with the test. There must be a better implementation though - possibly a MIR visitor to estabilsh a span that doesn't include the `async` keyword?
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/borrow_check/conflict_errors.rs11
-rw-r--r--src/test/ui/async-await/async-borrowck-escaping-closure-error.rs10
-rw-r--r--src/test/ui/async-await/async-borrowck-escaping-closure-error.stderr21
3 files changed, 41 insertions, 1 deletions
diff --git a/src/librustc_mir/borrow_check/conflict_errors.rs b/src/librustc_mir/borrow_check/conflict_errors.rs
index 4217a29bc66..247783c420e 100644
--- a/src/librustc_mir/borrow_check/conflict_errors.rs
+++ b/src/librustc_mir/borrow_check/conflict_errors.rs
@@ -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 ") {
+                    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()
         };
 
diff --git a/src/test/ui/async-await/async-borrowck-escaping-closure-error.rs b/src/test/ui/async-await/async-borrowck-escaping-closure-error.rs
new file mode 100644
index 00000000000..d2fa5d0a3d0
--- /dev/null
+++ b/src/test/ui/async-await/async-borrowck-escaping-closure-error.rs
@@ -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() {
+}
diff --git a/src/test/ui/async-await/async-borrowck-escaping-closure-error.stderr b/src/test/ui/async-await/async-borrowck-escaping-closure-error.stderr
new file mode 100644
index 00000000000..8bcfcf98920
--- /dev/null
+++ b/src/test/ui/async-await/async-borrowck-escaping-closure-error.stderr
@@ -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`.