Skip to content

Commit

Permalink
Parser: check for excess _Complex initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas committed Mar 3, 2024
1 parent 68d44a6 commit 0638a2f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3266,7 +3266,22 @@ fn complexInitializer(p: *Parser, init_ty: Type) Error!Result {
}
}

// Eat excess initializers
var extra_tok: ?TokenIndex = null;
while (p.eatToken(.comma)) |_| {
if (p.tok_ids[p.tok_i] == .r_brace) break;
extra_tok = p.tok_i;
const extra = try p.assignExpr();
if (extra.empty(p)) {
try p.errTok(.expected_expr, p.tok_i);
p.skipTo(.r_brace);
return error.ParsingFailed;
}
}
try p.expectClosing(l_brace, .r_brace);
if (extra_tok) |tok| {
try p.errTok(.excess_scalar_init, tok);
}

const arr_init_node: Tree.Node = .{
.tag = .array_init_expr_two,
Expand Down
6 changes: 6 additions & 0 deletions test/cases/complex values.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ _Static_assert(1.0 / (_Complex double){__builtin_inf(), __builtin_nan("")} == 0.
constexpr _Complex double product = (_Complex double){__builtin_inf(), __builtin_nan("") * 2.0};
_Static_assert(__builtin_isinf(__real__ product), "");
_Static_assert(__builtin_isnan(__imag__ product), "");
_Complex double a = (_Complex double) {1.0, 2.0,};
_Complex double b = (_Complex double) {1.0, 2.0,,,,,,};
_Complex double c = (_Complex double) {1.0, 2.0,3.0};


#define EXPECTED_ERRORS "complex values.c:24:17: error: static_assert expression is not an integral constant expression" \
"complex values.c:42:49: error: expected expression" \
"complex values.c:43:49: warning: excess elements in scalar initializer [-Wexcess-initializers]" \

0 comments on commit 0638a2f

Please sign in to comment.