Skip to content

Commit 5d94e75

Browse files
g-w1Vexu
authored andcommitted
fmt: fix #8974
also add a regression test
1 parent 83e0a49 commit 5d94e75

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

lib/std/zig/parser_test.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4409,6 +4409,13 @@ test "zig fmt: regression test for #5722" {
44094409
);
44104410
}
44114411

4412+
test "zig fmt: regression test for #8974" {
4413+
try testCanonical(
4414+
\\pub const VARIABLE;
4415+
\\
4416+
);
4417+
}
4418+
44124419
test "zig fmt: allow trailing line comments to do manual array formatting" {
44134420
try testCanonical(
44144421
\\fn foo() void {

lib/std/zig/render.zig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -989,16 +989,18 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: ast.Tree, var_decl: ast.full.
989989
}
990990
}
991991

992-
assert(var_decl.ast.init_node != 0);
993-
const eq_token = tree.firstToken(var_decl.ast.init_node) - 1;
994-
const eq_space: Space = if (tree.tokensOnSameLine(eq_token, eq_token + 1)) .space else .newline;
995-
{
996-
ais.pushIndent();
997-
try renderToken(ais, tree, eq_token, eq_space); // =
998-
ais.popIndent();
992+
if (var_decl.ast.init_node != 0) {
993+
const eq_token = tree.firstToken(var_decl.ast.init_node) - 1;
994+
const eq_space: Space = if (tree.tokensOnSameLine(eq_token, eq_token + 1)) .space else .newline;
995+
{
996+
ais.pushIndent();
997+
try renderToken(ais, tree, eq_token, eq_space); // =
998+
ais.popIndent();
999+
}
1000+
ais.pushIndentOneShot();
1001+
return renderExpression(gpa, ais, tree, var_decl.ast.init_node, .semicolon); // ;
9991002
}
1000-
ais.pushIndentOneShot();
1001-
try renderExpression(gpa, ais, tree, var_decl.ast.init_node, .semicolon);
1003+
return renderToken(ais, tree, var_decl.ast.mut_token + 2, .newline); // ;
10021004
}
10031005

10041006
fn renderIf(gpa: *Allocator, ais: *Ais, tree: ast.Tree, if_node: ast.full.If, space: Space) Error!void {

0 commit comments

Comments
 (0)