Skip to content

Commit e1f56c9

Browse files
committed
std.zig.ast: add test for iterate
closes #1101
1 parent 41e6c66 commit e1f56c9

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

std/zig/ast.zig

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ pub const Node = struct {
734734
var i = index;
735735

736736
if (self.doc_comments) |comments| {
737-
if (i < 1) return *comments.base;
737+
if (i < 1) return &comments.base;
738738
i -= 1;
739739
}
740740

@@ -1243,7 +1243,7 @@ pub const Node = struct {
12431243
i -= 1;
12441244

12451245
if (self.@"else") |@"else"| {
1246-
if (i < 1) return *@"else".base;
1246+
if (i < 1) return &@"else".base;
12471247
i -= 1;
12481248
}
12491249

@@ -1296,7 +1296,7 @@ pub const Node = struct {
12961296
i -= 1;
12971297

12981298
if (self.@"else") |@"else"| {
1299-
if (i < 1) return *@"else".base;
1299+
if (i < 1) return &@"else".base;
13001300
i -= 1;
13011301
}
13021302

@@ -1347,7 +1347,7 @@ pub const Node = struct {
13471347
i -= 1;
13481348

13491349
if (self.@"else") |@"else"| {
1350-
if (i < 1) return *@"else".base;
1350+
if (i < 1) return &@"else".base;
13511351
i -= 1;
13521352
}
13531353

@@ -1536,22 +1536,27 @@ pub const Node = struct {
15361536
var i = index;
15371537

15381538
switch (self.op) {
1539+
// TODO https://github.com/ziglang/zig/issues/1107
15391540
Op.SliceType => |addr_of_info| {
15401541
if (addr_of_info.align_info) |align_info| {
15411542
if (i < 1) return align_info.node;
15421543
i -= 1;
15431544
}
15441545
},
1545-
Op.AddrOf => |addr_of_info| {
1546+
1547+
Op.PtrType => |addr_of_info| {
15461548
if (addr_of_info.align_info) |align_info| {
15471549
if (i < 1) return align_info.node;
15481550
i -= 1;
15491551
}
15501552
},
1553+
15511554
Op.ArrayType => |size_expr| {
15521555
if (i < 1) return size_expr;
15531556
i -= 1;
15541557
},
1558+
1559+
Op.AddressOf,
15551560
Op.Await,
15561561
Op.BitNot,
15571562
Op.BoolNot,
@@ -1561,8 +1566,6 @@ pub const Node = struct {
15611566
Op.NegationWrap,
15621567
Op.Try,
15631568
Op.Resume,
1564-
Op.UnwrapOptional,
1565-
Op.PointerType,
15661569
=> {},
15671570
}
15681571

@@ -1667,7 +1670,9 @@ pub const Node = struct {
16671670
if (i < fields.len) return fields.at(i).*;
16681671
i -= fields.len;
16691672
},
1670-
Op.Deref => {},
1673+
Op.UnwrapOptional,
1674+
Op.Deref,
1675+
=> {},
16711676
}
16721677

16731678
return null;
@@ -2022,7 +2027,7 @@ pub const Node = struct {
20222027

20232028
switch (self.kind) {
20242029
Kind.Variable => |variable_name| {
2025-
if (i < 1) return *variable_name.base;
2030+
if (i < 1) return &variable_name.base;
20262031
i -= 1;
20272032
},
20282033
Kind.Return => |return_type| {
@@ -2092,10 +2097,10 @@ pub const Node = struct {
20922097
pub fn iterate(self: *Asm, index: usize) ?*Node {
20932098
var i = index;
20942099

2095-
if (i < self.outputs.len) return *(self.outputs.at(index).*).base;
2100+
if (i < self.outputs.len) return &self.outputs.at(index).*.base;
20962101
i -= self.outputs.len;
20972102

2098-
if (i < self.inputs.len) return *(self.inputs.at(index).*).base;
2103+
if (i < self.inputs.len) return &self.inputs.at(index).*.base;
20992104
i -= self.inputs.len;
21002105

21012106
return null;
@@ -2205,3 +2210,14 @@ pub const Node = struct {
22052210
}
22062211
};
22072212
};
2213+
2214+
test "iterate" {
2215+
var root = Node.Root{
2216+
.base = Node{ .id = Node.Id.Root },
2217+
.doc_comments = null,
2218+
.decls = Node.Root.DeclList.init(std.debug.global_allocator),
2219+
.eof_token = 0,
2220+
};
2221+
var base = &root.base;
2222+
assert(base.iterate(0) == null);
2223+
}

0 commit comments

Comments
 (0)