-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #77341 - davidtwco:issue-73427-you-might-have-meant-var…
…iant, r=estebank resolve: improve "try using the enum's variant" Fixes #73427. This PR improves the "try using the enum's variant" suggestion: - Variants in suggestions would not result in more errors (e.g. use of a struct variant is only suggested if the suggestion can trivially construct that variant). Therefore, suggestions are only emitted for variants that have no fields (since the suggestion can't know what value fields would have). - Suggestions include the syntax for constructing the variant. If a struct or tuple variant is suggested, then it is constructed in the suggestion - unless in pattern-matching or when arguments are already provided. - A help message is added which mentions the variants which are no longer suggested. All of the diagnostic logic introduced by this PR is separated from the normal code path for a successful compilation. r? `@estebank`
- Loading branch information
Showing
5 changed files
with
258 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
enum A { | ||
StructWithFields { x: () }, | ||
TupleWithFields(()), | ||
Struct {}, | ||
Tuple(), | ||
Unit, | ||
} | ||
|
||
enum B { | ||
StructWithFields { x: () }, | ||
TupleWithFields(()), | ||
} | ||
|
||
enum C { | ||
StructWithFields { x: () }, | ||
TupleWithFields(()), | ||
Unit, | ||
} | ||
|
||
enum D { | ||
TupleWithFields(()), | ||
Unit, | ||
} | ||
|
||
fn main() { | ||
// Only variants without fields are suggested (and others mentioned in a note) where an enum | ||
// is used rather than a variant. | ||
|
||
A.foo(); | ||
//~^ ERROR expected value, found enum `A` | ||
B.foo(); | ||
//~^ ERROR expected value, found enum `B` | ||
C.foo(); | ||
//~^ ERROR expected value, found enum `C` | ||
D.foo(); | ||
//~^ ERROR expected value, found enum `D` | ||
|
||
// Only tuple variants are suggested in calls or tuple struct pattern matching. | ||
|
||
let x = A(3); | ||
//~^ ERROR expected function, tuple struct or tuple variant, found enum `A` | ||
if let A(3) = x { } | ||
//~^ ERROR expected tuple struct or tuple variant, found enum `A` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
error[E0423]: expected value, found enum `A` | ||
--> $DIR/issue-73427.rs:29:5 | ||
| | ||
LL | A.foo(); | ||
| ^ | ||
| | ||
= help: you might have meant to use one of the enum's other variants that have fields | ||
help: try using one of the enum's variants | ||
| | ||
LL | (A::Struct {}).foo(); | ||
| ^^^^^^^^^^^^^^ | ||
LL | (A::Tuple()).foo(); | ||
| ^^^^^^^^^^^^ | ||
LL | A::Unit.foo(); | ||
| ^^^^^^^ | ||
|
||
error[E0423]: expected value, found enum `B` | ||
--> $DIR/issue-73427.rs:31:5 | ||
| | ||
LL | B.foo(); | ||
| ^ | ||
| | ||
= help: you might have meant to use one of the enum's variants | ||
|
||
error[E0423]: expected value, found enum `C` | ||
--> $DIR/issue-73427.rs:33:5 | ||
| | ||
LL | C.foo(); | ||
| ^ help: try using one of the enum's variants: `C::Unit` | ||
| | ||
= help: you might have meant to use one of the enum's other variants that have fields | ||
|
||
error[E0423]: expected value, found enum `D` | ||
--> $DIR/issue-73427.rs:35:5 | ||
| | ||
LL | D.foo(); | ||
| ^ help: try using one of the enum's variants: `D::Unit` | ||
| | ||
= help: you might have meant to use the enum's other variant that has fields | ||
|
||
error[E0423]: expected function, tuple struct or tuple variant, found enum `A` | ||
--> $DIR/issue-73427.rs:40:13 | ||
| | ||
LL | let x = A(3); | ||
| ^ | ||
| | ||
= help: you might have meant to construct one of the enum's non-tuple variants | ||
help: try using one of the enum's variants | ||
| | ||
LL | let x = A::TupleWithFields(3); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
LL | let x = A::Tuple(3); | ||
| ^^^^^^^^ | ||
|
||
error[E0532]: expected tuple struct or tuple variant, found enum `A` | ||
--> $DIR/issue-73427.rs:42:12 | ||
| | ||
LL | if let A(3) = x { } | ||
| ^ | ||
| | ||
= help: you might have meant to match against one of the enum's non-tuple variants | ||
help: try using one of the enum's variants | ||
| | ||
LL | if let A::TupleWithFields(3) = x { } | ||
| ^^^^^^^^^^^^^^^^^^ | ||
LL | if let A::Tuple(3) = x { } | ||
| ^^^^^^^^ | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
Some errors have detailed explanations: E0423, E0532. | ||
For more information about an error, try `rustc --explain E0423`. |
Oops, something went wrong.