authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-27 13:52:37+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-27 13:52:37+02:00
logaf9a9a13747b1e7007e29ff4f76e700f5bd7f7cf
tree8eafad5d7280bea42b2c09568d71c3b47bafd978
parent547e3684bea1fce9b14ffd40be18dcc88479d5a0

zig fmt: improve handling of comptime tuple fields


4 files changed, 21 insertions(+), 7 deletions(-)

doc/langref.html.in+5-3
......@@ -12094,7 +12094,9 @@ FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? AddrSpa
1209412094
1209512095VarDecl <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? AddrSpace? LinkSection? (EQUAL Expr)? SEMICOLON
1209612096
12097ContainerField <- doc_comment? KEYWORD_comptime? IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)?
12097ContainerField
12098 <- doc_comment? KEYWORD_comptime? IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)?
12099 / doc_comment? KEYWORD_comptime? (IDENTIFIER COLON)? !KEYWORD_fn TypeExpr ByteAlign? (EQUAL Expr)?
1209812100
1209912101# *** Block Level ***
1210012102Statement
......@@ -12479,8 +12481,8 @@ string_char
1247912481 <- char_escape
1248012482 / [^\\"\n]
1248112483
12482container_doc_comment <- ('//!' [^\n]* [ \n]*)+
12483doc_comment <- ('///' [^\n]* [ \n]*)+ skip
12484container_doc_comment <- ('//!' [^\n]* [ \n]* skip)+
12485doc_comment <- ('///' [^\n]* [ \n]* skip)+
1248412486line_comment <- '//' ![!/][^\n]* / '////' [^\n]*
1248512487line_string <- ("\\\\" [^\n]* [ \n]*)+
1248612488skip <- ([ \n] / line_comment)*
lib/std/zig/Ast.zig+1-2
......@@ -559,8 +559,7 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
559559 .container_field,
560560 => {
561561 const name_token = main_tokens[n];
562 if (token_tags[name_token + 1] != .colon) return name_token - end_offset;
563 if (name_token > 0 and token_tags[name_token - 1] == .keyword_comptime) {
562 if (token_tags[name_token] != .keyword_comptime and name_token > 0 and token_tags[name_token - 1] == .keyword_comptime) {
564563 end_offset += 1;
565564 }
566565 return name_token - end_offset;
lib/std/zig/parse.zig-1
......@@ -313,7 +313,6 @@ const Parser = struct {
313313 trailing = false;
314314 },
315315 else => {
316 p.tok_i += 1;
317316 const identifier = p.tok_i;
318317 defer last_field = identifier;
319318 const container_field = p.expectContainerField() catch |err| switch (err) {
lib/std/zig/parser_test.zig+15-1
......@@ -1,7 +1,9 @@
11test "zig fmt: tuple struct" {
22 try testCanonical(
33 \\const T = struct {
4 \\ comptime u32,
4 \\ /// doc comment on tuple field
5 \\ comptime comptime u32,
6 \\ /// another doc comment on tuple field
57 \\ *u32 = 1,
68 \\ // needs to be wrapped in parentheses to not be parsed as a function decl
79 \\ (fn () void) align(1),
......@@ -4238,6 +4240,18 @@ test "zig fmt: remove newlines surrounding doc comment within container decl" {
42384240 );
42394241}
42404242
4243test "zig fmt: comptime before comptime field" {
4244 try testError(
4245 \\const Foo = struct {
4246 \\ a: i32,
4247 \\ comptime comptime b: i32 = 1234,
4248 \\};
4249 \\
4250 , &[_]Error{
4251 .expected_comma_after_field,
4252 });
4253}
4254
42414255test "zig fmt: invalid else branch statement" {
42424256 try testError(
42434257 \\/// This is a doc comment for a comptime block.