zig fmt: fix asm_legacy conversion with quoted identifier

renderSpace takes the length of the original lexeme, so passing the
length of the new lexeme is not correct.
This commit is contained in:
Kendall Condon 2025-07-28 14:23:11 -04:00
parent 15d830fd15
commit a55e720d17
2 changed files with 15 additions and 3 deletions

View file

@ -3673,7 +3673,7 @@ fn tokenSliceForRender(tree: Ast, token_index: Ast.TokenIndex) []const u8 {
return ret; return ret;
} }
fn writeStringLiteralAsIdentifier(r: *Render, token_index: Ast.TokenIndex) !usize { fn writeStringLiteralAsIdentifier(r: *Render, token_index: Ast.TokenIndex) Error!usize {
const tree = r.tree; const tree = r.tree;
const ais = r.ais; const ais = r.ais;
assert(tree.tokenTag(token_index) == .string_literal); assert(tree.tokenTag(token_index) == .string_literal);
@ -3681,12 +3681,11 @@ fn writeStringLiteralAsIdentifier(r: *Render, token_index: Ast.TokenIndex) !usiz
const unquoted = lexeme[1..][0 .. lexeme.len - 2]; const unquoted = lexeme[1..][0 .. lexeme.len - 2];
if (std.zig.isValidId(unquoted)) { if (std.zig.isValidId(unquoted)) {
try ais.writeAll(unquoted); try ais.writeAll(unquoted);
return unquoted.len;
} else { } else {
try ais.writeByte('@'); try ais.writeByte('@');
try ais.writeAll(lexeme); try ais.writeAll(lexeme);
return lexeme.len + 1;
} }
return lexeme.len;
} }
fn hasSameLineComment(tree: Ast, token_index: Ast.TokenIndex) bool { fn hasSameLineComment(tree: Ast, token_index: Ast.TokenIndex) bool {

View file

@ -6777,6 +6777,19 @@ test "zig fmt: canonicalize stray backslashes in identifiers" {
); );
} }
test "zig fmt: asm_legacy conversion with quoted identifier" {
try testTransform(
\\const a = asm (x :: [L] "" (q) : "");
,
\\const a = asm (x
\\ :
\\ : [L] "" (q),
\\ : .{ .@"" = true }
\\);
\\
);
}
test "recovery: top level" { test "recovery: top level" {
try testError( try testError(
\\test "" {inline} \\test "" {inline}