zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit d5d3e40b6c262875d766946d0edc02b8cd14ee98 (tree)
parent fcc54ca981d30af0a187c9d187b2a63ce0195f3c
Author: Kendall Condon <goon.pri.low@gmail.com>
Date:   Mon, 28 Jul 2025 13:32:29 -0400

zig fmt: preserve space for field accesses of number literals

Diffstat:
Mlib/std/zig/Ast/Render.zig | 13+++++++++----
Mlib/std/zig/parser_test.zig | 9+++++++++
2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/lib/std/zig/Ast/Render.zig b/lib/std/zig/Ast/Render.zig @@ -441,13 +441,18 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void { const dot_token = name_token - 1; try ais.pushIndent(.field_access); - try renderExpression(r, lhs, .none); - // Allow a line break between the lhs and the dot if the lhs and rhs - // are on different lines. const lhs_last_token = tree.lastToken(lhs); const same_line = tree.tokensOnSameLine(lhs_last_token, name_token); - if (!same_line and !hasComment(tree, lhs_last_token, dot_token)) try ais.insertNewline(); + // Keeping a space after the number ensures it will not turn into a decimal number + // (e.g. "0xF .A"). + const number_space: Space = if (tree.tokenTag(lhs_last_token) == .number_literal and same_line) .space else .none; + try renderExpression(r, lhs, number_space); + + // Allow a line break between the lhs and the dot if the lhs and rhs + // are on different lines. + if (!same_line and !hasComment(tree, lhs_last_token, dot_token)) + try ais.insertNewline(); try renderToken(r, dot_token, .none); diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig @@ -6054,6 +6054,15 @@ test "zig fmt: extern addrspace in struct" { ); } +test "zig fmt: field accesses on number literals" { + try testCanonical( + \\const a = 0xF .A; + \\const a = 0xF + \\ .A; + \\ + ); +} + test "zig fmt: whitespace with multiline strings" { try testCanonical( \\const a = .{