Skip to content

Commit 4129f7f

Browse files
committed
std.zig.Ast: store lbrace and rbrace token in data of .error_set_decl
This makes the `.data` field the better choice over the `.main_token` for this tag.
1 parent d84055f commit 4129f7f

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

lib/compiler/reduce/Walk.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,7 @@ fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void {
444444
},
445445

446446
.error_set_decl => {
447-
const error_token = ast.nodeMainToken(node);
448-
const lbrace = error_token + 1;
449-
const rbrace = ast.nodeData(node).token;
447+
const lbrace, const rbrace = ast.nodeData(node).token_and_token;
450448

451449
var i = lbrace + 1;
452450
while (i < rbrace) : (i += 1) {

lib/std/zig/Ast.zig

+4-3
Original file line numberDiff line numberDiff line change
@@ -996,9 +996,8 @@ pub fn lastToken(tree: Ast, node: Node.Index) TokenIndex {
996996
.unwrap_optional,
997997
.asm_simple,
998998
=> return tree.nodeData(n).node_and_token[1] + end_offset,
999-
.error_set_decl => return tree.nodeData(n).token + end_offset,
1000999
.grouped_expression, .asm_input => return tree.nodeData(n).node_and_token[1] + end_offset,
1001-
.multiline_string_literal => return tree.nodeData(n).token_and_token[1] + end_offset,
1000+
.multiline_string_literal, .error_set_decl => return tree.nodeData(n).token_and_token[1] + end_offset,
10021001
.asm_output => return tree.nodeData(n).opt_node_and_token[1] + end_offset,
10031002
.error_value => return tree.nodeMainToken(n) + 2 + end_offset,
10041003

@@ -3758,7 +3757,9 @@ pub const Node = struct {
37583757
builtin_call_comma,
37593758
/// `error{a, b}`.
37603759
///
3761-
/// The `data` field is a `.token` to the '}'.
3760+
/// The `data` field is a `.token_and_token`:
3761+
/// 1. a `TokenIndex` to the `{` token.
3762+
/// 2. a `TokenIndex` to the `}` token.
37623763
///
37633764
/// The `main_token` field is the `error`.
37643765
error_set_decl,

lib/std/zig/AstGen.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -5974,9 +5974,9 @@ fn errorSetDecl(gz: *GenZir, ri: ResultInfo, node: Ast.Node.Index) InnerError!Zi
59745974
var idents: std.AutoHashMapUnmanaged(Zir.NullTerminatedString, Ast.TokenIndex) = .empty;
59755975
defer idents.deinit(gpa);
59765976

5977-
const error_token = tree.nodeMainToken(node);
5978-
var tok_i = error_token + 2;
5979-
while (true) : (tok_i += 1) {
5977+
const lbrace, const rbrace = tree.nodeData(node).token_and_token;
5978+
for (lbrace + 1..rbrace) |i| {
5979+
const tok_i: Ast.TokenIndex = @intCast(i);
59805980
switch (tree.tokenTag(tok_i)) {
59815981
.doc_comment, .comma => {},
59825982
.identifier => {
@@ -6003,7 +6003,6 @@ fn errorSetDecl(gz: *GenZir, ri: ResultInfo, node: Ast.Node.Index) InnerError!Zi
60036003
try astgen.extra.append(gpa, @intFromEnum(str_index));
60046004
fields_len += 1;
60056005
},
6006-
.r_brace => break,
60076006
else => unreachable,
60086007
}
60096008
}

lib/std/zig/Parse.zig

+6-1
Original file line numberDiff line numberDiff line change
@@ -2763,7 +2763,12 @@ fn parsePrimaryTypeExpr(p: *Parse) !?Node.Index {
27632763
return try p.addNode(.{
27642764
.tag = .error_set_decl,
27652765
.main_token = error_token,
2766-
.data = .{ .token = p.tok_i - 1 }, // rbrace
2766+
.data = .{
2767+
.token_and_token = .{
2768+
error_token + 1, // lbrace
2769+
p.tok_i - 1, // rbrace
2770+
},
2771+
},
27672772
});
27682773
},
27692774
else => {

lib/std/zig/render.zig

+1-2
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,7 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
739739

740740
.error_set_decl => {
741741
const error_token = tree.nodeMainToken(node);
742-
const lbrace = error_token + 1;
743-
const rbrace = tree.nodeData(node).token;
742+
const lbrace, const rbrace = tree.nodeData(node).token_and_token;
744743

745744
try renderToken(r, error_token, .none);
746745

0 commit comments

Comments
 (0)