support zero-sized structs in zig.fmt.format
This commit is contained in:
parent
74abc5ad2f
commit
a0ebfa64d9
18
std/fmt.zig
18
std/fmt.zig
@ -374,9 +374,10 @@ pub fn formatType(
|
|||||||
return output(context, "{ ... }");
|
return output(context, "{ ... }");
|
||||||
}
|
}
|
||||||
comptime var field_i = 0;
|
comptime var field_i = 0;
|
||||||
|
try output(context, "{");
|
||||||
inline while (field_i < @memberCount(T)) : (field_i += 1) {
|
inline while (field_i < @memberCount(T)) : (field_i += 1) {
|
||||||
if (field_i == 0) {
|
if (field_i == 0) {
|
||||||
try output(context, "{ .");
|
try output(context, " .");
|
||||||
} else {
|
} else {
|
||||||
try output(context, ", .");
|
try output(context, ", .");
|
||||||
}
|
}
|
||||||
@ -1439,6 +1440,21 @@ test "struct.self-referential" {
|
|||||||
try testFmt("S{ .a = S{ .a = S{ .a = S{ ... } } } }", "{}", inst);
|
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" {
|
test "bytes.hex" {
|
||||||
const some_bytes = "\xCA\xFE\xBA\xBE";
|
const some_bytes = "\xCA\xFE\xBA\xBE";
|
||||||
try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes);
|
try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user