authorgravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2026-07-03 12:22:31+02:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-07-17 10:15:09+01:00
log0c6153d82773f47efb5d7d7ce3746f741919a6fc
treed5d7495f7223c6930c2088391306070321553495
parent274949fdbb7a98d374d5e994110dbfd639c70acf
signaturelock-open Commit is signed but in an unrecognized format.

std.zig.Ast.Render: canonicalize `@intFromEnum(x)` and `@enumFromInt(x)` to `@backingInt(x)` and `@fromBackingInt(@intCast(x))`

In other words, implements a `zig fmt` fixup.

2 files changed, 79 insertions(+), 2 deletions(-)

lib/std/zig/Ast/Render.zig+19-2
......@@ -1652,7 +1652,17 @@ fn renderBuiltinCall(
16521652 const tree = r.tree;
16531653 const ais = r.ais;
16541654
1655 try renderToken(r, builtin_token, .none); // @name
1655 // remove before 0.18.0 is released
1656 const builtin_token_slice = tree.tokenSlice(builtin_token); // @name
1657 const lexeme: []const u8, const have_int_cast: bool = lexeme: {
1658 if (mem.eql(u8, builtin_token_slice, "@intFromEnum"))
1659 break :lexeme .{ "@backingInt", false };
1660 if (mem.eql(u8, builtin_token_slice, "@enumFromInt"))
1661 break :lexeme .{ "@fromBackingInt(@intCast", true };
1662 break :lexeme .{ builtin_token_slice, false };
1663 };
1664 try ais.writeAll(lexeme);
1665 try renderSpace(r, builtin_token, builtin_token_slice.len, .none);
16561666
16571667 if (r.fixups.rebase_imported_paths) |prefix| {
16581668 const slice = tree.tokenSlice(builtin_token);
......@@ -1675,7 +1685,14 @@ fn renderBuiltinCall(
16751685 }
16761686 }
16771687
1678 return renderParamList(r, builtin_token + 1, params, space);
1688 try renderParamList(r, builtin_token + 1, params, .skip); // space is rendered below
1689 if (have_int_cast) try ais.writeAll(")");
1690 const rparen: Ast.TokenIndex = rparen: {
1691 if (params.len == 0) break :rparen builtin_token + 1 + 1;
1692 const after_last_param_tok = tree.lastToken(params[params.len - 1]) + 1;
1693 break :rparen after_last_param_tok + @intFromBool(tree.tokenTag(after_last_param_tok) == .comma);
1694 };
1695 return renderSpace(r, rparen, tokenSliceForRender(tree, rparen).len, space);
16791696}
16801697
16811698fn fnProtoRparen(tree: Ast, fn_proto: Ast.full.FnProto, maybe_bang: Ast.TokenIndex) Ast.TokenIndex {
lib/std/zig/parser_test.zig+60
......@@ -6938,6 +6938,66 @@ test "zig fmt: inner over-indented if expressions becoming multiline" {
69386938 );
69396939}
69406940
6941test "zig fmt: canonicalize @intFromEnum(x) to @backingInt(x)" {
6942 try testTransform(
6943 \\const a = @intFromEnum(x);
6944 \\
6945 \\const b = @intFromEnum(
6946 \\ x,
6947 \\);
6948 \\
6949 \\const c = @intFromEnum(x); // comment preserved
6950 \\
6951 \\const d = @intFromEnum( // comment 1 preserved
6952 \\ x, // comment 2 preserved
6953 \\); // comment 3 preserved
6954 \\
6955 ,
6956 \\const a = @backingInt(x);
6957 \\
6958 \\const b = @backingInt(
6959 \\ x,
6960 \\);
6961 \\
6962 \\const c = @backingInt(x); // comment preserved
6963 \\
6964 \\const d = @backingInt( // comment 1 preserved
6965 \\ x, // comment 2 preserved
6966 \\); // comment 3 preserved
6967 \\
6968 );
6969}
6970
6971test "zig fmt: canonicalize @enumFromInt(x) to @fromBackingInt(@intCast(x))" {
6972 try testTransform(
6973 \\const a: E = @enumFromInt(x);
6974 \\
6975 \\const b: E = @enumFromInt(
6976 \\ x,
6977 \\);
6978 \\
6979 \\const c: E = @enumFromInt(x); // comment preserved
6980 \\
6981 \\const d: E = @enumFromInt( // comment 1 preserved
6982 \\ x, // comment 2 preserved
6983 \\); // comment 3 preserved
6984 \\
6985 ,
6986 \\const a: E = @fromBackingInt(@intCast(x));
6987 \\
6988 \\const b: E = @fromBackingInt(@intCast(
6989 \\ x,
6990 \\));
6991 \\
6992 \\const c: E = @fromBackingInt(@intCast(x)); // comment preserved
6993 \\
6994 \\const d: E = @fromBackingInt(@intCast( // comment 1 preserved
6995 \\ x, // comment 2 preserved
6996 \\)); // comment 3 preserved
6997 \\
6998 );
6999}
7000
69417001test "recovery: top level" {
69427002 try testError(
69437003 \\test "" {inline}