aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorIsaac Freund <ifreund@ifreund.xyz>2021-02-10 19:19:58 +0100
committerAndrew Kelley <andrew@ziglang.org>2021-02-10 11:53:53 -0800
commit515d4920e79ca3c631f243f6a1d7fb6b48aa91cd (patch)
treeccfabf7bdf13af646ea5fd9e1d9340a109ecd1b1 /lib/std
parent5df7fc36c6e5d7caf7b5b5437bf40fec77a2b971 (diff)
downloadzig-515d4920e79ca3c631f243f6a1d7fb6b48aa91cd.tar.gz
zig-515d4920e79ca3c631f243f6a1d7fb6b48aa91cd.zip
zig fmt: fix 0 element struct and array init
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/zig/ast.zig7
-rw-r--r--lib/std/zig/parser_test.zig27
2 files changed, 32 insertions, 2 deletions
diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig
index 527c358103..358c40b28a 100644
--- a/lib/std/zig/ast.zig
+++ b/lib/std/zig/ast.zig
@@ -796,8 +796,11 @@ pub const Tree = struct {
.StructInitOne,
=> {
end_offset += 1; // rbrace
- n = datas[n].rhs;
- assert(n != 0);
+ if (datas[n].rhs == 0) {
+ return main_tokens[n] + end_offset;
+ } else {
+ n = datas[n].rhs;
+ }
},
.SliceOpen,
.CallOneComma,
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index 944722c296..bc1a36d7e3 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -464,6 +464,15 @@ test "zig fmt: anon literal in array" {
// );
//}
+test "zig fmt: anon struct literal 0 element" {
+ try testCanonical(
+ \\test {
+ \\ const x = .{};
+ \\}
+ \\
+ );
+}
+
test "zig fmt: anon struct literal 1 element" {
try testCanonical(
\\test {
@@ -527,6 +536,15 @@ test "zig fmt: anon struct literal 3 element comma" {
);
}
+test "zig fmt: struct literal 0 element" {
+ try testCanonical(
+ \\test {
+ \\ const x = X{};
+ \\}
+ \\
+ );
+}
+
test "zig fmt: struct literal 1 element" {
try testCanonical(
\\test {
@@ -653,6 +671,15 @@ test "zig fmt: anon list literal 3 element comma" {
);
}
+test "zig fmt: array literal 0 element" {
+ try testCanonical(
+ \\test {
+ \\ const x = [_]u32{};
+ \\}
+ \\
+ );
+}
+
test "zig fmt: array literal 1 element" {
try testCanonical(
\\test {