Skip to content

Commit 359bd1d

Browse files
committed
add error message for c# style named arguments
bless tests
1 parent 21982a4 commit 359bd1d

File tree

7 files changed

+234
-42
lines changed

7 files changed

+234
-42
lines changed

compiler/rustc_parse/src/parser/expr.rs

+26
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,32 @@ 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+
151+
let snapshot = self.create_snapshot_for_diagnostic();
152+
self.bump();
153+
match self.parse_expr() {
154+
Ok(expr) => {
155+
err.emit();
156+
Ok(expr)
157+
}
158+
159+
Err(expr_err) => {
160+
expr_err.cancel();
161+
self.restore_snapshot(snapshot);
162+
Err(err)
163+
}
164+
}
165+
}
140166
_ => Err(err),
141167
},
142168
}
+24-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
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[E0425]: cannot find value `mx` in this scope
14+
--> $DIR/issue-111416.rs:2:25
15+
|
16+
LL | let my = monad_bind(mx, T: Try);
17+
| ^^ not found in this scope
18+
19+
error[E0425]: cannot find value `Try` in this scope
20+
--> $DIR/issue-111416.rs:2:32
21+
|
22+
LL | let my = monad_bind(mx, T: Try);
23+
| ^^^ not found in this scope
24+
25+
error[E0425]: cannot find function `monad_bind` in this scope
26+
--> $DIR/issue-111416.rs:2:14
27+
|
28+
LL | let my = monad_bind(mx, T: Try);
29+
| ^^^^^^^^^^ not found in this scope
30+
31+
error: aborting due to 4 previous errors
1832

33+
For more information about this error, try `rustc --explain E0425`.
+28-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
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[E0425]: cannot find value `field` in this scope
23+
--> $DIR/issue-34255-1.rs:8:17
24+
|
25+
LL | Test::Drill(field: 42);
26+
| ^^^^^ not found in this scope
27+
28+
error[E0533]: expected value, found struct variant `Test::Drill`
29+
--> $DIR/issue-34255-1.rs:8:5
30+
|
31+
LL | Test::Drill(field: 42);
32+
| ^^^^^^^^^^^ not a value
33+
34+
error: aborting due to 4 previous errors
1835

36+
Some errors have detailed explanations: E0425, E0533.
37+
For more information about an error, try `rustc --explain E0425`.
+42-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,55 @@
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[E0425]: cannot find value `baz` in this scope
32+
--> $DIR/issue-44406.rs:3:13
33+
|
34+
LL | bar(baz: $rest)
35+
| ^^^ not found in this scope
36+
...
37+
LL | foo!(true);
38+
| ---------- in this macro invocation
39+
|
40+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
41+
42+
error[E0425]: cannot find function `bar` in this scope
43+
--> $DIR/issue-44406.rs:3:9
44+
|
45+
LL | bar(baz: $rest)
46+
| ^^^ not found in this scope
47+
...
48+
LL | foo!(true);
49+
| ---------- in this macro invocation
50+
|
51+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
52+
53+
error: aborting due to 4 previous errors
2254

55+
For more information about this error, try `rustc --explain E0425`.
+25-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1+
error: expected expression, found `:`
2+
--> $DIR/issue-91461.rs:2:8
3+
|
4+
LL | a(_:b:,)
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+
|
12+
13+
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `:`
14+
--> $DIR/issue-91461.rs:2:8
15+
|
16+
LL | a(_:b:,)
17+
| ^
18+
| |
19+
| expected one of `)`, `,`, `.`, `?`, or an operator
20+
| help: missing `,`
21+
122
error: expected identifier, found reserved identifier `_`
223
--> $DIR/issue-91461.rs:2:7
324
|
425
LL | a(_:b:,)
526
| ^ expected identifier, found reserved identifier
627

7-
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `:`
8-
--> $DIR/issue-91461.rs:2:8
28+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
29+
--> $DIR/issue-91461.rs:2:10
930
|
1031
LL | a(_:b:,)
11-
| ^ expected one of `)`, `,`, `.`, `?`, or an operator
32+
| ^ expected one of 8 possible tokens
1233

13-
error: aborting due to 2 previous errors
34+
error: aborting due to 4 previous errors
1435

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

+54-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,62 @@
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
3+
|
4+
LL | let x = Enum::Foo(a: 3, b: 4);
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);
11+
|
12+
13+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
14+
--> $DIR/recover-from-bad-variant.rs:7:24
315
|
416
LL | let x = Enum::Foo(a: 3, b: 4);
5-
| ^^^^^^^^^^^^^^^^^^^^^
17+
| ^
18+
| |
19+
| expected one of 8 possible tokens
20+
| help: missing `,`
21+
22+
error: expected expression, found `:`
23+
--> $DIR/recover-from-bad-variant.rs:7:30
624
|
7-
help: if `Enum::Foo` is a struct, use braces as delimiters
25+
LL | let x = Enum::Foo(a: 3, b: 4);
26+
| ^ expected expression
827
|
9-
LL | let x = Enum::Foo { a: 3, b: 4 };
10-
| ~ ~
11-
help: if `Enum::Foo` is a function, use the arguments directly
28+
help: if this is a parameter, remove the name for the parameter
1229
|
1330
LL - let x = Enum::Foo(a: 3, b: 4);
14-
LL + let x = Enum::Foo(3, 4);
31+
LL + let x = Enum::Foo(a: 3, 4);
32+
|
33+
34+
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
35+
--> $DIR/recover-from-bad-variant.rs:7:30
1536
|
37+
LL | let x = Enum::Foo(a: 3, b: 4);
38+
| ^
39+
| |
40+
| expected one of 8 possible tokens
41+
| help: missing `,`
42+
43+
error[E0425]: cannot find value `a` in this scope
44+
--> $DIR/recover-from-bad-variant.rs:7:23
45+
|
46+
LL | let x = Enum::Foo(a: 3, b: 4);
47+
| ^ not found in this scope
48+
49+
error[E0425]: cannot find value `b` in this scope
50+
--> $DIR/recover-from-bad-variant.rs:7:29
51+
|
52+
LL | let x = Enum::Foo(a: 3, b: 4);
53+
| ^ not found in this scope
54+
55+
error[E0533]: expected value, found struct variant `Enum::Foo`
56+
--> $DIR/recover-from-bad-variant.rs:7:13
57+
|
58+
LL | let x = Enum::Foo(a: 3, b: 4);
59+
| ^^^^^^^^^ not a value
1660

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

34-
error: aborting due to 3 previous errors
78+
error: aborting due to 9 previous errors
3579

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

tests/ui/type/type-ascription-precedence.stderr

+35-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ error: expected identifier, found `:`
99
|
1010
LL | *(S: Z);
1111
| ^ expected identifier
12+
|
13+
help: if this is a parameter, remove the name for the parameter
14+
|
15+
LL - *(S: Z);
16+
LL + *(Z);
17+
|
1218

1319
error: expected identifier, found `:`
1420
--> $DIR/type-ascription-precedence.rs:37:8
1521
|
1622
LL | -(S: Z);
1723
| ^ expected identifier
24+
|
25+
help: if this is a parameter, remove the name for the parameter
26+
|
27+
LL - -(S: Z);
28+
LL + -(Z);
29+
|
1830

1931
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `:`
2032
--> $DIR/type-ascription-precedence.rs:41:12
@@ -42,5 +54,27 @@ error: expected one of `.`, `;`, `?`, `}`, or an operator, found `:`
4254
LL | (S .. S): S;
4355
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
4456

45-
error: aborting due to 7 previous errors
57+
error[E0614]: type `Z` cannot be dereferenced
58+
--> $DIR/type-ascription-precedence.rs:33:5
59+
|
60+
LL | *(S: Z);
61+
| ^^^^^^^
62+
63+
error[E0600]: cannot apply unary operator `-` to type `Z`
64+
--> $DIR/type-ascription-precedence.rs:37:5
65+
|
66+
LL | -(S: Z);
67+
| ^^^^^^^ cannot apply unary operator `-`
68+
|
69+
note: an implementation of `std::ops::Neg` might be missing for `Z`
70+
--> $DIR/type-ascription-precedence.rs:7:1
71+
|
72+
LL | struct Z;
73+
| ^^^^^^^^ must implement `std::ops::Neg`
74+
note: the trait `std::ops::Neg` must be implemented
75+
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
76+
77+
error: aborting due to 9 previous errors
4678

79+
Some errors have detailed explanations: E0600, E0614.
80+
For more information about an error, try `rustc --explain E0600`.

0 commit comments

Comments
 (0)