commit 6332e1d21772584644998a9717c2bcf73bab59b4 (tree)
parent 8838daa358391a7507a98e7818dd06492cc474f2
Author: Motiejus Jakštys <motiejus.jakstys@chronosphere.io>
Date: Tue, 10 Feb 2026 18:04:29 +0000
parser: port fn param and container initializer tests
Port tests:
- "doc comments on param decl"
- "pointer of unknown length"
- "call expression"
- "anytype type"
- "container initializers"
Handle anytype keyword in function parameter lists.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Diffstat:
| M | parser.c | | | 12 | +++++++++++- |
| M | parser_test.zig | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 78 insertions(+), 1 deletion(-)
diff --git a/parser.c b/parser.c
@@ -1113,7 +1113,7 @@ static SmallSpan parseParamDeclList(Parser* p) {
&& p->token_tags[p->tok_i + 1] == TOKEN_COLON) {
p->tok_i += 2; // consume name and colon
} else if (p->token_tags[p->tok_i] == TOKEN_ELLIPSIS3) {
- // anytype (...) varargs
+ // varargs (...)
p->tok_i++;
if (eatToken(p, TOKEN_R_PAREN) != null_token)
break;
@@ -1121,6 +1121,16 @@ static SmallSpan parseParamDeclList(Parser* p) {
continue;
}
+ // anytype params are omitted from the AST
+ if (eatToken(p, TOKEN_KEYWORD_ANYTYPE) != null_token) {
+ if (p->token_tags[p->tok_i] == TOKEN_COMMA) {
+ p->tok_i++;
+ continue;
+ }
+ expectToken(p, TOKEN_R_PAREN);
+ break;
+ }
+
const AstNodeIndex type_expr = parseTypeExpr(p);
if (type_expr != 0)
SLICE_APPEND(AstNodeIndex, &p->scratch, type_expr);
diff --git a/parser_test.zig b/parser_test.zig
@@ -2098,6 +2098,73 @@ test "zig fmt: comment after params" {
);
}
+test "zig fmt: doc comments on param decl" {
+ try testCanonical(
+ \\pub const Allocator = struct {
+ \\ shrinkFn: fn (
+ \\ self: Allocator,
+ \\ /// Guaranteed to be the same as what was returned from most recent call to
+ \\ /// `allocFn`, `reallocFn`, or `shrinkFn`.
+ \\ old_mem: []u8,
+ \\ /// Guaranteed to be the same as what was returned from most recent call to
+ \\ /// `allocFn`, `reallocFn`, or `shrinkFn`.
+ \\ old_alignment: u29,
+ \\ /// Guaranteed to be less than or equal to `old_mem.len`.
+ \\ new_byte_count: usize,
+ \\ /// Guaranteed to be less than or equal to `old_alignment`.
+ \\ new_alignment: u29,
+ \\ ) []u8,
+ \\};
+ \\
+ );
+}
+
+test "zig fmt: pointer of unknown length" {
+ try testCanonical(
+ \\fn foo(ptr: [*]u8) void {}
+ \\
+ );
+}
+
+test "zig fmt: call expression" {
+ try testCanonical(
+ \\test "test calls" {
+ \\ a();
+ \\ a(1);
+ \\ a(1, 2);
+ \\ a(1, 2) + a(1, 2);
+ \\}
+ \\
+ );
+}
+
+test "zig fmt: anytype type" {
+ try testCanonical(
+ \\fn print(args: anytype) @This() {}
+ \\
+ );
+}
+
+test "zig fmt: container initializers" {
+ try testCanonical(
+ \\const a0 = []u8{};
+ \\const a1 = []u8{1};
+ \\const a2 = []u8{
+ \\ 1,
+ \\ 2,
+ \\ 3,
+ \\ 4,
+ \\};
+ \\const s0 = S{};
+ \\const s1 = S{ .a = 1 };
+ \\const s2 = S{
+ \\ .a = 1,
+ \\ .b = 2,
+ \\};
+ \\
+ );
+}
+
test "zig fmt: sentinel-terminated array type" {
try testCanonical(
\\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 {