Cleaner error message when missing comma in struct initializer #1941.

This commit is contained in:
Christoffer Lerno
2025-02-08 19:54:44 +01:00
parent d3f2180330
commit 940874e349
5 changed files with 30 additions and 1 deletions

View File

@@ -17,6 +17,7 @@
- Improve error message when using ',' in struct declarations. #1920
- Compile time array assign ops, e.g. `$c[1] += 3` #1890.
- Add `inline` to enums #1819.
- Cleaner error message when missing comma in struct initializer #1941.
### Fixes
- Fix issue requiring prefix on a generic interface declaration.

View File

@@ -138,6 +138,10 @@ inline Expr *parse_precedence_with_left_side(ParseContext *c, Expr *left_side, P
if (precedence > token_precedence) break;
// LHS may be poison.
if (!expr_ok(left_side)) return left_side;
// Special rule to prevent = {}.
if (tok == TOKEN_DOT && left_side->expr_kind == EXPR_INITIALIZER_LIST) break;
// See if there is a rule for infix.
ParseFn infix_rule = rules[tok].infix;
// Otherwise we ran into a symbol that can't appear in this position.
@@ -912,7 +916,11 @@ Expr *parse_initializer_list(ParseContext *c, Expr *left)
}
designated = 0;
}
CONSUME_OR_RET(TOKEN_RBRACE, poisoned_expr);
if (!try_consume(c, TOKEN_RBRACE))
{
PRINT_ERROR_HERE("A comma or '}' was expected here.");
return poisoned_expr;
}
RANGE_EXTEND_PREV(initializer_list);
if (designated == 1)
{

View File

@@ -2393,6 +2393,11 @@ static inline Decl *parse_enum_declaration(ParseContext *c)
// Allow trailing ','
if (!try_consume(c, TOKEN_COMMA))
{
if (tok_is(c, TOKEN_CONST_IDENT))
{
PRINT_ERROR_HERE("It looks like you forgot a comma before this identifier.");
return poisoned_decl;
}
EXPECT_OR_RET(TOKEN_RBRACE, poisoned_decl);
}
}

View File

@@ -0,0 +1,5 @@
enum Abc
{
FOO
BAR // #error: It looks like you forgot a comma
}

View File

@@ -0,0 +1,10 @@
module test;
struct Abc
{
int[2] a;
int b;
}
fn int main()
{
Abc a = { .a = {} .b = 3 }; // #error: A comma or '}' was expected here.
}