From 5d4e50075a7e0c32c27d1bc1d89b6ba69c8bb060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Tue, 10 Feb 2026 18:10:11 +0000 Subject: [PATCH] parser: port zig fmt on/off, defer, block slice tests Port tests: - "line and doc comment following 'zig fmt: off'" - "doc and line comment following 'zig fmt: off'" - "line comment following 'zig fmt: on'" - "doc comment following 'zig fmt: on'" - "line and doc comment following 'zig fmt: on'" - "doc and line comment following 'zig fmt: on'" - "block in slice expression" - "defer" Fix defer node data: body goes in lhs (not rhs) to match .node union variant. Co-Authored-By: Claude Opus 4.6 (1M context) --- parser.c | 4 +-- parser_test.zig | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/parser.c b/parser.c index 102ecdaa2a..c118700a4b 100644 --- a/parser.c +++ b/parser.c @@ -2086,8 +2086,8 @@ static AstNodeIndex expectStatement(Parser* p, bool allow_defer_var) { .tag = AST_NODE_DEFER, .main_token = nextToken(p), .data = { - .lhs = 0, - .rhs = expectBlockExprStatement(p), + .lhs = expectBlockExprStatement(p), + .rhs = 0, }, }); case TOKEN_KEYWORD_ERRDEFER: { diff --git a/parser_test.zig b/parser_test.zig index c3091f0d36..1aa2018a9c 100644 --- a/parser_test.zig +++ b/parser_test.zig @@ -2145,6 +2145,100 @@ test "zig fmt: anytype type" { ); } +test "zig fmt: line and doc comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\// test 1 + \\/// test 2 + \\const e = f; + ); +} + +test "zig fmt: doc and line comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\/// test 1 + \\// test 2 + \\const e = f; + ); +} + +test "zig fmt: line comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\// test + \\const e = f; + \\ + ); +} + +test "zig fmt: doc comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\/// test + \\const e = f; + \\ + ); +} + +test "zig fmt: line and doc comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\// test1 + \\/// test2 + \\const e = f; + \\ + ); +} + +test "zig fmt: doc and line comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\/// test1 + \\// test2 + \\const e = f; + \\ + ); +} + +test "zig fmt: block in slice expression" { + try testCanonical( + \\const a = b[{ + \\ _ = x; + \\}..]; + \\const c = d[0..{ + \\ _ = x; + \\ _ = y; + \\}]; + \\const e = f[0..1 :{ + \\ _ = x; + \\ _ = y; + \\ _ = z; + \\}]; + \\ + ); +} + +test "zig fmt: defer" { + try testCanonical( + \\test "defer" { + \\ defer foo(); + \\ defer { + \\ bar(); + \\ } + \\} + \\ + ); +} + test "zig fmt: container initializers" { try testCanonical( \\const a0 = []u8{};