Skip to content

Commit

Permalink
allow structs/enums with zero fields (FuelLabs#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
canndrew authored Dec 8, 2021
1 parent be31bf1 commit 4fea960
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions core_lang/src/hll.pest
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ match_condition = {expr|"_"}
code_block = {"{" ~ (declaration|control_flow|expr_statement)* ~ (expr)? ~ "}"}

struct_expression = {struct_name ~ "{" ~ struct_expr_fields ~ "}"}
struct_expr_fields = {struct_field_name ~ ":" ~ expr ~ ("," ~ struct_field_name ~ ":" ~ expr)* ~ ","?}
struct_expr_fields = {(struct_field_name ~ ":" ~ expr ~ ("," ~ struct_field_name ~ ":" ~ expr)* ~ ","?)?}
array_exp = {"[" ~ array_elems? ~ "]"}
// Strictly speaking the [val; count] initialiser for a static array can have any constant expression
// for the value and the count, but Sway doesn't yet have constant expression resolution, so for now
Expand All @@ -127,11 +127,11 @@ storage_decl = {storage_keyword ~ "{" ~ storage_fields ~ "}"}
storage_fields = {storage_field ~ ("," ~ storage_field)* ~ ","?}
storage_field = {ident ~ ":" ~ type_name ~ assign ~ expr}
struct_name = {ident}
struct_fields = {struct_field_name ~ ":" ~ type_name ~ ("," ~ struct_field_name ~ ":" ~ type_name)* ~ ","?}
struct_fields = {(struct_field_name ~ ":" ~ type_name ~ ("," ~ struct_field_name ~ ":" ~ type_name)* ~ ","?)?}
struct_field_name = {ident}
// // enum declaration
enum_decl = {visibility ~ enum_keyword ~ enum_name ~ type_params? ~ trait_bounds? ~ "{" ~ enum_fields ~ "}"}
enum_fields = {enum_field_name ~ ":" ~ type_name ~ ("," ~ enum_field_name ~ ":" ~ type_name)* ~ ","?}
enum_fields = {(enum_field_name ~ ":" ~ type_name ~ ("," ~ enum_field_name ~ ":" ~ type_name)* ~ ","?)?}
enum_name = {ident}
enum_field_name = {ident}

Expand Down
1 change: 1 addition & 0 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub fn run(filter_regex: Option<regex::Regex>) {
("import_method_from_other_file", ProgramState::Return(10)), // true
("address_test", ProgramState::Return(1)), // true
("generic_struct", ProgramState::Return(1)), // true
("zero_field_types", ProgramState::Return(10)), // true
("assert_test", ProgramState::Return(1)), // true
("b512_test", ProgramState::Return(1)), // true
("assert_test", ProgramState::Return(1)), // true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
author = "Andrew Cann"
license = "MIT"
name = "zero_field_types"
entry = "main.sw"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
script;

struct StructWithNoFields {}
enum EnumWithNoVariants {}

fn main() -> u64 {
let unit_struct = StructWithNoFields {};
10u64
}

0 comments on commit 4fea960

Please sign in to comment.