From c67c54c3fbfd41c6fd14205e2d8d4784dfb1c3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Tue, 10 Feb 2026 13:08:47 +0000 Subject: [PATCH] parser: add zig fmt on/off and slice operator tests Port tests from upstream parser_test.zig: - "comment to disable/enable zig fmt" - "line comment following 'zig fmt: off'" - "doc comment following 'zig fmt: off'" - "alternating 'zig fmt: off' and 'zig fmt: on'" - "spaces around slice operator" Fix parsePrimaryTypeExpr: don't reject identifier followed by colon; the colon may be part of slice sentinel syntax, not a label. Co-Authored-By: Claude Opus 4.6 (1M context) --- parser.c | 6 ----- parser_test.zig | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/parser.c b/parser.c index c29c79f1db..e4938c98ff 100644 --- a/parser.c +++ b/parser.c @@ -509,12 +509,6 @@ static AstNodeIndex parsePrimaryTypeExpr(Parser* p) { }); } case TOKEN_IDENTIFIER: - if (p->token_tags[p->tok_i + 1] == TOKEN_COLON) { - fprintf(stderr, - "parsePrimaryTypeExpr does not support identifier followed by " - "colon\n"); - exit(1); - } return addNode(&p->nodes, (AstNodeItem) { .tag = AST_NODE_IDENTIFIER, diff --git a/parser_test.zig b/parser_test.zig index 87c3b5ac20..aa44751a27 100644 --- a/parser_test.zig +++ b/parser_test.zig @@ -1432,3 +1432,67 @@ test "zig fmt: builtin call with trailing comma" { \\ ); } + +test "zig fmt: comment to disable/enable zig fmt" { + try testTransform( + \\const a = b; + \\// zig fmt: off + \\const c = d; + \\// zig fmt: on + \\const e = f; + , + \\const a = b; + \\// zig fmt: off + \\const c = d; + \\// zig fmt: on + \\const e = f; + \\ + ); +} + +test "zig fmt: line comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\// Test + \\const e = f; + ); +} + +test "zig fmt: doc comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\/// test + \\const e = f; + ); +} + +test "zig fmt: alternating 'zig fmt: off' and 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\// zig fmt: on + \\// zig fmt: off + \\const e = f; + \\// zig fmt: off + \\// zig fmt: on + \\// zig fmt: off + \\const a = b; + \\// zig fmt: on + \\const c = d; + \\// zig fmt: on + \\ + ); +} + +test "zig fmt: spaces around slice operator" { + try testCanonical( + \\var a = b[c..d]; + \\var a = b[c..d :0]; + \\var a = b[c + 1 .. d]; + \\var a = b[c + 1 ..]; + \\var a = b[c .. d + 1]; + \\var a = b[c .. d + 1 :0]; + \\var a = b[c.a..d.e]; + \\var a = b[c.a..d.e :0]; + \\ + ); +}