parser: port canonicalize and cast builtin tests

Port tests:
- "canonicalize symbols (simple)"
- "canonicalize symbols (character escapes)"
- "canonicalize symbols (asm)"
- "canonicalize cast builtins"
- "do not canonicalize invalid cast builtins"

Update test bodies to match upstream:
- "comptime block in container"
- "comment after empty comment"
- "comment after params"
- "decimal float literals with underscore separators"
- "container doc comments"

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-02-11 09:25:10 +00:00
parent 801dfc6c6e
commit 174a275c76

View File

@@ -5184,6 +5184,268 @@ test "zig fmt: test indentation of if expressions" {
);
}
test "zig fmt: canonicalize symbols (simple)" {
try testTransform(
\\const val_normal: Normal = .{};
\\const @"val_unesc_me": @"UnescMe" = .{};
\\const @"val_esc!": @"Esc!" = .{};
\\
\\fn fnNormal() void {}
\\fn @"fnUnescMe"() void {}
\\fn @"fnEsc!"() void {}
\\
\\extern fn protoNormal() void;
\\extern fn @"protoUnescMe"() void;
\\extern fn @"protoEsc!"() void;
\\
\\fn fnWithArgs(normal: Normal, @"unesc_me": @"UnescMe", @"esc!": @"Esc!") void {
\\ _ = normal;
\\ _ = @"unesc_me";
\\ _ = @"esc!";
\\}
\\
\\const Normal = struct {};
\\const @"UnescMe" = struct {
\\ @"x": @"X",
\\ const X = union(@"EnumUnesc") {
\\ normal,
\\ @"unesc_me",
\\ @"esc!",
\\ };
\\ const @"EnumUnesc" = enum {
\\ normal,
\\ @"unesc_me",
\\ @"esc!",
\\ };
\\};
\\const @"Esc!" = struct {
\\ normal: bool = false,
\\ @"unesc_me": bool = false,
\\ @"esc!": bool = false,
\\};
\\
\\pub fn main() void {
\\ _ = val_normal;
\\ _ = @"val_normal";
\\ _ = val_unesc_me;
\\ _ = @"val_unesc_me";
\\ _ = @"val_esc!";
\\
\\ fnNormal();
\\ @"fnNormal"();
\\ fnUnescMe();
\\ @"fnUnescMe"();
\\ @"fnEsc!"();
\\
\\ fnWithArgs(1, Normal{}, UnescMe{}, @"Esc!"{});
\\ fnWithArgs(1, @"Normal"{}, @"UnescMe"{}, @"Esc!"{});
\\ fnWithArgs(1, @"Normal"{}, @"Normal"{}, @"Esc!"{});
\\
\\ const local_val1: @"Normal" = .{};
\\ const @"local_val2": UnescMe = .{
\\ .@"x" = .@"unesc_me",
\\ };
\\ fnWithArgs(@"local_val1", @"local_val2", .{ .@"normal" = true, .@"unesc_me" = true, .@"esc!" = true });
\\ fnWithArgs(local_val1, local_val2, .{ .normal = true, .unesc_me = true, .@"esc!" = true });
\\
\\ var x: u8 = 'x';
\\ switch (@"x") {
\\ @"x" => {},
\\ }
\\
\\ _ = @import("std"); // Don't mess with @builtins
\\ // @"comment"
\\}
\\
,
\\const val_normal: Normal = .{};
\\const val_unesc_me: UnescMe = .{};
\\const @"val_esc!": @"Esc!" = .{};
\\
\\fn fnNormal() void {}
\\fn fnUnescMe() void {}
\\fn @"fnEsc!"() void {}
\\
\\extern fn protoNormal() void;
\\extern fn protoUnescMe() void;
\\extern fn @"protoEsc!"() void;
\\
\\fn fnWithArgs(normal: Normal, unesc_me: UnescMe, @"esc!": @"Esc!") void {
\\ _ = normal;
\\ _ = unesc_me;
\\ _ = @"esc!";
\\}
\\
\\const Normal = struct {};
\\const UnescMe = struct {
\\ x: X,
\\ const X = union(EnumUnesc) {
\\ normal,
\\ unesc_me,
\\ @"esc!",
\\ };
\\ const EnumUnesc = enum {
\\ normal,
\\ unesc_me,
\\ @"esc!",
\\ };
\\};
\\const @"Esc!" = struct {
\\ normal: bool = false,
\\ unesc_me: bool = false,
\\ @"esc!": bool = false,
\\};
\\
\\pub fn main() void {
\\ _ = val_normal;
\\ _ = val_normal;
\\ _ = val_unesc_me;
\\ _ = val_unesc_me;
\\ _ = @"val_esc!";
\\
\\ fnNormal();
\\ fnNormal();
\\ fnUnescMe();
\\ fnUnescMe();
\\ @"fnEsc!"();
\\
\\ fnWithArgs(1, Normal{}, UnescMe{}, @"Esc!"{});
\\ fnWithArgs(1, Normal{}, UnescMe{}, @"Esc!"{});
\\ fnWithArgs(1, Normal{}, Normal{}, @"Esc!"{});
\\
\\ const local_val1: Normal = .{};
\\ const local_val2: UnescMe = .{
\\ .x = .unesc_me,
\\ };
\\ fnWithArgs(local_val1, local_val2, .{ .normal = true, .unesc_me = true, .@"esc!" = true });
\\ fnWithArgs(local_val1, local_val2, .{ .normal = true, .unesc_me = true, .@"esc!" = true });
\\
\\ var x: u8 = 'x';
\\ switch (x) {
\\ x => {},
\\ }
\\
\\ _ = @import("std"); // Don't mess with @builtins
\\ // @"comment"
\\}
\\
);
}
test "zig fmt: canonicalize symbols (character escapes)" {
try testTransform(
\\const @"\x46\x6f\x6f\x64" = struct {
\\ @"\x62\x61\x72\x6E": @"\x43\x72\x61\x62" = false,
\\ @"\u{67}\u{6C}o\u{70}\xFF": @"Cra\x62" = false,
\\ @"\x65\x72\x72\x6F\x72": Crab = true,
\\ @"\x74\x72\x79": Crab = true,
\\ @"\u{74}\u{79}\u{70}\u{65}": @"any\u{6F}\u{70}\u{61}\u{71}\u{75}\u{65}",
\\
\\ const @"\x43\x72\x61\x62" = bool;
\\ const @"\x61\x6E\x79\x6F\x70\x61que" = void;
\\};
\\
\\test "unicode" {
\\ const @"cąbbäge ⚡" = 2;
\\ _ = @"cąbbäge ⚡";
\\ const @"\u{01f422} friend\u{f6}" = 4;
\\ _ = @"🐢 friendö";
\\}
\\
,
\\const Food = struct {
\\ barn: Crab = false,
\\ @"glop\xFF": Crab = false,
\\ @"error": Crab = true,
\\ @"try": Crab = true,
\\ type: @"anyopaque",
\\
\\ const Crab = bool;
\\ const @"anyopaque" = void;
\\};
\\
\\test "unicode" {
\\ const @"cąbbäge ⚡" = 2;
\\ _ = @"cąbbäge ⚡";
\\ const @"\u{01f422} friend\u{f6}" = 4;
\\ _ = @"🐢 friendö";
\\}
\\
);
}
test "zig fmt: canonicalize symbols (asm)" {
try testTransform(
\\test "asm" {
\\ const @"null" = usize;
\\ const @"try": usize = 808;
\\ const arg: usize = 2;
\\ _ = asm volatile ("syscall"
\\ : [@"void"] "={rax}" (-> @"null"),
\\ : [@"error"] "{rax}" (@"try"),
\\ [@"arg1"] "{rdi}" (arg),
\\ [arg2] "{rsi}" (arg),
\\ [arg3] "{rdx}" (arg),
\\ : "rcx", "fn"
\\ );
\\
\\ const @"false": usize = 10;
\\ const @"true" = "explode";
\\ _ = asm volatile (@"true"
\\ : [one] "={rax}" (@"false"),
\\ : [two] "{rax}" (@"false"),
\\ );
\\}
\\
,
\\test "asm" {
\\ const @"null" = usize;
\\ const @"try": usize = 808;
\\ const arg: usize = 2;
\\ _ = asm volatile ("syscall"
\\ : [void] "={rax}" (-> @"null"),
\\ : [@"error"] "{rax}" (@"try"),
\\ [arg1] "{rdi}" (arg),
\\ [arg2] "{rsi}" (arg),
\\ [arg3] "{rdx}" (arg),
\\ : .{ .rcx = true, .@"fn" = true }
\\ );
\\
\\ const @"false": usize = 10;
\\ const @"true" = "explode";
\\ _ = asm volatile (@"true"
\\ : [one] "={rax}" (false),
\\ : [two] "{rax}" (@"false"),
\\ );
\\}
\\
);
}
test "zig fmt: canonicalize cast builtins" {
try testTransform(
\\const foo = @alignCast(@ptrCast(bar));
\\const baz = @constCast(@ptrCast(@addrSpaceCast(@volatileCast(@alignCast(bar)))));
\\
,
\\const foo = @ptrCast(@alignCast(bar));
\\const baz = @ptrCast(@alignCast(@addrSpaceCast(@constCast(@volatileCast(bar)))));
\\
);
}
test "zig fmt: do not canonicalize invalid cast builtins" {
try testCanonical(
\\const foo = @alignCast(@volatileCast(@ptrCast(@alignCast(bar))));
\\
);
}
test "Ast header smoke test" {
try std.testing.expectEqual(zigNode(c.AST_NODE_IF), Ast.Node.Tag.@"if");
}