diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index 8df99a640f..483ae26956 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -529,6 +529,7 @@ pub const Tree = struct { .PtrTypeSentinel, .PtrType, .PtrTypeBitRange, + .ArrayType, .SwitchCaseOne, .SwitchCase, => n = datas[n].rhs, @@ -902,6 +903,10 @@ pub const Tree = struct { return main_tokens[n] + end_offset; } }, + .ArrayTypeSentinel => { + const extra = tree.extraData(datas[n].rhs, Node.ArrayTypeSentinel); + n = extra.elem_type; + }, // These are not supported by lastToken() because implementation would // require recursion due to the optional comma followed by rbrace. @@ -917,8 +922,6 @@ pub const Tree = struct { .TaggedUnionEnumTag => unreachable, // TODO .TaggedUnionEnumTagComma => unreachable, // TODO .SwitchRange => unreachable, // TODO - .ArrayType => unreachable, // TODO - .ArrayTypeSentinel => unreachable, // TODO }; } diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index e8e9c1a2e2..9eb7ecadba 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -353,6 +353,19 @@ test "zig fmt: anytype struct field" { ); } +test "zig fmt: array types last token" { + try testCanonical( + \\test { + \\ const x = [40]u32; + \\} + \\ + \\test { + \\ const x = [40:0]u32; + \\} + \\ + ); +} + //test "zig fmt: sentinel-terminated array type" { // try testCanonical( // \\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 {