mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
zon.stringify: Correctly serialize unions with void fields
Closes #22933
This commit is contained in:
parent
59dc15fa0a
commit
0cf6ae290b
@ -504,7 +504,6 @@ pub fn Serializer(Writer: type) type {
|
||||
.bool, .null => try std.fmt.format(self.writer, "{}", .{val}),
|
||||
.enum_literal => try self.ident(@tagName(val)),
|
||||
.@"enum" => try self.ident(@tagName(val)),
|
||||
.void => try self.writer.writeAll("{}"),
|
||||
.pointer => |pointer| {
|
||||
// Try to serialize as a string
|
||||
const item: ?type = switch (@typeInfo(pointer.child)) {
|
||||
@ -579,15 +578,21 @@ pub fn Serializer(Writer: type) type {
|
||||
},
|
||||
.@"union" => |@"union"| {
|
||||
comptime assert(@"union".tag_type != null);
|
||||
var container = try self.startStruct(.{ .whitespace_style = .{ .fields = 1 } });
|
||||
switch (val) {
|
||||
inline else => |pl, tag| try container.fieldArbitraryDepth(
|
||||
@tagName(tag),
|
||||
pl,
|
||||
options,
|
||||
),
|
||||
inline else => |pl, tag| if (@TypeOf(pl) == void)
|
||||
try self.writer.print(".{s}", .{@tagName(tag)})
|
||||
else {
|
||||
var container = try self.startStruct(.{ .whitespace_style = .{ .fields = 1 } });
|
||||
|
||||
try container.fieldArbitraryDepth(
|
||||
@tagName(tag),
|
||||
pl,
|
||||
options,
|
||||
);
|
||||
|
||||
try container.finish();
|
||||
},
|
||||
}
|
||||
try container.finish();
|
||||
},
|
||||
.optional => if (val) |inner| {
|
||||
try self.valueArbitraryDepth(inner, options);
|
||||
@ -1116,6 +1121,12 @@ test "std.zon stringify whitespace, high level API" {
|
||||
\\ 3,
|
||||
\\} }
|
||||
, .{ .inner = .{ 1, 2, 3 } }, .{});
|
||||
|
||||
const UnionWithVoid = union(enum) { a, b: void, c: u8 };
|
||||
|
||||
try expectSerializeEqual(
|
||||
\\.a
|
||||
, UnionWithVoid.a, .{});
|
||||
}
|
||||
|
||||
test "std.zon stringify whitespace, low level API" {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user