support zero-sized structs in zig.fmt.format

master
Evan Krause 2019-07-28 18:37:35 -07:00
parent 74abc5ad2f
commit a0ebfa64d9
1 changed files with 17 additions and 1 deletions

View File

@ -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);