Skip to content

Commit ac6e2e7

Browse files
committed
std.zig.render: fix switch rendering
1 parent e962ba9 commit ac6e2e7

File tree

3 files changed

+29
-38
lines changed

3 files changed

+29
-38
lines changed

lib/std/zig/Ast.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,11 +1909,12 @@ pub fn switchFull(tree: Ast, node: Node.Index) full.Switch {
19091909
.keyword_switch => .{ main_token, null },
19101910
else => unreachable,
19111911
};
1912+
const extra = tree.extraData(data.rhs, Ast.Node.SubRange);
19121913
return .{
19131914
.ast = .{
19141915
.switch_token = switch_token,
19151916
.condition = data.lhs,
1916-
.sub_range = data.rhs,
1917+
.cases = tree.extra_data[extra.start..extra.end],
19171918
},
19181919
.label_token = label_token,
19191920
};
@@ -2880,7 +2881,7 @@ pub const full = struct {
28802881
pub const Components = struct {
28812882
switch_token: TokenIndex,
28822883
condition: Node.Index,
2883-
sub_range: Node.Index,
2884+
cases: []const Node.Index,
28842885
};
28852886
};
28862887

lib/std/zig/AstGen.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7621,9 +7621,8 @@ fn switchExpr(
76217621
const node_tags = tree.nodes.items(.tag);
76227622
const main_tokens = tree.nodes.items(.main_token);
76237623
const token_tags = tree.tokens.items(.tag);
7624-
const operand_node = node_datas[node].lhs;
7625-
const extra = tree.extraData(node_datas[node].rhs, Ast.Node.SubRange);
7626-
const case_nodes = tree.extra_data[extra.start..extra.end];
7624+
const operand_node = switch_full.ast.condition;
7625+
const case_nodes = switch_full.ast.cases;
76277626

76287627
const need_rl = astgen.nodes_need_rl.contains(node);
76297628
const block_ri: ResultInfo = if (need_rl) ri else .{

lib/std/zig/render.zig

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

0 commit comments

Comments
 (0)