From 5d94e754f4159277a2cef98d0c4226bab0e697a3 Mon Sep 17 00:00:00 2001 From: Jacob G-W Date: Thu, 3 Jun 2021 22:30:31 -0400 Subject: [PATCH] fmt: fix #8974 also add a regression test --- lib/std/zig/parser_test.zig | 7 +++++++ lib/std/zig/render.zig | 20 +++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 5a6629571ad6..9e07810d9c80 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -4409,6 +4409,13 @@ test "zig fmt: regression test for #5722" { ); } +test "zig fmt: regression test for #8974" { + try testCanonical( + \\pub const VARIABLE; + \\ + ); +} + test "zig fmt: allow trailing line comments to do manual array formatting" { try testCanonical( \\fn foo() void { diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 10abbdace9ad..ecf0fa623fa9 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -989,16 +989,18 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: ast.Tree, var_decl: ast.full. } } - assert(var_decl.ast.init_node != 0); - const eq_token = tree.firstToken(var_decl.ast.init_node) - 1; - const eq_space: Space = if (tree.tokensOnSameLine(eq_token, eq_token + 1)) .space else .newline; - { - ais.pushIndent(); - try renderToken(ais, tree, eq_token, eq_space); // = - ais.popIndent(); + if (var_decl.ast.init_node != 0) { + const eq_token = tree.firstToken(var_decl.ast.init_node) - 1; + const eq_space: Space = if (tree.tokensOnSameLine(eq_token, eq_token + 1)) .space else .newline; + { + ais.pushIndent(); + try renderToken(ais, tree, eq_token, eq_space); // = + ais.popIndent(); + } + ais.pushIndentOneShot(); + return renderExpression(gpa, ais, tree, var_decl.ast.init_node, .semicolon); // ; } - ais.pushIndentOneShot(); - try renderExpression(gpa, ais, tree, var_decl.ast.init_node, .semicolon); + return renderToken(ais, tree, var_decl.ast.mut_token + 2, .newline); // ; } fn renderIf(gpa: *Allocator, ais: *Ais, tree: ast.Tree, if_node: ast.full.If, space: Space) Error!void {