From 380b222f52ee2f506d35ba89bbd8f83f6469f31f Mon Sep 17 00:00:00 2001 From: Eyal Kalderon Date: Tue, 15 Sep 2020 10:21:59 +0800 Subject: [PATCH 1/3] Consider mutable ident binding patterns to be simple This should fix `rustdoc` rendering of by-value mutable arguments in `async fn` contexts. --- compiler/rustc_ast_lowering/src/item.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 617cacee0e7f1..0cfcd843dddc3 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1096,9 +1096,12 @@ impl<'hir> LoweringContext<'_, 'hir> { // Check if this is a binding pattern, if so, we can optimize and avoid adding a // `let = __argN;` statement. In this case, we do not rename the parameter. let (ident, is_simple_parameter) = match parameter.pat.kind { - hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, _) => { - (ident, true) - } + hir::PatKind::Binding( + hir::BindingAnnotation::Unannotated | hir::BindingAnnotation::Mutable, + _, + ident, + _, + ) => (ident, true), _ => { // Replace the ident for bindings that aren't simple. let name = format!("__arg{}", index); From 2baa0ceff4e6dc75751fdca4be68e87f5cf35a6e Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 8 Nov 2020 11:45:13 -0500 Subject: [PATCH 2/3] Don't reuse bindings for `ref mut` 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 | 4 ++++ src/test/rustdoc/async-fn.rs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 0cfcd843dddc3..1335bb02580f6 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1102,6 +1102,10 @@ impl<'hir> LoweringContext<'_, 'hir> { 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); diff --git a/src/test/rustdoc/async-fn.rs b/src/test/rustdoc/async-fn.rs index 5a03e821e8a2f..d7c890738299d 100644 --- a/src/test/rustdoc/async-fn.rs +++ b/src/test/rustdoc/async-fn.rs @@ -20,6 +20,12 @@ pub async unsafe fn qux() -> char { '⚠' } +// @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) {} } From 38127caf730ab6e99d3ea546db4c2df69229afba Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 10 Nov 2020 07:49:06 -0500 Subject: [PATCH 3/3] Handle and test wildcard arguments --- compiler/rustc_ast_lowering/src/item.rs | 5 ++++- src/test/rustdoc/async-fn.rs | 6 ++++++ src/test/rustdoc/const-generics/const-generics-docs.rs | 3 +-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 1335bb02580f6..d353bc19f7aef 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1102,10 +1102,13 @@ impl<'hir> LoweringContext<'_, 'hir> { ident, _, ) => (ident, true), - // For `ref mut` arguments, we can't reuse the binding, but + // For `ref mut` or wildcard 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), + hir::PatKind::Wild => { + (Ident::with_dummy_span(rustc_span::symbol::kw::Underscore), false) + } _ => { // Replace the ident for bindings that aren't simple. let name = format!("__arg{}", index); diff --git a/src/test/rustdoc/async-fn.rs b/src/test/rustdoc/async-fn.rs index d7c890738299d..e7a7d1831f73e 100644 --- a/src/test/rustdoc/async-fn.rs +++ b/src/test/rustdoc/async-fn.rs @@ -1,4 +1,5 @@ // edition:2018 +#![feature(min_const_generics)] // @has async_fn/fn.foo.html '//pre[@class="rust fn"]' 'pub async fn foo() -> Option' pub async fn foo() -> Option { @@ -46,3 +47,8 @@ impl Foo { pub async unsafe fn g() {} pub async fn mut_self(mut self, mut first: usize) {} } + +pub trait Trait {} +// @has async_fn/fn.const_generics.html +// @has - '//pre[@class="rust fn"]' 'pub async fn const_generics(_: impl Trait)' +pub async fn const_generics(_: impl Trait) {} diff --git a/src/test/rustdoc/const-generics/const-generics-docs.rs b/src/test/rustdoc/const-generics/const-generics-docs.rs index 8dcba36600d26..9c68e067c6f8f 100644 --- a/src/test/rustdoc/const-generics/const-generics-docs.rs +++ b/src/test/rustdoc/const-generics/const-generics-docs.rs @@ -70,8 +70,7 @@ pub async fn a_sink(v: [u8; N]) -> impl Trait { } // @has foo/fn.b_sink.html '//pre[@class="rust fn"]' \ -// 'pub async fn b_sink(__arg0: impl Trait)' -// FIXME(const_generics): This should be `_` not `__arg0`. +// 'pub async fn b_sink(_: impl Trait)' pub async fn b_sink(_: impl Trait) {} // @has foo/fn.concrete.html '//pre[@class="rust fn"]' \