diff --git a/std/zig/parse.zig b/std/zig/parse.zig index 5214a0575..96aec714a 100644 --- a/std/zig/parse.zig +++ b/std/zig/parse.zig @@ -903,8 +903,20 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { State.ParamDeclEnd => |ctx| { if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| { ctx.param_decl.var_args_token = ellipsis3; - stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable; - continue; + + switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) { + ExpectCommaOrEndResult.end_token => |t| { + if (t == null) { + stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable; + continue; + } + continue; + }, + ExpectCommaOrEndResult.parse_error => |e| { + try tree.errors.push(e); + return tree; + }, + } } try stack.append(State{ .ParamDeclComma = ctx.fn_proto }); diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index 10434c8aa..434969948 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -354,6 +354,15 @@ test "zig fmt: fn decl with trailing comma" { ); } +test "zig fmt: var_args with trailing comma" { + try testCanonical( + \\pub fn add( + \\ a: ..., + \\) void {} + \\ + ); +} + test "zig fmt: enum decl with no trailing comma" { try testTransform( \\const StrLitKind = enum {Normal, C};