Skip to content

Add detailed error message with solution when detecting Rust style enums #5403

Open
@ironcev

Description

@ironcev

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)`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ODHack12compilerGeneral compiler. Should eventually become more specific as the issue is triagedcompiler: parserEverything to do with the parsergood first issueGood for newcomersteam:compilerCompiler Team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions