From e4dc15a96905217dbdf857159f67bfd79d5e4d7c Mon Sep 17 00:00:00 2001 From: David Wood Date: Fri, 7 Dec 2018 10:54:14 +0100 Subject: [PATCH 1/2] Add context for RFC 1685 change in 2018 edition. This commit adds a note providing context for the change to argument names being required in the 2018 edition for trait methods. --- src/libsyntax/parse/parser.rs | 10 ++++++---- src/test/ui/anon-params-denied-2018.stderr | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index c7eaf4d1eeeb7..af63314b15447 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1404,7 +1404,7 @@ impl<'a> Parser<'a> { // definition... // We don't allow argument names to be left off in edition 2018. - p.parse_arg_general(p.span.rust_2018()) + p.parse_arg_general(p.span.rust_2018(), true) })?; generics.where_clause = self.parse_where_clause()?; @@ -1817,7 +1817,7 @@ impl<'a> Parser<'a> { /// This version of parse arg doesn't necessarily require /// identifier names. - fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> { + fn parse_arg_general(&mut self, require_name: bool, is_trait_item: bool) -> PResult<'a, Arg> { maybe_whole!(self, NtArg, |x| x); if let Ok(Some(_)) = self.parse_self_arg() { @@ -1849,6 +1849,8 @@ impl<'a> Parser<'a> { String::from(": "), Applicability::HasPlaceholders, ); + } else if require_name && is_trait_item { + err.note("anonymous parameters are removed in the 2018 edition (see RFC 1685)"); } return Err(err); @@ -1914,7 +1916,7 @@ impl<'a> Parser<'a> { /// Parse a single function argument crate fn parse_arg(&mut self) -> PResult<'a, Arg> { - self.parse_arg_general(true) + self.parse_arg_general(true, false) } /// Parse an argument in a lambda header e.g. |arg, arg| @@ -5469,7 +5471,7 @@ impl<'a> Parser<'a> { } } } else { - match p.parse_arg_general(named_args) { + match p.parse_arg_general(named_args, false) { Ok(arg) => Ok(Some(arg)), Err(mut e) => { e.emit(); diff --git a/src/test/ui/anon-params-denied-2018.stderr b/src/test/ui/anon-params-denied-2018.stderr index 24a1e6ecd932c..e1aa65981f6fd 100644 --- a/src/test/ui/anon-params-denied-2018.stderr +++ b/src/test/ui/anon-params-denied-2018.stderr @@ -3,12 +3,16 @@ error: expected one of `:` or `@`, found `)` | LL | fn foo(i32); //~ expected one of `:` or `@`, found `)` | ^ expected one of `:` or `@` here + | + = note: anonymous parameters are removed in the 2018 edition (see RFC 1685) error: expected one of `:` or `@`, found `,` --> $DIR/anon-params-denied-2018.rs:8:36 | LL | fn bar_with_default_impl(String, String) {} | ^ expected one of `:` or `@` here + | + = note: anonymous parameters are removed in the 2018 edition (see RFC 1685) error: aborting due to 2 previous errors From 7fcf31b181433cc67b17af550d87fca1e3394f90 Mon Sep 17 00:00:00 2001 From: David Wood Date: Fri, 7 Dec 2018 11:16:13 +0100 Subject: [PATCH 2/2] Add suggestion for underscore binding fix. This commit emits a suggestion for adding an underscore binding to arguments in trait methods that previously did not have a argument name specified. --- src/libsyntax/parse/parser.rs | 9 +++++++++ src/test/ui/anon-params-denied-2018.stderr | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index af63314b15447..560078136eb8c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1850,6 +1850,15 @@ impl<'a> Parser<'a> { Applicability::HasPlaceholders, ); } else if require_name && is_trait_item { + if let PatKind::Ident(_, ident, _) = pat.node { + err.span_suggestion_with_applicability( + pat.span, + "explicitly ignore parameter", + format!("_: {}", ident), + Applicability::MachineApplicable, + ); + } + err.note("anonymous parameters are removed in the 2018 edition (see RFC 1685)"); } diff --git a/src/test/ui/anon-params-denied-2018.stderr b/src/test/ui/anon-params-denied-2018.stderr index e1aa65981f6fd..dd9e933542fa7 100644 --- a/src/test/ui/anon-params-denied-2018.stderr +++ b/src/test/ui/anon-params-denied-2018.stderr @@ -2,7 +2,9 @@ error: expected one of `:` or `@`, found `)` --> $DIR/anon-params-denied-2018.rs:6:15 | LL | fn foo(i32); //~ expected one of `:` or `@`, found `)` - | ^ expected one of `:` or `@` here + | ---^ expected one of `:` or `@` here + | | + | help: explicitly ignore parameter: `_: i32` | = note: anonymous parameters are removed in the 2018 edition (see RFC 1685) @@ -10,7 +12,9 @@ error: expected one of `:` or `@`, found `,` --> $DIR/anon-params-denied-2018.rs:8:36 | LL | fn bar_with_default_impl(String, String) {} - | ^ expected one of `:` or `@` here + | ------^ expected one of `:` or `@` here + | | + | help: explicitly ignore parameter: `_: String` | = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)