zig fmt: canonicalize stray backslashes in identifiers

This commit is contained in:
Kendall Condon 2025-07-28 14:10:18 -04:00
parent 4b73d5e029
commit 6bcd6b35ce
2 changed files with 21 additions and 0 deletions

View file

@ -3449,6 +3449,9 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void
} }
}, },
.failure => { .failure => {
// Escape the stray backslash
// This also avoids cases like "\x3\x39" becoming "\x39"
try ais.writeByte('\\');
try ais.writeAll(escape_sequence); try ais.writeAll(escape_sequence);
}, },
} }

View file

@ -6732,6 +6732,24 @@ test "zig fmt: doc comments on fn parameters" {
); );
} }
test "zig fmt: canonicalize stray backslashes in identifiers" {
try testTransform(
\\const @"\x" = undefined;
\\const @"\x3" = undefined;
\\const @"\x3\x39" = undefined;
\\const @"\u{" = undefined;
\\const @"\?" = undefined;
\\
,
\\const @"\\x" = undefined;
\\const @"\\x3" = undefined;
\\const @"\\x39" = undefined;
\\const @"\\u{" = undefined;
\\const @"\\?" = undefined;
\\
);
}
test "recovery: top level" { test "recovery: top level" {
try testError( try testError(
\\test "" {inline} \\test "" {inline}