authorgravatar for nico.b.elbers@gmail.comNico Elbers <nico.b.elbers@gmail.com> 2025-02-18 18:25:46+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-02-19 07:21:59+01:00
log0cf6ae290b97e7fee7e37b5c487fd3e6b35248b3
tree4c222c28fc5eab4fd62eb75e66ebdb9b13fbe0d0
parent59dc15fa0ac1253e999a6a0bddd1eac4fa058c77

zon.stringify: Correctly serialize unions with void fields

Closes #22933

1 files changed, 19 insertions(+), 8 deletions(-)

lib/std/zon/stringify.zig+19-8
......@@ -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
11211132test "std.zon stringify whitespace, low level API" {