From 6bcd6b35cef9eb818e6ce8e4a7e677591e70e36d Mon Sep 17 00:00:00 2001 From: Kendall Condon Date: Mon, 28 Jul 2025 14:10:18 -0400 Subject: [PATCH] zig fmt: canonicalize stray backslashes in identifiers --- lib/std/zig/Ast/Render.zig | 3 +++ lib/std/zig/parser_test.zig | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/std/zig/Ast/Render.zig b/lib/std/zig/Ast/Render.zig index 195a29aed9..3f013053e2 100644 --- a/lib/std/zig/Ast/Render.zig +++ b/lib/std/zig/Ast/Render.zig @@ -3449,6 +3449,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 index 758423e6b9..8006ee0784 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -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" { try testError( \\test "" {inline}