authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-04-30 19:20:03+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-01 18:30:31+01:00
log3b52e5a2217baca92f0328c0f9134e982bf15698
tree49e55842093f5ca9b63d5077692e9d5a81e130af
parent0cc8435a830d9d3850add163be4f12e5bd4f2f5c
signature Commit is signed but in an unrecognized format.

std.zig.render: fix switch rendering


3 files changed, 29 insertions(+), 38 deletions(-)

lib/std/zig/Ast.zig+3-2
......@@ -1896,11 +1896,12 @@ pub fn switchFull(tree: Ast, node: Node.Index) full.Switch {
18961896 .keyword_switch => .{ main_token, null },
18971897 else => unreachable,
18981898 };
1899 const extra = tree.extraData(data.rhs, Ast.Node.SubRange);
18991900 return .{
19001901 .ast = .{
19011902 .switch_token = switch_token,
19021903 .condition = data.lhs,
1903 .sub_range = data.rhs,
1904 .cases = tree.extra_data[extra.start..extra.end],
19041905 },
19051906 .label_token = label_token,
19061907 };
......@@ -2869,7 +2870,7 @@ pub const full = struct {
28692870 pub const Components = struct {
28702871 switch_token: TokenIndex,
28712872 condition: Node.Index,
2872 sub_range: Node.Index,
2873 cases: []const Node.Index,
28732874 };
28742875 };
28752876
lib/std/zig/AstGen.zig+2-3
......@@ -7598,9 +7598,8 @@ fn switchExpr(
75987598 const node_tags = tree.nodes.items(.tag);
75997599 const main_tokens = tree.nodes.items(.main_token);
76007600 const token_tags = tree.tokens.items(.tag);
7601 const operand_node = node_datas[node].lhs;
7602 const extra = tree.extraData(node_datas[node].rhs, Ast.Node.SubRange);
7603 const case_nodes = tree.extra_data[extra.start..extra.end];
7601 const operand_node = switch_full.ast.condition;
7602 const case_nodes = switch_full.ast.cases;
76047603
76057604 const need_rl = astgen.nodes_need_rl.contains(node);
76067605 const block_ri: ResultInfo = if (need_rl) ri else .{
lib/std/zig/render.zig+24-33
......@@ -693,39 +693,27 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
693693 return renderToken(r, datas[node].rhs, space);
694694 },
695695
696 .@"break" => {
696 .@"break", .@"continue" => {
697697 const main_token = main_tokens[node];
698698 const label_token = datas[node].lhs;
699699 const target = datas[node].rhs;
700700 if (label_token == 0 and target == 0) {
701 try renderToken(r, main_token, space); // break keyword
701 try renderToken(r, main_token, space); // break/continue
702702 } else if (label_token == 0 and target != 0) {
703 try renderToken(r, main_token, .space); // break keyword
703 try renderToken(r, main_token, .space); // break/continue
704704 try renderExpression(r, target, space);
705705 } else if (label_token != 0 and target == 0) {
706 try renderToken(r, main_token, .space); // break keyword
707 try renderToken(r, label_token - 1, .none); // colon
706 try renderToken(r, main_token, .space); // break/continue
707 try renderToken(r, label_token - 1, .none); // :
708708 try renderIdentifier(r, label_token, space, .eagerly_unquote); // identifier
709709 } else if (label_token != 0 and target != 0) {
710 try renderToken(r, main_token, .space); // break keyword
711 try renderToken(r, label_token - 1, .none); // colon
710 try renderToken(r, main_token, .space); // break/continue
711 try renderToken(r, label_token - 1, .none); // :
712712 try renderIdentifier(r, label_token, .space, .eagerly_unquote); // identifier
713713 try renderExpression(r, target, space);
714714 }
715715 },
716716
717 .@"continue" => {
718 const main_token = main_tokens[node];
719 const label = datas[node].lhs;
720 if (label != 0) {
721 try renderToken(r, main_token, .space); // continue
722 try renderToken(r, label - 1, .none); // :
723 return renderIdentifier(r, label, space, .eagerly_unquote); // label
724 } else {
725 return renderToken(r, main_token, space); // continue
726 }
727 },
728
729717 .@"return" => {
730718 if (datas[node].lhs != 0) {
731719 try renderToken(r, main_tokens[node], .space);
......@@ -845,26 +833,29 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
845833 .@"switch",
846834 .switch_comma,
847835 => {
848 const switch_token = main_tokens[node];
849 const condition = datas[node].lhs;
850 const extra = tree.extraData(datas[node].rhs, Ast.Node.SubRange);
851 const cases = tree.extra_data[extra.start..extra.end];
852 const rparen = tree.lastToken(condition) + 1;
836 const full = tree.switchFull(node);
853837
854 try renderToken(r, switch_token, .space); // switch keyword
855 try renderToken(r, switch_token + 1, .none); // lparen
856 try renderExpression(r, condition, .none); // condition expression
857 try renderToken(r, rparen, .space); // rparen
838 if (full.label_token) |label_token| {
839 try renderIdentifier(r, label_token, .none, .eagerly_unquote); // label
840 try renderToken(r, label_token + 1, .space); // :
841 }
842
843 const rparen = tree.lastToken(full.ast.condition) + 1;
844
845 try renderToken(r, full.ast.switch_token, .space); // switch
846 try renderToken(r, full.ast.switch_token + 1, .none); // (
847 try renderExpression(r, full.ast.condition, .none); // condition expression
848 try renderToken(r, rparen, .space); // )
858849
859850 ais.pushIndentNextLine();
860 if (cases.len == 0) {
861 try renderToken(r, rparen + 1, .none); // lbrace
851 if (full.ast.cases.len == 0) {
852 try renderToken(r, rparen + 1, .none); // {
862853 } else {
863 try renderToken(r, rparen + 1, .newline); // lbrace
864 try renderExpressions(r, cases, .comma);
854 try renderToken(r, rparen + 1, .newline); // {
855 try renderExpressions(r, full.ast.cases, .comma);
865856 }
866857 ais.popIndent();
867 return renderToken(r, tree.lastToken(node), space); // rbrace
858 return renderToken(r, tree.lastToken(node), space); // }
868859 },
869860
870861 .switch_case_one,