parser: port full "while" and "for" tests

Port tests:
- "while" (full test with all variants)
- "for" (full test with all variants including testTransform)

Fix in parser.c:
- comptime var decl: don't wrap in comptime node (renderer
  detects comptime from token positions)
- forPrefix: handle trailing comma in input list and capture list

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-02-10 22:57:15 +00:00
parent 571fb20bb7
commit fdaeca84fe
2 changed files with 205 additions and 2 deletions

View File

@@ -3778,6 +3778,194 @@ test "zig fmt: switch" {
);
}
test "zig fmt: while" {
try testCanonical(
\\test "while" {
\\ while (10 < 1) unreachable;
\\
\\ while (10 < 1) unreachable else unreachable;
\\
\\ while (10 < 1) {
\\ unreachable;
\\ }
\\
\\ while (10 < 1)
\\ unreachable;
\\
\\ var i: usize = 0;
\\ while (i < 10) : (i += 1) {
\\ continue;
\\ }
\\
\\ i = 0;
\\ while (i < 10) : (i += 1)
\\ continue;
\\
\\ i = 0;
\\ var j: usize = 0;
\\ while (i < 10) : ({
\\ i += 1;
\\ j += 1;
\\ }) continue;
\\
\\ while (i < 10) : ({
\\ i += 1;
\\ j += 1;
\\ }) {
\\ continue;
\\ }
\\
\\ var a: ?u8 = 2;
\\ while (a) |v| : (a = null) {
\\ continue;
\\ }
\\
\\ while (a) |v| : (a = null)
\\ continue;
\\
\\ while (a) |v| : (a = null)
\\ continue
\\ else
\\ unreachable;
\\
\\ for (&[_]u8{}) |v| {
\\ continue;
\\ }
\\
\\ while (a) |v| : (a = null)
\\ unreachable;
\\
\\ label: while (10 < 0) {
\\ unreachable;
\\ }
\\
\\ const res = while (0 < 10) {
\\ break 7;
\\ } else {
\\ unreachable;
\\ };
\\
\\ const res = while (0 < 10)
\\ break 7
\\ else
\\ unreachable;
\\
\\ var a: anyerror!u8 = 0;
\\ while (a) |v| {
\\ a = error.Err;
\\ } else |err| {
\\ i = 1;
\\ }
\\
\\ comptime var k: usize = 0;
\\ inline while (i < 10) : (i += 1)
\\ j += 2;
\\}
\\
);
}
test "zig fmt: for" {
try testCanonical(
\\test "for" {
\\ for (a) |v| {
\\ continue;
\\ }
\\
\\ for (a) |v| continue;
\\
\\ for (a) |v| continue else return;
\\
\\ for (a) |v| {
\\ continue;
\\ } else return;
\\
\\ for (a) |v| continue else {
\\ return;
\\ }
\\
\\ for (a) |v|
\\ continue
\\ else
\\ return;
\\
\\ for (a) |v|
\\ continue;
\\
\\ for (a) |*v|
\\ continue;
\\
\\ for (a, 0..) |v, i| {
\\ continue;
\\ }
\\
\\ for (a, 0..) |v, i|
\\ continue;
\\
\\ for (a) |b| switch (b) {
\\ c => {},
\\ d => {},
\\ };
\\
\\ const res = for (a, 0..) |v, i| {
\\ break v;
\\ } else {
\\ unreachable;
\\ };
\\
\\ var num: usize = 0;
\\ inline for (a, 0..1) |v, i| {
\\ num += v;
\\ num += i;
\\ }
\\
\\ for (a, b) |
\\ long_name,
\\ another_long_name,
\\ | {
\\ continue;
\\ }
\\}
\\
);
try testTransform(
\\test "fix for" {
\\ for (a) |x|
\\ f(x) else continue;
\\}
\\
,
\\test "fix for" {
\\ for (a) |x|
\\ f(x)
\\ else
\\ continue;
\\}
\\
);
try testTransform(
\\test "fix for" {
\\ for (a, b, c,) |long, another, third,| {}
\\}
\\
,
\\test "fix for" {
\\ for (
\\ a,
\\ b,
\\ c,
\\ ) |
\\ long,
\\ another,
\\ third,
\\ | {}
\\}
\\
);
}
test "zig fmt: for if" {
try testCanonical(
\\test {