Skip to content

Commit

Permalink
Fix bug in parsing inline enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Feb 3, 2025
1 parent 9d2f4e7 commit aaa5c0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
17 changes: 7 additions & 10 deletions src/compiler/parse_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -1235,14 +1235,8 @@ static inline Decl *parse_global_declaration(ParseContext *c)
/**
* enum_param_decl ::= type IDENT attributes?
*/
static inline bool parse_enum_param_decl(ParseContext *c, Decl*** parameters, bool *has_inline)
static inline bool parse_enum_param_decl(ParseContext *c, Decl*** parameters)
{
bool is_inline = try_consume(c, TOKEN_INLINE);
if (is_inline)
{
if (*has_inline) RETURN_PRINT_ERROR_HERE("An enum may only have one inline parameter.");
*has_inline = true;
}
ASSIGN_TYPE_OR_RET(TypeInfo *type, parse_optional_type(c), false);
if (type->optional) RETURN_PRINT_ERROR_AT(false, type, "Parameters may not be optional.");
Decl *param = decl_new_var_current(c, type, VARDECL_PARAM);
Expand Down Expand Up @@ -2282,15 +2276,18 @@ static inline bool parse_enum_param_list(ParseContext *c, Decl*** parameters_ref
if (!try_consume(c, TOKEN_LPAREN)) return true;

ArrayIndex index = -1;
bool has_inline = !inline_index;
while (!try_consume(c, TOKEN_RPAREN))
{
index++;
bool has_inline = !inline_index || *inline_index > -1;
if (!parse_enum_param_decl(c, parameters_ref, &has_inline)) return false;
if (has_inline)
bool is_inline = try_consume(c, TOKEN_INLINE);
if (is_inline)
{
if (has_inline) RETURN_PRINT_ERROR_HERE("An enum cannot combine an inline value and a inline parameter.");
if (*inline_index > -1) RETURN_PRINT_ERROR_HERE("An enum may only have one inline parameter.");
*inline_index = index;
}
if (!parse_enum_param_decl(c, parameters_ref)) return false;
Decl *last_parameter = VECLAST(*parameters_ref);
ASSERT(last_parameter);
last_parameter->var.index = vec_size(*parameters_ref) - 1; // NOLINT
Expand Down
6 changes: 3 additions & 3 deletions test/test_suite/enumerations/inline_enums.c3t
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ enum Abc : inline int
DEF,
}

enum Foo : int (inline String name)
enum Foo : int (inline String name, int y, int z)
{
ABC = "Hello",
DEF = "World"
ABC = { "Hello", 1, 2 },
DEF = { "World", 2, 3 },
}
fn void main()
{
Expand Down

0 comments on commit aaa5c0f

Please sign in to comment.