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...@@ -12094,7 +12094,9 @@ FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? AddrSpa
1209412094
12095VarDecl <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? AddrSpace? LinkSection? (EQUAL Expr)? SEMICOLON12095VarDecl <- (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
12099# *** Block Level ***12101# *** Block Level ***
12100Statement12102Statement
...@@ -12479,8 +12481,8 @@ string_char...@@ -12479,8 +12481,8 @@ string_char
12479 <- char_escape12481 <- char_escape
12480 / [^\\"\n]12482 / [^\\"\n]
1248112483
12482container_doc_comment <- ('//!' [^\n]* [ \n]*)+12484container_doc_comment <- ('//!' [^\n]* [ \n]* skip)+
12483doc_comment <- ('///' [^\n]* [ \n]*)+ skip12485doc_comment <- ('///' [^\n]* [ \n]* skip)+
12484line_comment <- '//' ![!/][^\n]* / '////' [^\n]*12486line_comment <- '//' ![!/][^\n]* / '////' [^\n]*
12485line_string <- ("\\\\" [^\n]* [ \n]*)+12487line_string <- ("\\\\" [^\n]* [ \n]*)+
12486skip <- ([ \n] / line_comment)*12488skip <- ([ \n] / line_comment)*
lib/std/zig/Ast.zig+1-2
...@@ -559,8 +559,7 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {...@@ -559,8 +559,7 @@ pub fn firstToken(tree: Ast, node: Node.Index) TokenIndex {
559 .container_field,559 .container_field,
560 => {560 => {
561 const name_token = main_tokens[n];561 const name_token = main_tokens[n];
562 if (token_tags[name_token + 1] != .colon) return name_token - end_offset;562 if (token_tags[name_token] != .keyword_comptime and name_token > 0 and token_tags[name_token - 1] == .keyword_comptime) {
563 if (name_token > 0 and token_tags[name_token - 1] == .keyword_comptime) {
564 end_offset += 1;563 end_offset += 1;
565 }564 }
566 return name_token - end_offset;565 return name_token - end_offset;
lib/std/zig/parse.zig-1
...@@ -313,7 +313,6 @@ const Parser = struct {...@@ -313,7 +313,6 @@ const Parser = struct {
313 trailing = false;313 trailing = false;
314 },314 },
315 else => {315 else => {
316 p.tok_i += 1;
317 const identifier = p.tok_i;316 const identifier = p.tok_i;
318 defer last_field = identifier;317 defer last_field = identifier;
319 const container_field = p.expectContainerField() catch |err| switch (err) {318 const container_field = p.expectContainerField() catch |err| switch (err) {
lib/std/zig/parser_test.zig+15-1
...@@ -1,7 +1,9 @@...@@ -1,7 +1,9 @@
1test "zig fmt: tuple struct" {1test "zig fmt: tuple struct" {
2 try testCanonical(2 try testCanonical(
3 \\const T = struct {3 \\const T = struct {
4 \\ comptime u32,4 \\ /// doc comment on tuple field
5 \\ comptime comptime u32,
6 \\ /// another doc comment on tuple field
5 \\ *u32 = 1,7 \\ *u32 = 1,
6 \\ // needs to be wrapped in parentheses to not be parsed as a function decl8 \\ // needs to be wrapped in parentheses to not be parsed as a function decl
7 \\ (fn () void) align(1),9 \\ (fn () void) align(1),
...@@ -4238,6 +4240,18 @@ test "zig fmt: remove newlines surrounding doc comment within container decl" {...@@ -4238,6 +4240,18 @@ test "zig fmt: remove newlines surrounding doc comment within container decl" {
4238 );4240 );
4239}4241}
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
4241test "zig fmt: invalid else branch statement" {4255test "zig fmt: invalid else branch statement" {
4242 try testError(4256 try testError(
4243 \\/// This is a doc comment for a comptime block.4257 \\/// This is a doc comment for a comptime block.