Skip to content
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

Improve error message for illegal enum variants #5397

Merged
merged 12 commits into from
Dec 18, 2023
2 changes: 2 additions & 0 deletions sway-error/src/parser_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub enum ParseErrorKind {
UnexpectedTokenAfterSliceType,
#[error("Expected a path type.")]
ExpectedPathType,
#[error("Expected ':'. Enum variants must be in the form `Variant: ()`, `Variant: <type>`, or `Variant: (<type1>, ..., <typeN>)`. E.g., `Foo: (), or `Bar: (bool, u32)`.")]
MissingColonInEnumTypeField,
}

#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
Expand Down
10 changes: 7 additions & 3 deletions sway-parse/src/item/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{Parse, ParseResult, ParseToEnd, Parser, ParserConsumed};

use sway_ast::keywords::{
AbiToken, ClassToken, ConfigurableToken, ConstToken, EnumToken, FnToken, ImplToken, ModToken,
MutToken, OpenAngleBracketToken, RefToken, SelfToken, SemicolonToken, StorageToken,
AbiToken, ClassToken, ColonToken, ConfigurableToken, ConstToken, EnumToken, FnToken, ImplToken,
ModToken, MutToken, OpenAngleBracketToken, RefToken, SelfToken, SemicolonToken, StorageToken,
StructToken, TraitToken, TypeToken, UseToken, WhereToken,
};
use sway_ast::{
Expand Down Expand Up @@ -96,7 +96,11 @@ impl Parse for TypeField {
fn parse(parser: &mut Parser) -> ParseResult<TypeField> {
Ok(TypeField {
name: parser.parse()?,
colon_token: parser.parse()?,
colon_token: if parser.peek::<ColonToken>().is_some() {
parser.parse()
} else {
Err(parser.emit_error(ParseErrorKind::MissingColonInEnumTypeField))
}?,
ty: parser.parse()?,
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "core"
source = "path+from-root-EC2C1FC903667D61"

[[package]]
name = "enum_rust_like"
source = "member"
dependencies = ["core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "enum_rust_like"

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
script;

// A developer familiar with Rust might be tempted to define enum variants as `Foo` or `Bar(u32)`.
// This file tests the error message that informs the developer of the correct syntax.

enum Enum1 {
Ok, // Illegal
Err, // Also illegal, but shadowed by previous error
}

enum Enum2 {
F(u32), // Illegal
G(u32, u32), // Also illegal, but shadowed by previous error
}

fn main() {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
category = "fail"

# check: $()error
# check: enum_rust_like/src/main.sw:7:5
# check: $()Ok, // Illegal
# nextln: $()Expected ':'. Enum variants must be in the form `Variant: ()`, `Variant: <type>`, or `Variant: (<type1>, ..., <typeN>)`. E.g., `Foo: (), or `Bar: (bool, u32)`.

# check: $()error
# check: enum_rust_like/src/main.sw:12:4
# check: $()F(u32), // Illegal
# nextln: $()Expected ':'. Enum variants must be in the form `Variant: ()`, `Variant: <type>`, or `Variant: (<type1>, ..., <typeN>)`. E.g., `Foo: (), or `Bar: (bool, u32)`.
Loading