| author | |
| committer | |
| log | 0c6153d82773f47efb5d7d7ce3746f741919a6fc |
| tree | d5d7495f7223c6930c2088391306070321553495 |
| parent | 274949fdbb7a98d374d5e994110dbfd639c70acf |
| signature |
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( | ... | @@ -1652,7 +1652,17 @@ fn renderBuiltinCall( |
| 1652 | const tree = r.tree; | 1652 | const tree = r.tree; |
| 1653 | const ais = r.ais; | 1653 | const ais = r.ais; |
| 1654 | 1654 | ||
| 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); | ||
| 1656 | 1666 | ||
| 1657 | if (r.fixups.rebase_imported_paths) |prefix| { | 1667 | if (r.fixups.rebase_imported_paths) |prefix| { |
| 1658 | const slice = tree.tokenSlice(builtin_token); | 1668 | const slice = tree.tokenSlice(builtin_token); |
| ... | @@ -1675,7 +1685,14 @@ fn renderBuiltinCall( | ... | @@ -1675,7 +1685,14 @@ fn renderBuiltinCall( |
| 1675 | } | 1685 | } |
| 1676 | } | 1686 | } |
| 1677 | 1687 | ||
| 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); | ||
| 1679 | } | 1696 | } |
| 1680 | 1697 | ||
| 1681 | fn fnProtoRparen(tree: Ast, fn_proto: Ast.full.FnProto, maybe_bang: Ast.TokenIndex) Ast.TokenIndex { | 1698 | fn 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" { | ... | @@ -6938,6 +6938,66 @@ test "zig fmt: inner over-indented if expressions becoming multiline" { |
| 6938 | ); | 6938 | ); |
| 6939 | } | 6939 | } |
| 6940 | 6940 | ||
| 6941 | test "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 | |||
| 6971 | test "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 | |||
| 6941 | test "recovery: top level" { | 7001 | test "recovery: top level" { |
| 6942 | try testError( | 7002 | try testError( |
| 6943 | \\test "" {inline} | 7003 | \\test "" {inline} |