From c6efb23796053a4409b0cf7b6abb4044719779aa Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 21 Feb 2021 18:20:46 -0700 Subject: [PATCH] zig fmt: rewrite inline functions as callconv(.Inline) --- lib/std/zig/parser_test.zig | 24 ++++++++++-------------- lib/std/zig/render.zig | 10 ++++++++++ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 40d58337e0..2ba85dca3c 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -4,18 +4,16 @@ // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. -// TODO Remove this after zig 0.8.0 is released. -// TODO need to add the logic to make this test pass. it was added in master -// but was not added in the ast-memory-layout branch yet. -//test "zig fmt: rewrite inline functions as callconv(.Inline)" { -// try testTransform( -// \\inline fn foo() void {} -// \\ -// , -// \\fn foo() callconv(.Inline) void {} -// \\ -// ); -//} +// TODO Remove this after zig 0.9.0 is released. +test "zig fmt: rewrite inline functions as callconv(.Inline)" { + try testTransform( + \\inline fn foo() void {} + \\ + , + \\fn foo() callconv(.Inline) void {} + \\ + ); +} test "zig fmt: simple top level comptime block" { try testCanonical( @@ -2490,8 +2488,6 @@ test "zig fmt: function attributes" { \\pub extern fn foo() void; \\extern "c" fn foo() void; \\pub extern "c" fn foo() void; - \\inline fn foo() void {} - \\pub inline fn foo() void {} \\noinline fn foo() void {} \\pub noinline fn foo() void {} \\ diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 48746ec391..37587fe5d3 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -79,6 +79,11 @@ fn renderMember(ais: *Ais, tree: ast.Tree, decl: ast.Node.Index, space: Space) E } } while (i < fn_token) : (i += 1) { + if (token_tags[i] == .keyword_inline) { + // TODO remove this special case when 0.9.0 is released. + // See the commit that introduced this comment for more details. + continue; + } try renderToken(ais, tree, i, .space); } assert(datas[decl].rhs != 0); @@ -1260,6 +1265,9 @@ fn renderFnProto(ais: *Ais, tree: ast.Tree, fn_proto: ast.full.FnProto, space: S const token_tags = tree.tokens.items(.tag); const token_starts = tree.tokens.items(.start); + const is_inline = fn_proto.ast.fn_token > 0 and + token_tags[fn_proto.ast.fn_token - 1] == .keyword_inline; + const after_fn_token = fn_proto.ast.fn_token + 1; const lparen = if (token_tags[after_fn_token] == .identifier) blk: { try renderToken(ais, tree, fn_proto.ast.fn_token, .space); // fn @@ -1435,6 +1443,8 @@ fn renderFnProto(ais: *Ais, tree: ast.Tree, fn_proto: ast.full.FnProto, space: S try renderToken(ais, tree, callconv_lparen, .none); // ( try renderExpression(ais, tree, fn_proto.ast.callconv_expr, .none); try renderToken(ais, tree, callconv_rparen, .space); // ) + } else if (is_inline) { + try ais.writer().writeAll("callconv(.Inline) "); } if (token_tags[maybe_bang] == .bang) {