Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error message for c# style named arguments #118733

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Expand Up @@ -137,6 +137,32 @@ impl<'a> Parser<'a> {
self.bump();
Ok(self.mk_expr(self.prev_token.span, ExprKind::Err))
}
None if self.may_recover()
&& self.prev_token.is_ident()
&& self.token.kind == token::Colon =>
{
err.span_suggestion_verbose(
self.prev_token.span.until(self.look_ahead(1, |t| t.span)),
"if this is a parameter, remove the name for the parameter",
"",
Applicability::MaybeIncorrect,
);

let snapshot = self.create_snapshot_for_diagnostic();
self.bump();
match self.parse_expr() {
Ok(expr) => {
err.emit();
Ok(expr)
}

Err(expr_err) => {
expr_err.cancel();
self.restore_snapshot(snapshot);
Err(err)
}
}
}
_ => Err(err),
},
}
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/parser/issues/issue-111416.rs
@@ -1,3 +1,7 @@
fn main() {
let my = monad_bind(mx, T: Try); //~ ERROR invalid `struct` delimiters or `fn` call arguments
let my = monad_bind(mx, T: Try);
//~^ ERROR expected identifier, found `:`
//~| ERROR cannot find value `mx`
//~| ERROR cannot find value `Try`
//~| ERROR cannot find function `monad_bind`
}
33 changes: 24 additions & 9 deletions tests/ui/parser/issues/issue-111416.stderr
@@ -1,18 +1,33 @@
error: invalid `struct` delimiters or `fn` call arguments
--> $DIR/issue-111416.rs:2:14
error: expected identifier, found `:`
--> $DIR/issue-111416.rs:2:30
|
LL | let my = monad_bind(mx, T: Try);
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: if `monad_bind` is a struct, use braces as delimiters
| ^ expected identifier
|
LL | let my = monad_bind { mx, T: Try };
| ~ ~
help: if `monad_bind` is a function, use the arguments directly
Comment on lines -7 to -10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still sad of losing this suggestion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible for me to add on this suggestion along with the named arguments error? something like -

if this is a parameter, remove the parameter name
...

if this is a struct, use braces as delimiters
...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you could likely include it to. Look at the existing one because there's some subtlety here: this suggestion is only potentially valid if all the previous expressions in the argument list are either an ident (like mx) or the type ascription (which is no longer represented in the expression syntax tree, the T: Try), so we only emit the that suggestion if when encountering the problem, all prior args are just idents. We would also want to remove the older logic as now it won't trigger anymore.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, I'll take a look at the existing solution and try to adapt it to this. Can we go over other changes that should be made to get all the tests to pass?

help: if this is a parameter, remove the name for the parameter
|
LL - let my = monad_bind(mx, T: Try);
LL + let my = monad_bind(mx, Try);
|

error: aborting due to 1 previous error
error[E0425]: cannot find value `mx` in this scope
--> $DIR/issue-111416.rs:2:25
|
LL | let my = monad_bind(mx, T: Try);
| ^^ not found in this scope

error[E0425]: cannot find value `Try` in this scope
--> $DIR/issue-111416.rs:2:32
|
LL | let my = monad_bind(mx, T: Try);
| ^^^ not found in this scope

error[E0425]: cannot find function `monad_bind` in this scope
--> $DIR/issue-111416.rs:2:14
|
LL | let my = monad_bind(mx, T: Try);
| ^^^^^^^^^^ not found in this scope

nouritsu marked this conversation as resolved.
Show resolved Hide resolved
error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0425`.
37 changes: 28 additions & 9 deletions tests/ui/parser/issues/issue-34255-1.stderr
@@ -1,18 +1,37 @@
error: invalid `struct` delimiters or `fn` call arguments
--> $DIR/issue-34255-1.rs:8:5
error: expected expression, found `:`
--> $DIR/issue-34255-1.rs:8:22
|
LL | Test::Drill(field: 42);
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: if `Test::Drill` is a struct, use braces as delimiters
| ^ expected expression
|
LL | Test::Drill { field: 42 };
| ~ ~
help: if `Test::Drill` is a function, use the arguments directly
help: if this is a parameter, remove the name for the parameter
|
LL - Test::Drill(field: 42);
LL + Test::Drill(42);
|

error: aborting due to 1 previous error
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
--> $DIR/issue-34255-1.rs:8:22
|
LL | Test::Drill(field: 42);
| ^
| |
| expected one of 8 possible tokens
| help: missing `,`

error[E0425]: cannot find value `field` in this scope
--> $DIR/issue-34255-1.rs:8:17
|
LL | Test::Drill(field: 42);
| ^^^^^ not found in this scope

error[E0533]: expected value, found struct variant `Test::Drill`
--> $DIR/issue-34255-1.rs:8:5
|
LL | Test::Drill(field: 42);
| ^^^^^^^^^^^ not a value

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0425, E0533.
For more information about an error, try `rustc --explain E0425`.
51 changes: 42 additions & 9 deletions tests/ui/parser/issues/issue-44406.stderr
@@ -1,22 +1,55 @@
error: invalid `struct` delimiters or `fn` call arguments
--> $DIR/issue-44406.rs:3:9
error: expected expression, found `:`
--> $DIR/issue-44406.rs:3:16
|
LL | bar(baz: $rest)
| ^^^^^^^^^^^^^^^
| ^ expected expression
...
LL | foo!(true);
| ---------- in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
help: if `bar` is a struct, use braces as delimiters
|
LL | bar { baz: $rest }
| ~ ~
help: if `bar` is a function, use the arguments directly
help: if this is a parameter, remove the name for the parameter
|
LL - bar(baz: $rest)
LL + bar(: $rest)
|

error: aborting due to 1 previous error
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
--> $DIR/issue-44406.rs:3:16
|
LL | bar(baz: $rest)
| ^
| |
| expected one of 8 possible tokens
| help: missing `,`
...
LL | foo!(true);
| ---------- in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find value `baz` in this scope
--> $DIR/issue-44406.rs:3:13
|
LL | bar(baz: $rest)
| ^^^ not found in this scope
...
LL | foo!(true);
| ---------- in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find function `bar` in this scope
--> $DIR/issue-44406.rs:3:9
|
LL | bar(baz: $rest)
| ^^^ not found in this scope
...
LL | foo!(true);
| ---------- in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0425`.
29 changes: 25 additions & 4 deletions tests/ui/parser/issues/issue-91461.stderr
@@ -1,14 +1,35 @@
error: expected expression, found `:`
--> $DIR/issue-91461.rs:2:8
|
LL | a(_:b:,)
| ^ expected expression
|
help: if this is a parameter, remove the name for the parameter
|
LL - a(_:b:,)
LL + a(b:,)
|

error: expected one of `)`, `,`, `.`, `?`, or an operator, found `:`
--> $DIR/issue-91461.rs:2:8
|
LL | a(_:b:,)
| ^
| |
| expected one of `)`, `,`, `.`, `?`, or an operator
| help: missing `,`

error: expected identifier, found reserved identifier `_`
--> $DIR/issue-91461.rs:2:7
|
LL | a(_:b:,)
| ^ expected identifier, found reserved identifier

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

error: aborting due to 2 previous errors
error: aborting due to 4 previous errors

64 changes: 54 additions & 10 deletions tests/ui/parser/recover/recover-from-bad-variant.stderr
@@ -1,18 +1,62 @@
error: invalid `struct` delimiters or `fn` call arguments
--> $DIR/recover-from-bad-variant.rs:7:13
error: expected expression, found `:`
--> $DIR/recover-from-bad-variant.rs:7:24
|
LL | let x = Enum::Foo(a: 3, b: 4);
| ^ expected expression
|
help: if this is a parameter, remove the name for the parameter
|
LL - let x = Enum::Foo(a: 3, b: 4);
LL + let x = Enum::Foo(3, b: 4);
|

error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
--> $DIR/recover-from-bad-variant.rs:7:24
|
LL | let x = Enum::Foo(a: 3, b: 4);
| ^^^^^^^^^^^^^^^^^^^^^
| ^
| |
| expected one of 8 possible tokens
| help: missing `,`

error: expected expression, found `:`
--> $DIR/recover-from-bad-variant.rs:7:30
|
help: if `Enum::Foo` is a struct, use braces as delimiters
LL | let x = Enum::Foo(a: 3, b: 4);
| ^ expected expression
|
LL | let x = Enum::Foo { a: 3, b: 4 };
| ~ ~
help: if `Enum::Foo` is a function, use the arguments directly
help: if this is a parameter, remove the name for the parameter
|
LL - let x = Enum::Foo(a: 3, b: 4);
LL + let x = Enum::Foo(3, 4);
LL + let x = Enum::Foo(a: 3, 4);
|

error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `:`
--> $DIR/recover-from-bad-variant.rs:7:30
|
LL | let x = Enum::Foo(a: 3, b: 4);
| ^
| |
| expected one of 8 possible tokens
| help: missing `,`

error[E0425]: cannot find value `a` in this scope
--> $DIR/recover-from-bad-variant.rs:7:23
|
LL | let x = Enum::Foo(a: 3, b: 4);
| ^ not found in this scope

error[E0425]: cannot find value `b` in this scope
--> $DIR/recover-from-bad-variant.rs:7:29
|
LL | let x = Enum::Foo(a: 3, b: 4);
| ^ not found in this scope

error[E0533]: expected value, found struct variant `Enum::Foo`
--> $DIR/recover-from-bad-variant.rs:7:13
|
LL | let x = Enum::Foo(a: 3, b: 4);
| ^^^^^^^^^ not a value

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

error: aborting due to 3 previous errors
error: aborting due to 9 previous errors

Some errors have detailed explanations: E0164, E0769.
Some errors have detailed explanations: E0164, E0425, E0533, E0769.
For more information about an error, try `rustc --explain E0164`.
36 changes: 35 additions & 1 deletion tests/ui/type/type-ascription-precedence.stderr
Expand Up @@ -9,12 +9,24 @@ error: expected identifier, found `:`
|
LL | *(S: Z);
| ^ expected identifier
|
help: if this is a parameter, remove the name for the parameter
|
LL - *(S: Z);
LL + *(Z);
|

error: expected identifier, found `:`
--> $DIR/type-ascription-precedence.rs:37:8
|
LL | -(S: Z);
| ^ expected identifier
|
help: if this is a parameter, remove the name for the parameter
|
LL - -(S: Z);
LL + -(Z);
|

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

error: aborting due to 7 previous errors
error[E0614]: type `Z` cannot be dereferenced
--> $DIR/type-ascription-precedence.rs:33:5
|
LL | *(S: Z);
| ^^^^^^^

error[E0600]: cannot apply unary operator `-` to type `Z`
--> $DIR/type-ascription-precedence.rs:37:5
|
LL | -(S: Z);
| ^^^^^^^ cannot apply unary operator `-`
|
note: an implementation of `std::ops::Neg` might be missing for `Z`
--> $DIR/type-ascription-precedence.rs:7:1
|
LL | struct Z;
| ^^^^^^^^ must implement `std::ops::Neg`
note: the trait `std::ops::Neg` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL

error: aborting due to 9 previous errors

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