Skip to content

Commit

Permalink
Enum associated declarations accidentally allowed declaration in func…
Browse files Browse the repository at this point in the history
…tion style. #1841
  • Loading branch information
lerno committed Jan 14, 2025
1 parent 3033295 commit 70da1f7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
- @tag on macros cannot be retrieved with tagof #1582
- Taking the $typeof of a wildcard optional returns `void!`.
- Fix bug with enums with jump tables #1840.
- Enum associated declarations accidentally allowed declaration in function style. #1841

### Stdlib changes
- Remove unintended print of `char[]` as String
Expand Down
20 changes: 17 additions & 3 deletions src/compiler/parse_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -2331,17 +2331,31 @@ static inline Decl *parse_enum_declaration(ParseContext *c)
if (!parse_attributes_for_global(c, enum_const)) return poisoned_decl;
if (try_consume(c, TOKEN_EQ))
{
Expr **args = NULL;
if (expected_parameters == 1 || !tok_is(c, TOKEN_LBRACE))
{
ASSIGN_EXPR_OR_RET(Expr *single, parse_expr(c), poisoned_decl);
vec_add(enum_const->enum_constant.args, single);
vec_add(args, single);
}
else
{
CONSUME_OR_RET(TOKEN_LBRACE, poisoned_decl);
if (!parse_arg_list(c, &enum_const->enum_constant.args, TOKEN_RBRACE, 0)) return poisoned_decl;
CONSUME_OR_RET(TOKEN_RBRACE, poisoned_decl);
while (1)
{
if (try_consume(c, TOKEN_RBRACE)) break;
ASSIGN_EXPR_OR_RET(Expr *arg, parse_expr(c), poisoned_decl);
vec_add(args, arg);
if (tok_is(c, TOKEN_COLON) && arg->expr_kind == EXPR_IDENTIFIER)

This comment has been minimized.

Copy link
@cbuttner

cbuttner Jan 15, 2025

Contributor

I think this part can be removed, {a: 1, b: 1} is not a valid construct anyway, it looks like mix between designated initializers and named call args.

{
print_error_at(extend_span_with_token(arg->span, c->span),
"This looks like a designated initializer, but that style of declaration "
"is not supported for declaring enum associated values.");
return poisoned_decl;
}
if (try_consume(c, TOKEN_COMMA)) continue;
}
}
enum_const->enum_constant.args = args;
}
vec_add(decl->enums.values, enum_const);
// Allow trailing ','
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum Test2 : (usz a, usz b) {
FOO = {4, 5, },
}

enum Test : (usz a, usz b) {
FOO = {a: 1, b: 1}, // #error: This looks like
}

0 comments on commit 70da1f7

Please sign in to comment.