]> git.lizzy.rs Git - rust.git/commitdiff
Don't reuse bindings for `ref mut`
authorJoshua Nelson <jyn514@gmail.com>
Sun, 8 Nov 2020 16:45:13 +0000 (11:45 -0500)
committerJoshua Nelson <jyn514@gmail.com>
Thu, 12 Nov 2020 16:13:05 +0000 (11:13 -0500)
Reusing bindings causes errors later in lowering:

```
 error[E0596]: cannot borrow `vec` as mutable, as it is not declared as mutable
  --> /checkout/src/test/ui/async-await/argument-patterns.rs:12:20
   |
LL | async fn b(n: u32, ref mut vec: A) {
   |                    ^^^^^^^^^^^
   |                    |
   |                    cannot borrow as mutable
   |                    help: consider changing this to be mutable: `mut vec`
```

compiler/rustc_ast_lowering/src/item.rs
src/test/rustdoc/async-fn.rs

index 0cfcd843dddc36762fa122a795fa50eba3519793..1335bb02580f67e85df1eedcdf0866dbef22abda 100644 (file)
@@ -1102,6 +1102,10 @@ fn lower_maybe_async_body(
                         ident,
                         _,
                     ) => (ident, true),
+                    // For `ref mut` arguments, we can't reuse the binding, but
+                    // we can keep the same name for the parameter.
+                    // This lets rustdoc render it correctly in documentation.
+                    hir::PatKind::Binding(_, _, ident, _) => (ident, false),
                     _ => {
                         // Replace the ident for bindings that aren't simple.
                         let name = format!("__arg{}", index);
index 5a03e821e8a2ff637b7adbc2117dd987652ba04a..d7c890738299da0ad225774fff0604e06756bdca 100644 (file)
@@ -20,6 +20,12 @@ pub async fn baz<T>(a: T) -> T {
     '⚠'
 }
 
+// @has async_fn/fn.mut_args.html '//pre[@class="rust fn"]' 'pub async fn mut_args(a: usize)'
+pub async fn mut_args(mut a: usize) {}
+
+// @has async_fn/fn.mut_ref.html '//pre[@class="rust fn"]' 'pub async fn mut_ref(x: i32)'
+pub async fn mut_ref(ref mut x: i32) {}
+
 trait Bar {}
 
 impl Bar for () {}
@@ -32,9 +38,11 @@ pub async fn quux() -> impl Bar {
 // @has async_fn/struct.Foo.html
 // @matches - '//code' 'pub async fn f\(\)$'
 // @matches - '//code' 'pub async unsafe fn g\(\)$'
+// @matches - '//code' 'pub async fn mut_self\(self, first: usize\)$'
 pub struct Foo;
 
 impl Foo {
     pub async fn f() {}
     pub async unsafe fn g() {}
+    pub async fn mut_self(mut self, mut first: usize) {}
 }