unfriendly error diagnostic when using expressions in pattern #112593
Closed
Description
opened on Jun 13, 2023
Code
struct Foo(String);
fn bar(foo: Foo) -> bool {
match foo {
Foo("hi".to_owned()) => true,
_ => false
}
}
Current output
Compiling playground v0.0.1 (/playground)
error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found `.`
--> src/lib.rs:5:17
|
5 | Foo("hi".to_owned()) => true,
| ^
| |
| expected one of `)`, `,`, `...`, `..=`, `..`, or `|`
| help: missing `,`
error[E0531]: cannot find tuple struct or tuple variant `to_owned` in this scope
--> src/lib.rs:5:18
|
5 | Foo("hi".to_owned()) => true,
| ^^^^^^^^ not found in this scope
error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 1 field
--> src/lib.rs:5:13
|
1 | struct Foo(String);
| ------ tuple struct has 1 field
...
5 | Foo("hi".to_owned()) => true,
| ^^^^ ^^^^^^^^^^ expected 1 field, found 2
Some errors have detailed explanations: E0023, E0531.
For more information about an error, try `rustc --explain E0023`.
error: could not compile `playground` (lib) due to 3 previous errors
Desired output
Compiling playground v0.0.1 (/playground)
error[E0164]: expected tuple struct or tuple variant, found associated function `<&str as ToOwned>::to_owned`
--> src/lib.rs:5:13
|
5 | Foo("hi".to_owned()) => true,
| ^^^^^^^^^^^^^^^ `fn` calls are not allowed in patterns
|
= help: for more information, visit https://doc.rust-lang.org/reference/patterns.html
For more information about this error, try `rustc --explain E0164`.
error: could not compile `playground` (lib) due to previous error
Rationale and extra context
This error occured while I was doing an assert!(matches!(...))
for a struct that doesn't implement Eq
.
Using String::new
gives better messages;
error[E0164]: expected tuple struct or tuple variant, found associated function `String::new`
--> src/lib.rs:5:20
|
5 | Foo { bar: String::new("hi") } => true,
| ^^^^^^^^^^^^^^^^^ `fn` calls are not allowed in patterns
|
= help: for more information, visit https://doc.rust-lang.org/book/ch18-00-patterns.html
Clippy gives the same error messages.
Other cases
struct Foo { bar: String }
fn bar(foo: Foo) -> bool {
match foo {
Foo { bar: "hi".to_owned() } => true,
_ => false
}
}
rustc output:
Compiling playground v0.0.1 (/playground)
error: expected `,`
--> src/lib.rs:5:24
|
5 | Foo { bar: "hi".to_owned() } => true,
| --- ^
| |
| while parsing the fields for this pattern
error: could not compile `playground` (lib) due to previous error
Similar errors when using expressions such as Foo(1 + 2)
.
Anything else?
No response
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: The parsing of Rust source code to an ASTRelating to patterns and pattern matchingCategory: An issue proposing an enhancement or a PR with one.Diagnostics: An error or lint that needs small tweaks.Call for participation: Help is requested to fix this issue.Call for participation: Medium difficulty. Experience needed to fix: Intermediate.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity