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 {...@@ -504,7 +504,6 @@ pub fn Serializer(Writer: type) type {
504 .bool, .null => try std.fmt.format(self.writer, "{}", .{val}),504 .bool, .null => try std.fmt.format(self.writer, "{}", .{val}),
505 .enum_literal => try self.ident(@tagName(val)),505 .enum_literal => try self.ident(@tagName(val)),
506 .@"enum" => try self.ident(@tagName(val)),506 .@"enum" => try self.ident(@tagName(val)),
507 .void => try self.writer.writeAll("{}"),
508 .pointer => |pointer| {507 .pointer => |pointer| {
509 // Try to serialize as a string508 // Try to serialize as a string
510 const item: ?type = switch (@typeInfo(pointer.child)) {509 const item: ?type = switch (@typeInfo(pointer.child)) {
...@@ -579,15 +578,21 @@ pub fn Serializer(Writer: type) type {...@@ -579,15 +578,21 @@ pub fn Serializer(Writer: type) type {
579 },578 },
580 .@"union" => |@"union"| {579 .@"union" => |@"union"| {
581 comptime assert(@"union".tag_type != null);580 comptime assert(@"union".tag_type != null);
582 var container = try self.startStruct(.{ .whitespace_style = .{ .fields = 1 } });
583 switch (val) {581 switch (val) {
584 inline else => |pl, tag| try container.fieldArbitraryDepth(582 inline else => |pl, tag| if (@TypeOf(pl) == void)
585 @tagName(tag),583 try self.writer.print(".{s}", .{@tagName(tag)})
586 pl,584 else {
587 options,585 var container = try self.startStruct(.{ .whitespace_style = .{ .fields = 1 } });
588 ),586
587 try container.fieldArbitraryDepth(
588 @tagName(tag),
589 pl,
590 options,
591 );
592
593 try container.finish();
594 },
589 }595 }
590 try container.finish();
591 },596 },
592 .optional => if (val) |inner| {597 .optional => if (val) |inner| {
593 try self.valueArbitraryDepth(inner, options);598 try self.valueArbitraryDepth(inner, options);
...@@ -1116,6 +1121,12 @@ test "std.zon stringify whitespace, high level API" {...@@ -1116,6 +1121,12 @@ test "std.zon stringify whitespace, high level API" {
1116 \\ 3,1121 \\ 3,
1117 \\} }1122 \\} }
1118 , .{ .inner = .{ 1, 2, 3 } }, .{});1123 , .{ .inner = .{ 1, 2, 3 } }, .{});
1124
1125 const UnionWithVoid = union(enum) { a, b: void, c: u8 };
1126
1127 try expectSerializeEqual(
1128 \\.a
1129 , UnionWithVoid.a, .{});
1119}1130}
11201131
1121test "std.zon stringify whitespace, low level API" {1132test "std.zon stringify whitespace, low level API" {