Skip to content

Commit 0cf6ae2

Browse files
NicoElbersalexrp
authored andcommitted
zon.stringify: Correctly serialize unions with void fields
Closes #22933
1 parent 59dc15f commit 0cf6ae2

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/std/zon/stringify.zig

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@ pub fn Serializer(Writer: type) type {
504504
.bool, .null => try std.fmt.format(self.writer, "{}", .{val}),
505505
.enum_literal => try self.ident(@tagName(val)),
506506
.@"enum" => try self.ident(@tagName(val)),
507-
.void => try self.writer.writeAll("{}"),
508507
.pointer => |pointer| {
509508
// Try to serialize as a string
510509
const item: ?type = switch (@typeInfo(pointer.child)) {
@@ -579,15 +578,21 @@ pub fn Serializer(Writer: type) type {
579578
},
580579
.@"union" => |@"union"| {
581580
comptime assert(@"union".tag_type != null);
582-
var container = try self.startStruct(.{ .whitespace_style = .{ .fields = 1 } });
583581
switch (val) {
584-
inline else => |pl, tag| try container.fieldArbitraryDepth(
585-
@tagName(tag),
586-
pl,
587-
options,
588-
),
582+
inline else => |pl, tag| if (@TypeOf(pl) == void)
583+
try self.writer.print(".{s}", .{@tagName(tag)})
584+
else {
585+
var container = try self.startStruct(.{ .whitespace_style = .{ .fields = 1 } });
586+
587+
try container.fieldArbitraryDepth(
588+
@tagName(tag),
589+
pl,
590+
options,
591+
);
592+
593+
try container.finish();
594+
},
589595
}
590-
try container.finish();
591596
},
592597
.optional => if (val) |inner| {
593598
try self.valueArbitraryDepth(inner, options);
@@ -1116,6 +1121,12 @@ test "std.zon stringify whitespace, high level API" {
11161121
\\ 3,
11171122
\\} }
11181123
, .{ .inner = .{ 1, 2, 3 } }, .{});
1124+
1125+
const UnionWithVoid = union(enum) { a, b: void, c: u8 };
1126+
1127+
try expectSerializeEqual(
1128+
\\.a
1129+
, UnionWithVoid.a, .{});
11191130
}
11201131

11211132
test "std.zon stringify whitespace, low level API" {

0 commit comments

Comments
 (0)