Skip to content

Commit 121caa9

Browse files
committed
Correct handling of arguments in async fn
1 parent 747dd57 commit 121caa9

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

src/libsyntax/parse/parser.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ impl<'a> Parser<'a> {
15761576
let ident = self.parse_ident()?;
15771577
let mut generics = self.parse_generics()?;
15781578

1579-
let d = self.parse_fn_decl_with_self(|p: &mut Parser<'a>| {
1579+
let mut decl = self.parse_fn_decl_with_self(|p: &mut Parser<'a>| {
15801580
// This is somewhat dubious; We don't want to allow
15811581
// argument names to be left off if there is a
15821582
// definition...
@@ -1585,7 +1585,7 @@ impl<'a> Parser<'a> {
15851585
p.parse_arg_general(p.span.rust_2018(), true, false)
15861586
})?;
15871587
generics.where_clause = self.parse_where_clause()?;
1588-
self.construct_async_arguments(&mut asyncness, &d);
1588+
self.construct_async_arguments(&mut asyncness, &mut decl);
15891589

15901590
let sig = ast::MethodSig {
15911591
header: FnHeader {
@@ -1594,7 +1594,7 @@ impl<'a> Parser<'a> {
15941594
abi,
15951595
asyncness,
15961596
},
1597-
decl: d,
1597+
decl,
15981598
};
15991599

16001600
let body = match self.token {
@@ -6479,10 +6479,10 @@ impl<'a> Parser<'a> {
64796479
-> PResult<'a, ItemInfo> {
64806480
let (ident, mut generics) = self.parse_fn_header()?;
64816481
let allow_c_variadic = abi == Abi::C && unsafety == Unsafety::Unsafe;
6482-
let decl = self.parse_fn_decl(allow_c_variadic)?;
6482+
let mut decl = self.parse_fn_decl(allow_c_variadic)?;
64836483
generics.where_clause = self.parse_where_clause()?;
64846484
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
6485-
self.construct_async_arguments(&mut asyncness, &decl);
6485+
self.construct_async_arguments(&mut asyncness, &mut decl);
64866486
let header = FnHeader { unsafety, asyncness, constness, abi };
64876487
Ok((ident, ItemKind::Fn(decl, header, generics, body), Some(inner_attrs)))
64886488
}
@@ -6666,9 +6666,9 @@ impl<'a> Parser<'a> {
66666666
let (constness, unsafety, mut asyncness, abi) = self.parse_fn_front_matter()?;
66676667
let ident = self.parse_ident()?;
66686668
let mut generics = self.parse_generics()?;
6669-
let decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
6669+
let mut decl = self.parse_fn_decl_with_self(|p| p.parse_arg())?;
66706670
generics.where_clause = self.parse_where_clause()?;
6671-
self.construct_async_arguments(&mut asyncness, &decl);
6671+
self.construct_async_arguments(&mut asyncness, &mut decl);
66726672
*at_end = true;
66736673
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
66746674
let header = ast::FnHeader { abi, unsafety, constness, asyncness };
@@ -8714,9 +8714,9 @@ impl<'a> Parser<'a> {
87148714
///
87158715
/// The arguments of the function are replaced in HIR lowering with the arguments created by
87168716
/// this function and the statements created here are inserted at the top of the closure body.
8717-
fn construct_async_arguments(&mut self, asyncness: &mut Spanned<IsAsync>, decl: &FnDecl) {
8717+
fn construct_async_arguments(&mut self, asyncness: &mut Spanned<IsAsync>, decl: &mut FnDecl) {
87188718
if let IsAsync::Async { ref mut arguments, .. } = asyncness.node {
8719-
for (index, input) in decl.inputs.iter().enumerate() {
8719+
for (index, input) in decl.inputs.iter_mut().enumerate() {
87208720
let id = ast::DUMMY_NODE_ID;
87218721
let span = input.pat.span;
87228722

@@ -8728,8 +8728,10 @@ impl<'a> Parser<'a> {
87288728
// `let <pat> = __argN;` statement, instead just adding a `let <pat> = <pat>;`
87298729
// statement.
87308730
let (binding_mode, ident, is_simple_pattern) = match input.pat.node {
8731-
PatKind::Ident(binding_mode, ident, _) => (binding_mode, ident, true),
8732-
_ => (BindingMode::ByValue(Mutability::Immutable), ident, false),
8731+
PatKind::Ident(binding_mode @ BindingMode::ByValue(_), ident, _) => {
8732+
(binding_mode, ident, true)
8733+
}
8734+
_ => (BindingMode::ByValue(Mutability::Mutable), ident, false),
87338735
};
87348736

87358737
// Construct an argument representing `__argN: <ty>` to replace the argument of the
@@ -8796,6 +8798,15 @@ impl<'a> Parser<'a> {
87968798
})
87978799
};
87988800

8801+
// Remove mutability from arguments. If this is not a simple pattern,
8802+
// those arguments are replaced by `__argN`, so there is no need to do this.
8803+
if let PatKind::Ident(BindingMode::ByValue(mutability @ Mutability::Mutable), ..) =
8804+
&mut input.pat.node
8805+
{
8806+
assert!(is_simple_pattern);
8807+
*mutability = Mutability::Immutable;
8808+
}
8809+
87998810
let move_stmt = Stmt { id, node: StmtKind::Local(P(move_local)), span };
88008811
arguments.push(AsyncArgument { ident, arg, pat_stmt, move_stmt });
88018812
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// edition:2018
2+
// run-pass
3+
4+
#![allow(unused_variables)]
5+
#![deny(unused_mut)]
6+
#![feature(async_await)]
7+
8+
type A = Vec<u32>;
9+
10+
async fn a(n: u32, mut vec: A) {
11+
vec.push(n);
12+
}
13+
14+
async fn b(n: u32, ref mut vec: A) {
15+
vec.push(n);
16+
}
17+
18+
async fn c(ref vec: A) {
19+
vec.contains(&0);
20+
}
21+
22+
async fn d((a, mut b): (A, A)) {
23+
b.push(1);
24+
}
25+
26+
async fn f((ref mut a, ref b): (A, A)) {}
27+
28+
async fn g(((ref a, ref mut b), (ref mut c, ref d)): ((A, A), (A, A))) {}
29+
30+
fn main() {}

src/test/ui/async-await/mutable-arguments.rs

-10
This file was deleted.

0 commit comments

Comments
 (0)