Commit a0ebfa64d9

Evan Krause <evangrayk@gmail.com>
2019-07-29 03:37:35
support zero-sized structs in zig.fmt.format
1 parent 74abc5a
Changed files (1)
std/fmt.zig
@@ -374,9 +374,10 @@ pub fn formatType(
                 return output(context, "{ ... }");
             }
             comptime var field_i = 0;
+            try output(context, "{");
             inline while (field_i < @memberCount(T)) : (field_i += 1) {
                 if (field_i == 0) {
-                    try output(context, "{ .");
+                    try output(context, " .");
                 } else {
                     try output(context, ", .");
                 }
@@ -1439,6 +1440,21 @@ test "struct.self-referential" {
     try testFmt("S{ .a = S{ .a = S{ .a = S{ ... } } } }", "{}", inst);
 }
 
+test "struct.zero-size" {
+    const A = struct {
+        fn foo() void {}
+    };
+    const B = struct {
+        a: A,
+        c: i32,
+    };
+
+    const a = A{};
+    const b = B{ .a = a, .c = 0 };
+
+    try testFmt("B{ .a = A{ }, .c = 0 }", "{}", b);
+}
+
 test "bytes.hex" {
     const some_bytes = "\xCA\xFE\xBA\xBE";
     try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes);