Add detailed error message with solution when detecting Rust style enums
This is a further polishing of #4942. It adds Diagnostic to CompileError::Parse to achieve the following messages:
error: Enum variant declaration is not valid
enum Enum1 {
Ok, // Illegal
^^ `Ok` is not a valid enum variant declaration.
-- help: Did you mean `Ok: ()`?
help: In Sway, enum variants are in the form `Variant: ()`, `Variant: <type>`, or `Variant: (<type1>, ..., <typeN>)`.
help: E.g., `Foo: (), `Bar: u64`, or `Bar: (bool, u32)`.
error: Enum variant declaration is not valid
enum Enum2 {
F(u32), // Illegal
^^^^^^ `F(u32)` is not a valid enum variant declaration.
------ help: Did you mean `F: (u32)`?
help: In Sway, enum variants are in the form `Variant: ()`, `Variant: <type>`, or `Variant: (<type1>, ..., <typeN>)`.
help: E.g., `Foo: (), `Bar: u64`, or `Bar: (bool, u32)`.
error: Enum variant declaration is not valid
enum Enum3 {
A(MyType, bool, u8),
^^^^^^^^^^^^^^^^^^^ `A(MyType, bool, u8)` is not a valid enum variant declaration.
------------------- help: Did you mean `A: (MyType, bool, u8)`?
help: In Sway, enum variants are in the form `Variant: ()`, `Variant: <type>`, or `Variant: (<type1>, ..., <typeN>)`.
help: E.g., `Foo: (), `Bar: u64`, or `Bar: (bool, u32)`.
We do not have to parse the Rust struct-like variant. That would be a bit too much work, and it's also not a kind of a mistake Sway programmers do. In that and all other cases we can go for the common help message at the bottom of the message without the particular suggestion.
error: Enum variant declaration is not valid
enum Enum4 {
A { x: i32, y: i32 },
^^^^^^^^^^^^^^^^^^^ `A { x: i32, y: i32 }` is not a valid enum variant declaration.
help: In Sway, enum variants are in the form `Variant: ()`, `Variant: <type>`, or `Variant: (<type1>, ..., <typeN>)`.
help: E.g., `Foo: (), `Bar: u64`, or `Bar: (bool, u32)`.
Hi! I have experience with Rust and wrote some procedural macros in the past, and I think that it has something similar with writing parsers. Also, I like low-level staff a lot. Please let me tackle it
Hi,
I’m Manasseh, and I’d love to work on this! As a Rustacean with 3 years of experience and solid programming principles. I’ll add clear error messages with fixes for Rust-style enums, like turning Ok into Ok: () or F(u32) into F: (u32). I’ll use Diagnostic in CompileError::Parse to make it helpful and easy to read.
Let me take this—I’ll make it sharp and useful!
Thanks,
Manasseh
Hi, I am super sorry for delaying this issue so much. I'll push my work and create a PR tomorrow. Thank you for your patience.