Skip to content

Commit 747a5e5

Browse files
committed
Auto merge of #56584 - davidtwco:issue-53990, r=nikomatsakis
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.
2 parents 7f04a64 + 7fcf31b commit 747a5e5

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/libsyntax/parse/parser.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ impl<'a> Parser<'a> {
14071407
// definition...
14081408

14091409
// We don't allow argument names to be left off in edition 2018.
1410-
p.parse_arg_general(p.span.rust_2018())
1410+
p.parse_arg_general(p.span.rust_2018(), true)
14111411
})?;
14121412
generics.where_clause = self.parse_where_clause()?;
14131413

@@ -1820,7 +1820,7 @@ impl<'a> Parser<'a> {
18201820

18211821
/// This version of parse arg doesn't necessarily require
18221822
/// identifier names.
1823-
fn parse_arg_general(&mut self, require_name: bool) -> PResult<'a, Arg> {
1823+
fn parse_arg_general(&mut self, require_name: bool, is_trait_item: bool) -> PResult<'a, Arg> {
18241824
maybe_whole!(self, NtArg, |x| x);
18251825

18261826
if let Ok(Some(_)) = self.parse_self_arg() {
@@ -1852,6 +1852,17 @@ impl<'a> Parser<'a> {
18521852
String::from("<identifier>: <type>"),
18531853
Applicability::HasPlaceholders,
18541854
);
1855+
} else if require_name && is_trait_item {
1856+
if let PatKind::Ident(_, ident, _) = pat.node {
1857+
err.span_suggestion_with_applicability(
1858+
pat.span,
1859+
"explicitly ignore parameter",
1860+
format!("_: {}", ident),
1861+
Applicability::MachineApplicable,
1862+
);
1863+
}
1864+
1865+
err.note("anonymous parameters are removed in the 2018 edition (see RFC 1685)");
18551866
}
18561867

18571868
return Err(err);
@@ -1917,7 +1928,7 @@ impl<'a> Parser<'a> {
19171928

19181929
/// Parse a single function argument
19191930
crate fn parse_arg(&mut self) -> PResult<'a, Arg> {
1920-
self.parse_arg_general(true)
1931+
self.parse_arg_general(true, false)
19211932
}
19221933

19231934
/// Parse an argument in a lambda header e.g., |arg, arg|
@@ -5473,7 +5484,7 @@ impl<'a> Parser<'a> {
54735484
}
54745485
}
54755486
} else {
5476-
match p.parse_arg_general(named_args) {
5487+
match p.parse_arg_general(named_args, false) {
54775488
Ok(arg) => Ok(Some(arg)),
54785489
Err(mut e) => {
54795490
e.emit();

src/test/ui/anon-params-denied-2018.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@ error: expected one of `:` or `@`, found `)`
22
--> $DIR/anon-params-denied-2018.rs:6:15
33
|
44
LL | fn foo(i32); //~ expected one of `:` or `@`, found `)`
5-
| ^ expected one of `:` or `@` here
5+
| ---^ expected one of `:` or `@` here
6+
| |
7+
| help: explicitly ignore parameter: `_: i32`
8+
|
9+
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
610

711
error: expected one of `:` or `@`, found `,`
812
--> $DIR/anon-params-denied-2018.rs:8:36
913
|
1014
LL | fn bar_with_default_impl(String, String) {}
11-
| ^ expected one of `:` or `@` here
15+
| ------^ expected one of `:` or `@` here
16+
| |
17+
| help: explicitly ignore parameter: `_: String`
18+
|
19+
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
1220

1321
error: aborting due to 2 previous errors
1422

0 commit comments

Comments
 (0)