authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-05-20 01:31:37+02:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-05-20 17:13:47+02:00
log4a582734fd84f2096ca9bb559387ddd04c70ed57
treefa4a8353e8eaa9aa901e1f7641631905df6f3368
parent3fd8ac092e88ac4bc604afcdd3fcb33249dba967
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: replace callconv(.Inline) with the inline keyword


3 files changed, 47 insertions(+), 19 deletions(-)

lib/std/zig/ast.zig+2-1
......@@ -459,7 +459,8 @@ pub const Tree = struct {
459459 .keyword_extern,
460460 .keyword_export,
461461 .keyword_pub,
462 .keyword_threadlocal,
462 .keyword_inline,
463 .keyword_noinline,
463464 .string_literal,
464465 => continue,
465466
lib/std/zig/parser_test.zig+27-7
......@@ -61,13 +61,33 @@ test "zig fmt: respect line breaks in struct field value declaration" {
6161 );
6262}
6363
64// TODO Remove this after zig 0.9.0 is released.
65test "zig fmt: rewrite inline functions as callconv(.Inline)" {
66 try testTransform(
64test "zig fmt: respect line breaks before functions" {
65 try testCanonical(
66 \\const std = @import("std");
67 \\
6768 \\inline fn foo() void {}
6869 \\
69 ,
70 \\noinline fn foo() void {}
71 \\
72 \\export fn foo() void {}
73 \\
74 \\extern fn foo() void;
75 \\
76 \\extern "foo" fn foo() void;
77 \\
78 );
79}
80
81test "zig fmt: rewrite callconv(.Inline) to the inline keyword" {
82 try testTransform(
7083 \\fn foo() callconv(.Inline) void {}
84 \\const bar = .Inline;
85 \\fn foo() callconv(bar) void {}
86 \\
87 ,
88 \\inline fn foo() void {}
89 \\const bar = .Inline;
90 \\fn foo() callconv(bar) void {}
7191 \\
7292 );
7393}
......@@ -2867,17 +2887,17 @@ test "zig fmt: functions" {
28672887 \\extern fn puts(s: *const u8) c_int;
28682888 \\extern "c" fn puts(s: *const u8) c_int;
28692889 \\export fn puts(s: *const u8) c_int;
2870 \\fn puts(s: *const u8) callconv(.Inline) c_int;
2890 \\inline fn puts(s: *const u8) c_int;
28712891 \\noinline fn puts(s: *const u8) c_int;
28722892 \\pub extern fn puts(s: *const u8) c_int;
28732893 \\pub extern "c" fn puts(s: *const u8) c_int;
28742894 \\pub export fn puts(s: *const u8) c_int;
2875 \\pub fn puts(s: *const u8) callconv(.Inline) c_int;
2895 \\pub inline fn puts(s: *const u8) c_int;
28762896 \\pub noinline fn puts(s: *const u8) c_int;
28772897 \\pub extern fn puts(s: *const u8) align(2 + 2) c_int;
28782898 \\pub extern "c" fn puts(s: *const u8) align(2 + 2) c_int;
28792899 \\pub export fn puts(s: *const u8) align(2 + 2) c_int;
2880 \\pub fn puts(s: *const u8) align(2 + 2) callconv(.Inline) c_int;
2900 \\pub inline fn puts(s: *const u8) align(2 + 2) c_int;
28812901 \\pub noinline fn puts(s: *const u8) align(2 + 2) c_int;
28822902 \\
28832903 );
lib/std/zig/render.zig+18-11
......@@ -83,13 +83,23 @@ fn renderMember(gpa: *Allocator, ais: *Ais, tree: ast.Tree, decl: ast.Node.Index
8383 }
8484 }
8585 while (i < fn_token) : (i += 1) {
86 if (token_tags[i] == .keyword_inline) {
87 // TODO remove this special case when 0.9.0 is released.
88 // See the commit that introduced this comment for more details.
89 continue;
90 }
9186 try renderToken(ais, tree, i, .space);
9287 }
88 switch (tree.nodes.items(.tag)[fn_proto]) {
89 .fn_proto_one, .fn_proto => {
90 const callconv_expr = if (tree.nodes.items(.tag)[fn_proto] == .fn_proto_one)
91 tree.extraData(datas[fn_proto].lhs, ast.Node.FnProtoOne).callconv_expr
92 else
93 tree.extraData(datas[fn_proto].lhs, ast.Node.FnProto).callconv_expr;
94 if (callconv_expr != 0 and tree.nodes.items(.tag)[callconv_expr] == .enum_literal) {
95 if (mem.eql(u8, "Inline", tree.tokenSlice(main_tokens[callconv_expr]))) {
96 try ais.writer().writeAll("inline ");
97 }
98 }
99 },
100 .fn_proto_simple, .fn_proto_multi => {},
101 else => unreachable,
102 }
93103 assert(datas[decl].rhs != 0);
94104 try renderExpression(gpa, ais, tree, fn_proto, .space);
95105 return renderExpression(gpa, ais, tree, datas[decl].rhs, space);
......@@ -1246,9 +1256,6 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
12461256 const token_tags = tree.tokens.items(.tag);
12471257 const token_starts = tree.tokens.items(.start);
12481258
1249 const is_inline = fn_proto.ast.fn_token > 0 and
1250 token_tags[fn_proto.ast.fn_token - 1] == .keyword_inline;
1251
12521259 const after_fn_token = fn_proto.ast.fn_token + 1;
12531260 const lparen = if (token_tags[after_fn_token] == .identifier) blk: {
12541261 try renderToken(ais, tree, fn_proto.ast.fn_token, .space); // fn
......@@ -1424,7 +1431,9 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
14241431 try renderToken(ais, tree, section_rparen, .space); // )
14251432 }
14261433
1427 if (fn_proto.ast.callconv_expr != 0) {
1434 if (fn_proto.ast.callconv_expr != 0 and
1435 !mem.eql(u8, "Inline", tree.tokenSlice(tree.nodes.items(.main_token)[fn_proto.ast.callconv_expr])))
1436 {
14281437 const callconv_lparen = tree.firstToken(fn_proto.ast.callconv_expr) - 1;
14291438 const callconv_rparen = tree.lastToken(fn_proto.ast.callconv_expr) + 1;
14301439
......@@ -1432,8 +1441,6 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
14321441 try renderToken(ais, tree, callconv_lparen, .none); // (
14331442 try renderExpression(gpa, ais, tree, fn_proto.ast.callconv_expr, .none);
14341443 try renderToken(ais, tree, callconv_rparen, .space); // )
1435 } else if (is_inline) {
1436 try ais.writer().writeAll("callconv(.Inline) ");
14371444 }
14381445
14391446 if (token_tags[maybe_bang] == .bang) {