authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-30 15:48:07-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-08-30 15:48:07-04:00
log2148943fff5af1e31f75b1e6651f0b7614e7e36a
tree28b57538d63b452532f8d37ddf87be22bd009245
parentfed5c12d9a2ab14c5bb66251ba6ee3e356bce8d3
parent19964f5dc80792eb50cb3a45bdabe887a92d92cc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3139 from hspak/zig-fmt-nested-if

zig fmt: fix nested if

3 files changed, 32 insertions(+), 8 deletions(-)

std/special/compiler_rt/comparetf2.zig+7-6
...@@ -38,12 +38,14 @@ pub extern fn __letf2(a: f128, b: f128) c_int {...@@ -38,12 +38,14 @@ pub extern fn __letf2(a: f128, b: f128) c_int {
3838
39 // If at least one of a and b is positive, we get the same result comparing39 // If at least one of a and b is positive, we get the same result comparing
40 // a and b as signed integers as we would with a floating-point compare.40 // a and b as signed integers as we would with a floating-point compare.
41 return if ((aInt & bInt) >= 0) if (aInt < bInt)41 return if ((aInt & bInt) >= 0)
42 LE_LESS42 if (aInt < bInt)
43 else if (aInt == bInt)43 LE_LESS
44 LE_EQUAL44 else if (aInt == bInt)
45 LE_EQUAL
46 else
47 LE_GREATER
45 else48 else
46 LE_GREATER else
47 // Otherwise, both are negative, so we need to flip the sense of the49 // Otherwise, both are negative, so we need to flip the sense of the
48 // comparison to get the correct result. (This assumes a twos- or ones-50 // comparison to get the correct result. (This assumes a twos- or ones-
49 // complement integer representation; if integers are represented in a51 // complement integer representation; if integers are represented in a
...@@ -73,7 +75,6 @@ pub extern fn __getf2(a: f128, b: f128) c_int {...@@ -73,7 +75,6 @@ pub extern fn __getf2(a: f128, b: f128) c_int {
7375
74 if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED;76 if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED;
75 if ((aAbs | bAbs) == 0) return GE_EQUAL;77 if ((aAbs | bAbs) == 0) return GE_EQUAL;
76 // zig fmt issue here, see https://github.com/ziglang/zig/issues/2661
77 return if ((aInt & bInt) >= 0)78 return if ((aInt & bInt) >= 0)
78 if (aInt < bInt)79 if (aInt < bInt)
79 GE_LESS80 GE_LESS
std/zig/parser_test.zig+21
...@@ -482,6 +482,27 @@ test "zig fmt: if-else with comment before else" {...@@ -482,6 +482,27 @@ test "zig fmt: if-else with comment before else" {
482 );482 );
483}483}
484484
485test "zig fmt: if nested" {
486 try testCanonical(
487 \\pub fn foo() void {
488 \\ return if ((aInt & bInt) >= 0)
489 \\ if (aInt < bInt)
490 \\ GE_LESS
491 \\ else if (aInt == bInt)
492 \\ GE_EQUAL
493 \\ else
494 \\ GE_GREATER
495 \\ else if (aInt > bInt)
496 \\ GE_LESS
497 \\ else if (aInt == bInt)
498 \\ GE_EQUAL
499 \\ else
500 \\ GE_GREATER;
501 \\}
502 \\
503 );
504}
505
485test "zig fmt: respect line breaks in if-else" {506test "zig fmt: respect line breaks in if-else" {
486 try testCanonical(507 try testCanonical(
487 \\comptime {508 \\comptime {
std/zig/render.zig+4-2
...@@ -276,7 +276,6 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i...@@ -276,7 +276,6 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i
276 } else {276 } else {
277 try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type,277 try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type,
278 }278 }
279
280 } else if (field.type_expr == null and field.value_expr != null) {279 } else if (field.type_expr == null and field.value_expr != null) {
281 try renderToken(tree, stream, field.name_token, indent, start_col, Space.Space); // name280 try renderToken(tree, stream, field.name_token, indent, start_col, Space.Space); // name
282 try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // =281 try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // =
...@@ -1521,9 +1520,12 @@ fn renderExpression(...@@ -1521,9 +1520,12 @@ fn renderExpression(
15211520
1522 try renderExpression(allocator, stream, tree, indent, start_col, if_node.condition, Space.None); // condition1521 try renderExpression(allocator, stream, tree, indent, start_col, if_node.condition, Space.None); // condition
15231522
1523 const body_is_if_block = if_node.body.id == ast.Node.Id.If;
1524 const body_is_block = nodeIsBlock(if_node.body);1524 const body_is_block = nodeIsBlock(if_node.body);
15251525
1526 if (body_is_block) {1526 if (body_is_if_block) {
1527 try renderExtraNewline(tree, stream, start_col, if_node.body);
1528 } else if (body_is_block) {
1527 const after_rparen_space = if (if_node.payload == null) Space.BlockStart else Space.Space;1529 const after_rparen_space = if (if_node.payload == null) Space.BlockStart else Space.Space;
1528 try renderToken(tree, stream, rparen, indent, start_col, after_rparen_space); // )1530 try renderToken(tree, stream, rparen, indent, start_col, after_rparen_space); // )
15291531