Skip to content

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

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

Open
ironcev opened this issue Dec 17, 2023 · 3 comments · May be fixed by #7047
Open

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

ironcev opened this issue Dec 17, 2023 · 3 comments · May be fixed by #7047
Labels
compiler: parser Everything to do with the parser compiler General compiler. Should eventually become more specific as the issue is triaged good first issue Good for newcomers ODHack12 team:compiler Compiler Team

Comments

@ironcev
Copy link
Member

ironcev commented Dec 17, 2023

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)`.
@ironcev ironcev added good first issue Good for newcomers compiler General compiler. Should eventually become more specific as the issue is triaged compiler: parser Everything to do with the parser labels Dec 17, 2023
@IGI-111 IGI-111 added the team:compiler Compiler Team label Feb 10, 2025
@kfastov
Copy link

kfastov commented Mar 20, 2025

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

@manassehO
Copy link

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

@kfastov
Copy link

kfastov commented Mar 29, 2025

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.

@kfastov kfastov linked a pull request Mar 30, 2025 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: parser Everything to do with the parser compiler General compiler. Should eventually become more specific as the issue is triaged good first issue Good for newcomers ODHack12 team:compiler Compiler Team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants