authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-31 15:47:48+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-31 18:25:58-05:00
logdee7804a8186d3f946860aa639cbf71046532549
tree2a143e9c3a1aced7e6da27c5b657d60cecba61a7
parenta62b4f268a956d7b1571cf738f7b916d4e83ad05

fmt: Fix logic to find the argument list closing )

Closes #4341

2 files changed, 53 insertions(+), 4 deletions(-)

lib/std/zig/parser_test.zig+38
......@@ -1,3 +1,41 @@
1test "zig fmt: trailing comma in fn parameter list" {
2 try testCanonical(
3 \\pub fn f(
4 \\ a: i32,
5 \\ b: i32,
6 \\) i32 {}
7 \\pub fn f(
8 \\ a: i32,
9 \\ b: i32,
10 \\) align(8) i32 {}
11 \\pub fn f(
12 \\ a: i32,
13 \\ b: i32,
14 \\) linksection(".text") i32 {}
15 \\pub fn f(
16 \\ a: i32,
17 \\ b: i32,
18 \\) callconv(.C) i32 {}
19 \\pub fn f(
20 \\ a: i32,
21 \\ b: i32,
22 \\) align(8) linksection(".text") i32 {}
23 \\pub fn f(
24 \\ a: i32,
25 \\ b: i32,
26 \\) align(8) callconv(.C) i32 {}
27 \\pub fn f(
28 \\ a: i32,
29 \\ b: i32,
30 \\) align(8) linksection(".text") callconv(.C) i32 {}
31 \\pub fn f(
32 \\ a: i32,
33 \\ b: i32,
34 \\) linksection(".text") callconv(.C) i32 {}
35 \\
36 );
37}
38
139// TODO: Remove condition after deprecating 'typeOf'. See https://github.com/ziglang/zig/issues/1348
240test "zig fmt: change @typeOf to @TypeOf" {
341 try testTransform(
lib/std/zig/render.zig+15-4
......@@ -1344,11 +1344,22 @@ fn renderExpression(
13441344 try renderToken(tree, stream, fn_proto.fn_token, indent, start_col, Space.Space); // fn
13451345 break :blk tree.nextToken(fn_proto.fn_token);
13461346 };
1347
1348 const rparen = tree.prevToken(switch (fn_proto.return_type) {
1349 ast.Node.FnProto.ReturnType.Explicit => |node| node.firstToken(),
1350 ast.Node.FnProto.ReturnType.InferErrorSet => |node| tree.prevToken(node.firstToken()),
1347 assert(tree.tokens.at(lparen).id == .LParen);
1348
1349 const rparen = tree.prevToken(
1350 // the first token for the annotation expressions is the left
1351 // parenthesis, hence the need for two prevToken
1352 if (fn_proto.align_expr) |align_expr|
1353 tree.prevToken(tree.prevToken(align_expr.firstToken()))
1354 else if (fn_proto.section_expr) |section_expr|
1355 tree.prevToken(tree.prevToken(section_expr.firstToken()))
1356 else if (fn_proto.callconv_expr) |callconv_expr|
1357 tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
1358 else switch (fn_proto.return_type) {
1359 .Explicit => |node| node.firstToken(),
1360 .InferErrorSet => |node| tree.prevToken(node.firstToken()),
13511361 });
1362 assert(tree.tokens.at(rparen).id == .RParen);
13521363
13531364 const src_params_trailing_comma = blk: {
13541365 const maybe_comma = tree.tokens.at(rparen - 1).id;