Skip to content

Commit

Permalink
Auto merge of #56584 - davidtwco:issue-53990, r=nikomatsakis
Browse files Browse the repository at this point in the history
2018 edition - confusing error message when declaring unnamed parameters

Fixes #53990.

This PR adds a note providing context for the change to argument
names being required in the 2018 edition for trait methods and a
suggestion for the fix.
  • Loading branch information
bors committed Dec 15, 2018
2 parents 7f04a64 + 7fcf31b commit 747a5e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,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()?;

Expand Down Expand Up @@ -1820,7 +1820,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() {
Expand Down Expand Up @@ -1852,6 +1852,17 @@ impl<'a> Parser<'a> {
String::from("<identifier>: <type>"),
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)");
}

return Err(err);
Expand Down Expand Up @@ -1917,7 +1928,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|
Expand Down Expand Up @@ -5473,7 +5484,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();
Expand Down
12 changes: 10 additions & 2 deletions src/test/ui/anon-params-denied-2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ 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)

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)

error: aborting due to 2 previous errors

0 comments on commit 747a5e5

Please sign in to comment.