From a61bf83a11839fbabf45f0c7f9aa80ec9c684ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Tue, 10 Feb 2026 12:28:14 +0000 Subject: [PATCH] parser: add block, break, grouped expression, array type tests Port tests from upstream parser_test.zig: - "allow empty line before comment at start of block" - "comptime struct field" - "break from block" - "grouped expressions (parentheses)" - "array types last token" Fix bugs in parser.c: - parseBreakLabel: use null_token instead of null_node - test decl: use null_token for unnamed tests Co-Authored-By: Claude Opus 4.6 (1M context) --- parser.c | 6 ++--- parser_test.zig | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/parser.c b/parser.c index bf476553dc..a1e9e8b8cf 100644 --- a/parser.c +++ b/parser.c @@ -982,8 +982,8 @@ static AstNodeIndex parseVarDeclProto(Parser* p) { } static AstTokenIndex parseBreakLabel(Parser* p) { - if (eatToken(p, TOKEN_COLON) == null_node) - return null_node; + if (eatToken(p, TOKEN_COLON) == null_token) + return null_token; return expectToken(p, TOKEN_IDENTIFIER); } @@ -1609,7 +1609,7 @@ static Members parseContainerMembers(Parser* p) { = (p->token_tags[p->tok_i] == TOKEN_STRING_LITERAL || p->token_tags[p->tok_i] == TOKEN_IDENTIFIER) ? nextToken(p) - : 0; + : null_token; const AstNodeIndex body = parseBlock(p); assert(body != 0); const AstNodeIndex test_decl = addNode(&p->nodes, diff --git a/parser_test.zig b/parser_test.zig index cfbc4946ad..56adc8ff50 100644 --- a/parser_test.zig +++ b/parser_test.zig @@ -873,3 +873,62 @@ test "zig fmt: remove empty lines at start/end of container decl" { \\ ); } + +test "zig fmt: allow empty line before comment at start of block" { + try testCanonical( + \\test { + \\ + \\ // foo + \\ const x = 42; + \\} + \\ + ); +} + +test "zig fmt: comptime struct field" { + try testCanonical( + \\const Foo = struct { + \\ a: i32, + \\ comptime b: i32 = 1234, + \\}; + \\ + ); +} + +test "zig fmt: break from block" { + try testCanonical( + \\const a = blk: { + \\ break :blk 42; + \\}; + \\const b = blk: { + \\ break :blk; + \\}; + \\const c = { + \\ break 42; + \\}; + \\const d = { + \\ break; + \\}; + \\ + ); +} + +test "zig fmt: grouped expressions (parentheses)" { + try testCanonical( + \\const r = (x + y) * (a + b); + \\ + ); +} + +test "zig fmt: array types last token" { + try testCanonical( + \\test { + \\ const x = [40]u32; + \\} + \\ + \\test { + \\ const x = [40:0]u32; + \\} + \\ + ); +}