about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTaiki Endo <te316e89@gmail.com>2019-05-05 12:03:32 +0900
committerTaiki Endo <te316e89@gmail.com>2019-05-05 12:03:32 +0900
commit121caa927bc4dace0df64f728f62fbce42a15fe9 (patch)
treeea803a3dc1863ce02adbc3ef05f719bef705a2e8
parent747dd57e0f2eafc4c632f8698525e45db2f3e5ec (diff)
downloadrust-121caa927bc4dace0df64f728f62fbce42a15fe9.tar.gz
rust-121caa927bc4dace0df64f728f62fbce42a15fe9.zip
Correct handling of arguments in async fn
-rw-r--r--src/libsyntax/parse/parser.rs33
-rw-r--r--src/test/ui/async-await/argument-patterns.rs30
-rw-r--r--src/test/ui/async-await/mutable-arguments.rs10
3 files changed, 52 insertions, 21 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index d46feeab335..83f8b0e90c5 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -1576,7 +1576,7 @@ impl<'a> Parser<'a> {
             let ident = self.parse_ident()?;
             let mut generics = self.parse_generics()?;
 
-            let d = self.parse_fn_decl_with_self(|p: &mut Parser<'a>| {
+            let mut decl = self.parse_fn_decl_with_self(|p: &mut Parser<'a>| {
                 // This is somewhat dubious; We don't want to allow
                 // argument names to be left off if there is a
                 // definition...
@@ -1585,7 +1585,7 @@ impl<'a> Parser<'a> {
                 p.parse_arg_general(p.span.rust_2018(), true, false)
             })?;
             generics.where_clause = self.parse_where_clause()?;
-            self.construct_async_arguments(&mut asyncness, &d);
+            self.construct_async_arguments(&mut asyncness, &mut decl);
 
             let sig = ast::MethodSig {
                 header: FnHeader {
@@ -1594,7 +1594,7 @@ impl<'a> Parser<'a> {
                     abi,
                     asyncness,
                 },
-                decl: d,
+                decl,
             };
 
             let body = match self.token {
@@ -6479,10 +6479,10 @@ impl<'a> Parser<'a> {
                      -> PResult<'a, ItemInfo> {
         let (ident, mut generics) = self.parse_fn_header()?;
         let allow_c_variadic = abi == Abi::C && unsafety == Unsafety::Unsafe;
-        let decl = self.parse_fn_decl(allow_c_variadic)?;
+        let mut decl = self.parse_fn_decl(allow_c_variadic)?;
         generics.where_clause = self.parse_where_clause()?;
         let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
-        self.construct_async_arguments(&mut asyncness, &decl);
+        self.construct_async_arguments(&mut asyncness, &mut decl);
         let header = FnHeader { unsafety, asyncness, constness, abi };
         Ok((ident, ItemKind::Fn(decl, header, generics, body), Some(inner_attrs)))
     }
@@ -6666,9 +6666,9 @@ impl<'a> Parser<'a> {
             let (constness, unsafety, mut asyncness, abi) = self.parse_fn_front_matter()?;
             let ident = self.parse_ident()?;
             let mut generics = self.parse_generics()?;
-            let decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
+            let mut decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
             generics.where_clause = self.parse_where_clause()?;
-            self.construct_async_arguments(&mut asyncness, &decl);
+            self.construct_async_arguments(&mut asyncness, &mut decl);
             *at_end = true;
             let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
             let header = ast::FnHeader { abi, unsafety, constness, asyncness };
@@ -8714,9 +8714,9 @@ impl<'a> Parser<'a> {
     ///
     /// The arguments of the function are replaced in HIR lowering with the arguments created by
     /// this function and the statements created here are inserted at the top of the closure body.
-    fn construct_async_arguments(&mut self, asyncness: &mut Spanned<IsAsync>, decl: &FnDecl) {
+    fn construct_async_arguments(&mut self, asyncness: &mut Spanned<IsAsync>, decl: &mut FnDecl) {
         if let IsAsync::Async { ref mut arguments, .. } = asyncness.node {
-            for (index, input) in decl.inputs.iter().enumerate() {
+            for (index, input) in decl.inputs.iter_mut().enumerate() {
                 let id = ast::DUMMY_NODE_ID;
                 let span = input.pat.span;
 
@@ -8728,8 +8728,10 @@ impl<'a> Parser<'a> {
                 // `let <pat> = __argN;` statement, instead just adding a `let <pat> = <pat>;`
                 // statement.
                 let (binding_mode, ident, is_simple_pattern) = match input.pat.node {
-                    PatKind::Ident(binding_mode, ident, _) => (binding_mode, ident, true),
-                    _ => (BindingMode::ByValue(Mutability::Immutable), ident, false),
+                    PatKind::Ident(binding_mode @ BindingMode::ByValue(_), ident, _) => {
+                        (binding_mode, ident, true)
+                    }
+                    _ => (BindingMode::ByValue(Mutability::Mutable), ident, false),
                 };
 
                 // Construct an argument representing `__argN: <ty>` to replace the argument of the
@@ -8796,6 +8798,15 @@ impl<'a> Parser<'a> {
                     })
                 };
 
+                // Remove mutability from arguments. If this is not a simple pattern,
+                // those arguments are replaced by `__argN`, so there is no need to do this.
+                if let PatKind::Ident(BindingMode::ByValue(mutability @ Mutability::Mutable), ..) =
+                    &mut input.pat.node
+                {
+                    assert!(is_simple_pattern);
+                    *mutability = Mutability::Immutable;
+                }
+
                 let move_stmt = Stmt { id, node: StmtKind::Local(P(move_local)), span };
                 arguments.push(AsyncArgument { ident, arg, pat_stmt, move_stmt });
             }
diff --git a/src/test/ui/async-await/argument-patterns.rs b/src/test/ui/async-await/argument-patterns.rs
new file mode 100644
index 00000000000..3750c2bcb70
--- /dev/null
+++ b/src/test/ui/async-await/argument-patterns.rs
@@ -0,0 +1,30 @@
+// edition:2018
+// run-pass
+
+#![allow(unused_variables)]
+#![deny(unused_mut)]
+#![feature(async_await)]
+
+type A = Vec<u32>;
+
+async fn a(n: u32, mut vec: A) {
+    vec.push(n);
+}
+
+async fn b(n: u32, ref mut vec: A) {
+    vec.push(n);
+}
+
+async fn c(ref vec: A) {
+    vec.contains(&0);
+}
+
+async fn d((a, mut b): (A, A)) {
+    b.push(1);
+}
+
+async fn f((ref mut a, ref b): (A, A)) {}
+
+async fn g(((ref a, ref mut b), (ref mut c, ref d)): ((A, A), (A, A))) {}
+
+fn main() {}
diff --git a/src/test/ui/async-await/mutable-arguments.rs b/src/test/ui/async-await/mutable-arguments.rs
deleted file mode 100644
index 4d6dba74097..00000000000
--- a/src/test/ui/async-await/mutable-arguments.rs
+++ /dev/null
@@ -1,10 +0,0 @@
-// edition:2018
-// run-pass
-
-#![feature(async_await)]
-
-async fn foo(n: u32, mut vec: Vec<u32>) {
-    vec.push(n);
-}
-
-fn main() {}