Skip to content

Commit a78747c

Browse files
committed
add error message for c# style named arguments
1 parent 21982a4 commit a78747c

File tree

7 files changed

+295
-43
lines changed

7 files changed

+295
-43
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ impl<'a> Parser<'a> {
137137
self.bump();
138138
Ok(self.mk_expr(self.prev_token.span, ExprKind::Err))
139139
}
140+
None if self.may_recover()
141+
&& self.prev_token.is_ident()
142+
&& self.token.kind == token::Colon =>
143+
{
144+
err.span_suggestion_verbose(
145+
self.prev_token.span.until(self.look_ahead(1, |t| t.span)),
146+
"if this is a parameter, remove the name for the parameter",
147+
"",
148+
Applicability::MaybeIncorrect,
149+
);
150+
self.bump();
151+
err.emit();
152+
Ok(self.mk_expr(self.token.span, ExprKind::Err))
153+
}
140154
_ => Err(err),
141155
},
142156
}
Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
1-
error: invalid `struct` delimiters or `fn` call arguments
2-
--> $DIR/issue-111416.rs:2:14
1+
error: expected identifier, found `:`
2+
--> $DIR/issue-111416.rs:2:30
33
|
44
LL | let my = monad_bind(mx, T: Try);
5-
| ^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
help: if `monad_bind` is a struct, use braces as delimiters
5+
| ^ expected identifier
86
|
9-
LL | let my = monad_bind { mx, T: Try };
10-
| ~ ~
11-
help: if `monad_bind` is a function, use the arguments directly
7+
help: if this is a parameter, remove the name for the parameter
128
|
139
LL - let my = monad_bind(mx, T: Try);
1410
LL + let my = monad_bind(mx, Try);
1511
|
1612

17-
error: aborting due to 1 previous error
13+
error: expected one of `)` or `,`, found `Try`
14+
--> $DIR/issue-111416.rs:2:32
15+
|
16+
LL | let my = monad_bind(mx, T: Try);
17+
| -^^^ expected one of `)` or `,`
18+
| |
19+
| help: missing `,`
20+
21+
error[E0425]: cannot find value `mx` in this scope
22+
--> $DIR/issue-111416.rs:2:25
23+
|
24+
LL | let my = monad_bind(mx, T: Try);
25+
| ^^ not found in this scope
26+
27+
error[E0425]: cannot find value `Try` in this scope
28+
--> $DIR/issue-111416.rs:2:32
29+
|
30+
LL | let my = monad_bind(mx, T: Try);
31+
| ^^^ not found in this scope
32+
33+
error[E0425]: cannot find function `monad_bind` in this scope
34+
--> $DIR/issue-111416.rs:2:14
35+
|
36+
LL | let my = monad_bind(mx, T: Try);
37+
| ^^^^^^^^^^ not found in this scope
38+
39+
error: aborting due to 5 previous errors
1840

41+
For more information about this error, try `rustc --explain E0425`.
Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
1-
error: invalid `struct` delimiters or `fn` call arguments
2-
--> $DIR/issue-34255-1.rs:8:5
1+
error: expected expression, found `:`
2+
--> $DIR/issue-34255-1.rs:8:22
33
|
44
LL | Test::Drill(field: 42);
5-
| ^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
help: if `Test::Drill` is a struct, use braces as delimiters
5+
| ^ expected expression
86
|
9-
LL | Test::Drill { field: 42 };
10-
| ~ ~
11-
help: if `Test::Drill` is a function, use the arguments directly
7+
help: if this is a parameter, remove the name for the parameter
128
|
139
LL - Test::Drill(field: 42);
1410
LL + Test::Drill(42);
1511
|
1612

17-
error: aborting due to 1 previous error
13+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
14+
--> $DIR/issue-34255-1.rs:8:22
15+
|
16+
LL | Test::Drill(field: 42);
17+
| ^
18+
| |
19+
| expected one of 8 possible tokens
20+
| help: missing `,`
21+
22+
error: expected one of `)` or `,`, found `42`
23+
--> $DIR/issue-34255-1.rs:8:24
24+
|
25+
LL | Test::Drill(field: 42);
26+
| -^^ expected one of `)` or `,`
27+
| |
28+
| help: missing `,`
29+
30+
error[E0425]: cannot find value `field` in this scope
31+
--> $DIR/issue-34255-1.rs:8:17
32+
|
33+
LL | Test::Drill(field: 42);
34+
| ^^^^^ not found in this scope
35+
36+
error[E0533]: expected value, found struct variant `Test::Drill`
37+
--> $DIR/issue-34255-1.rs:8:5
38+
|
39+
LL | Test::Drill(field: 42);
40+
| ^^^^^^^^^^^ not a value
41+
42+
error: aborting due to 5 previous errors
1843

44+
Some errors have detailed explanations: E0425, E0533.
45+
For more information about an error, try `rustc --explain E0425`.
Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,64 @@
1-
error: invalid `struct` delimiters or `fn` call arguments
2-
--> $DIR/issue-44406.rs:3:9
1+
error: expected expression, found `:`
2+
--> $DIR/issue-44406.rs:3:16
33
|
44
LL | bar(baz: $rest)
5-
| ^^^^^^^^^^^^^^^
5+
| ^ expected expression
66
...
77
LL | foo!(true);
88
| ---------- in this macro invocation
99
|
1010
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
11-
help: if `bar` is a struct, use braces as delimiters
12-
|
13-
LL | bar { baz: $rest }
14-
| ~ ~
15-
help: if `bar` is a function, use the arguments directly
11+
help: if this is a parameter, remove the name for the parameter
1612
|
1713
LL - bar(baz: $rest)
1814
LL + bar(: $rest)
1915
|
2016

21-
error: aborting due to 1 previous error
17+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
18+
--> $DIR/issue-44406.rs:3:16
19+
|
20+
LL | bar(baz: $rest)
21+
| ^
22+
| |
23+
| expected one of 8 possible tokens
24+
| help: missing `,`
25+
...
26+
LL | foo!(true);
27+
| ---------- in this macro invocation
28+
|
29+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
30+
31+
error: expected one of `)` or `,`, found keyword `true`
32+
--> $DIR/issue-44406.rs:8:10
33+
|
34+
LL | bar(baz: $rest)
35+
| - help: missing `,`
36+
...
37+
LL | foo!(true);
38+
| ^^^^ expected one of `)` or `,`
39+
40+
error[E0425]: cannot find value `baz` in this scope
41+
--> $DIR/issue-44406.rs:3:13
42+
|
43+
LL | bar(baz: $rest)
44+
| ^^^ not found in this scope
45+
...
46+
LL | foo!(true);
47+
| ---------- in this macro invocation
48+
|
49+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
50+
51+
error[E0425]: cannot find function `bar` in this scope
52+
--> $DIR/issue-44406.rs:3:9
53+
|
54+
LL | bar(baz: $rest)
55+
| ^^^ not found in this scope
56+
...
57+
LL | foo!(true);
58+
| ---------- in this macro invocation
59+
|
60+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
61+
62+
error: aborting due to 5 previous errors
2263

64+
For more information about this error, try `rustc --explain E0425`.
Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,72 @@
1-
error: expected identifier, found reserved identifier `_`
2-
--> $DIR/issue-91461.rs:2:7
1+
error: expected expression, found `:`
2+
--> $DIR/issue-91461.rs:2:8
33
|
44
LL | a(_:b:,)
5-
| ^ expected identifier, found reserved identifier
5+
| ^ expected expression
6+
|
7+
help: if this is a parameter, remove the name for the parameter
8+
|
9+
LL - a(_:b:,)
10+
LL + a(b:,)
11+
|
612

713
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `:`
814
--> $DIR/issue-91461.rs:2:8
915
|
1016
LL | a(_:b:,)
11-
| ^ expected one of `)`, `,`, `.`, `?`, or an operator
17+
| ^
18+
| |
19+
| expected one of `)`, `,`, `.`, `?`, or an operator
20+
| help: missing `,`
21+
22+
error: expected one of `)` or `,`, found `b`
23+
--> $DIR/issue-91461.rs:2:9
24+
|
25+
LL | a(_:b:,)
26+
| ^
27+
| |
28+
| expected one of `)` or `,`
29+
| help: missing `,`
30+
31+
error: expected expression, found `:`
32+
--> $DIR/issue-91461.rs:2:10
33+
|
34+
LL | a(_:b:,)
35+
| ^ expected expression
36+
|
37+
help: if this is a parameter, remove the name for the parameter
38+
|
39+
LL - a(_:b:,)
40+
LL + a(_:,)
41+
|
42+
43+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
44+
--> $DIR/issue-91461.rs:2:10
45+
|
46+
LL | a(_:b:,)
47+
| ^
48+
| |
49+
| expected one of 8 possible tokens
50+
| help: missing `,`
51+
52+
error[E0425]: cannot find value `b` in this scope
53+
--> $DIR/issue-91461.rs:2:9
54+
|
55+
LL | a(_:b:,)
56+
| ^ not found in this scope
57+
58+
error: in expressions, `_` can only be used on the left-hand side of an assignment
59+
--> $DIR/issue-91461.rs:2:7
60+
|
61+
LL | a(_:b:,)
62+
| ^ `_` not allowed here
63+
64+
error[E0425]: cannot find function `a` in this scope
65+
--> $DIR/issue-91461.rs:2:5
66+
|
67+
LL | a(_:b:,)
68+
| ^ not found in this scope
1269

13-
error: aborting due to 2 previous errors
70+
error: aborting due to 8 previous errors
1471

72+
For more information about this error, try `rustc --explain E0425`.

tests/ui/parser/recover/recover-from-bad-variant.stderr

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,78 @@
1-
error: invalid `struct` delimiters or `fn` call arguments
2-
--> $DIR/recover-from-bad-variant.rs:7:13
1+
error: expected expression, found `:`
2+
--> $DIR/recover-from-bad-variant.rs:7:24
33
|
44
LL | let x = Enum::Foo(a: 3, b: 4);
5-
| ^^^^^^^^^^^^^^^^^^^^^
5+
| ^ expected expression
6+
|
7+
help: if this is a parameter, remove the name for the parameter
8+
|
9+
LL - let x = Enum::Foo(a: 3, b: 4);
10+
LL + let x = Enum::Foo(3, b: 4);
611
|
7-
help: if `Enum::Foo` is a struct, use braces as delimiters
12+
13+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
14+
--> $DIR/recover-from-bad-variant.rs:7:24
815
|
9-
LL | let x = Enum::Foo { a: 3, b: 4 };
10-
| ~ ~
11-
help: if `Enum::Foo` is a function, use the arguments directly
16+
LL | let x = Enum::Foo(a: 3, b: 4);
17+
| ^
18+
| |
19+
| expected one of 8 possible tokens
20+
| help: missing `,`
21+
22+
error: expected one of `)` or `,`, found `3`
23+
--> $DIR/recover-from-bad-variant.rs:7:26
24+
|
25+
LL | let x = Enum::Foo(a: 3, b: 4);
26+
| -^ expected one of `)` or `,`
27+
| |
28+
| help: missing `,`
29+
30+
error: expected expression, found `:`
31+
--> $DIR/recover-from-bad-variant.rs:7:30
32+
|
33+
LL | let x = Enum::Foo(a: 3, b: 4);
34+
| ^ expected expression
35+
|
36+
help: if this is a parameter, remove the name for the parameter
1237
|
1338
LL - let x = Enum::Foo(a: 3, b: 4);
14-
LL + let x = Enum::Foo(3, 4);
39+
LL + let x = Enum::Foo(a: 3, 4);
40+
|
41+
42+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
43+
--> $DIR/recover-from-bad-variant.rs:7:30
1544
|
45+
LL | let x = Enum::Foo(a: 3, b: 4);
46+
| ^
47+
| |
48+
| expected one of 8 possible tokens
49+
| help: missing `,`
50+
51+
error: expected one of `)` or `,`, found `4`
52+
--> $DIR/recover-from-bad-variant.rs:7:32
53+
|
54+
LL | let x = Enum::Foo(a: 3, b: 4);
55+
| -^ expected one of `)` or `,`
56+
| |
57+
| help: missing `,`
58+
59+
error[E0425]: cannot find value `a` in this scope
60+
--> $DIR/recover-from-bad-variant.rs:7:23
61+
|
62+
LL | let x = Enum::Foo(a: 3, b: 4);
63+
| ^ not found in this scope
64+
65+
error[E0425]: cannot find value `b` in this scope
66+
--> $DIR/recover-from-bad-variant.rs:7:29
67+
|
68+
LL | let x = Enum::Foo(a: 3, b: 4);
69+
| ^ not found in this scope
70+
71+
error[E0533]: expected value, found struct variant `Enum::Foo`
72+
--> $DIR/recover-from-bad-variant.rs:7:13
73+
|
74+
LL | let x = Enum::Foo(a: 3, b: 4);
75+
| ^^^^^^^^^ not a value
1676

1777
error[E0164]: expected tuple struct or tuple variant, found struct variant `Enum::Foo`
1878
--> $DIR/recover-from-bad-variant.rs:10:9
@@ -31,7 +91,7 @@ help: use the tuple variant pattern syntax instead
3191
LL | Enum::Bar(a, b) => {}
3292
| ~~~~~~
3393

34-
error: aborting due to 3 previous errors
94+
error: aborting due to 11 previous errors
3595

36-
Some errors have detailed explanations: E0164, E0769.
96+
Some errors have detailed explanations: E0164, E0425, E0533, E0769.
3797
For more information about an error, try `rustc --explain E0164`.

0 commit comments

Comments
 (0)