| ... | ... | @@ -559,7 +559,7 @@ fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void { |
| 559 | 559 | .while_simple, |
| 560 | 560 | .while_cont, |
| 561 | 561 | .@"while", |
| 562 | | => return walkWhile(w, ast.fullWhile(node).?), |
| 562 | => return walkWhile(w, node, ast.fullWhile(node).?), |
| 563 | 563 | |
| 564 | 564 | .for_simple, |
| 565 | 565 | .@"for", |
| ... | ... | @@ -863,7 +863,37 @@ fn walkSwitchCase(w: *Walk, switch_case: Ast.full.SwitchCase) Error!void { |
| 863 | 863 | try walkExpression(w, switch_case.ast.target_expr); |
| 864 | 864 | } |
| 865 | 865 | |
| 866 | | fn walkWhile(w: *Walk, while_node: Ast.full.While) Error!void { |
| 866 | fn walkWhile(w: *Walk, node_index: Ast.Node.Index, while_node: Ast.full.While) Error!void { |
| 867 | assert(while_node.ast.cond_expr != 0); |
| 868 | assert(while_node.ast.then_expr != 0); |
| 869 | |
| 870 | // Perform these transformations in this priority order: |
| 871 | // 1. If the `else` expression is missing or an empty block, replace the condition with `if (true)` if it is not already. |
| 872 | // 2. If the `then` block is empty, replace the condition with `if (false)` if it is not already. |
| 873 | // 3. If the condition is `if (true)`, replace the `if` expression with the contents of the `then` expression. |
| 874 | // 4. If the condition is `if (false)`, replace the `if` expression with the contents of the `else` expression. |
| 875 | if (!isTrueIdent(w.ast, while_node.ast.cond_expr) and |
| 876 | (while_node.ast.else_expr == 0 or isEmptyBlock(w.ast, while_node.ast.else_expr))) |
| 877 | { |
| 878 | try w.transformations.ensureUnusedCapacity(1); |
| 879 | w.transformations.appendAssumeCapacity(.{ .replace_with_true = while_node.ast.cond_expr }); |
| 880 | } else if (!isFalseIdent(w.ast, while_node.ast.cond_expr) and isEmptyBlock(w.ast, while_node.ast.then_expr)) { |
| 881 | try w.transformations.ensureUnusedCapacity(1); |
| 882 | w.transformations.appendAssumeCapacity(.{ .replace_with_false = while_node.ast.cond_expr }); |
| 883 | } else if (isTrueIdent(w.ast, while_node.ast.cond_expr)) { |
| 884 | try w.transformations.ensureUnusedCapacity(1); |
| 885 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 886 | .to_replace = node_index, |
| 887 | .replacement = while_node.ast.then_expr, |
| 888 | } }); |
| 889 | } else if (isFalseIdent(w.ast, while_node.ast.cond_expr)) { |
| 890 | try w.transformations.ensureUnusedCapacity(1); |
| 891 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 892 | .to_replace = node_index, |
| 893 | .replacement = while_node.ast.else_expr, |
| 894 | } }); |
| 895 | } |
| 896 | |
| 867 | 897 | try walkExpression(w, while_node.ast.cond_expr); // condition |
| 868 | 898 | |
| 869 | 899 | if (while_node.ast.cont_expr != 0) { |