Merge pull request #2969 from evangrayk/zero-width-structs

fix zero-width structs in zig.fmt.format
This commit is contained in:
Marc Tiehuis 2019-07-29 19:19:47 +12:00 committed by GitHub
commit c47b75312d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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