commit 5d4e50075a7e0c32c27d1bc1d89b6ba69c8bb060 (tree)
parent 26725d687abfa750b9c0db5554c03156c84c02a2
Author: Motiejus Jakštys <motiejus@jakstys.lt>
Date: Tue, 10 Feb 2026 18:10:11 +0000
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) <noreply@anthropic.com>
Diffstat:
| M | parser.c | | | 4 | ++-- |
| M | parser_test.zig | | | 94 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 96 insertions(+), 2 deletions(-)
diff --git 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
@@ -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{};