commit 409541bc77cb1d6a447dbcdf74f9fcbc2a8b0397 (tree)
parent bbc2e7a74da98cfcf7bd24f1904cc77a203002f8
Author: Kendall Condon <goon.pri.low@gmail.com>
Date: Mon, 28 Jul 2025 14:10:18 -0400
zig fmt: canonicalize stray backslashes in identifiers
Diffstat:
2 files changed, 21 insertions(+), 0 deletions(-)
diff --git a/lib/std/zig/Ast/Render.zig b/lib/std/zig/Ast/Render.zig
@@ -3257,6 +3257,9 @@ fn renderIdentifierContents(ais: *AutoIndentingStream, bytes: []const u8) !void
}
},
.failure => {
+ // Escape the stray backslash
+ // This also avoids cases like "\x3\x39" becoming "\x39"
+ try ais.writeByte('\\');
try ais.writeAll(escape_sequence);
},
}
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
@@ -6666,6 +6666,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" {
try testError(
\\test "" {inline}