diff --git a/lib/std/zig/Ast/Render.zig b/lib/std/zig/Ast/Render.zig index 665338072a3e9f38023e814b8fddda19730d716d..78b955c8cef9b0d74f37f301f633deb29e03ec2d 100644 --- 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 index 14cef741283d47749779e9b70fb03b6e8732a31a..09f4c0f8db129480f2f3a87439533ea39075e9b5 100644 --- 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 = .{