authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-26 12:15:37-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-26 12:15:37-04:00
log57b78fff7366c134bfb56084ec353006a7cb39fc
tree6987bc1c7b9be6d30c8edf1b01aced6a86143968
parentd10e407977f1ba165ca3d0a04d7b270096185559
parent62fefe864840e4d2c8bf4d6ad5509bcac141e7c3

Merge branch 'daurnimator-pretty-print-non-exhaustive-enums'

closes #4693

1 files changed, 21 insertions(+), 9 deletions(-)

lib/std/fmt.zig+21-9
...@@ -362,12 +362,21 @@ pub fn formatType(...@@ -362,12 +362,21 @@ pub fn formatType(
362 if (enumInfo.is_exhaustive) {362 if (enumInfo.is_exhaustive) {
363 try out_stream.writeAll(".");363 try out_stream.writeAll(".");
364 try out_stream.writeAll(@tagName(value));364 try out_stream.writeAll(@tagName(value));
365 } else {365 return;
366 // TODO: when @tagName works on exhaustive enums print known enum strings366 }
367 try out_stream.writeAll("(");367
368 try formatType(@enumToInt(value), fmt, options, out_stream, max_depth);368 // Use @tagName only if value is one of known fields
369 try out_stream.writeAll(")");369 inline for (enumInfo.fields) |enumField| {
370 if (@enumToInt(value) == enumField.value) {
371 try out_stream.writeAll(".");
372 try out_stream.writeAll(@tagName(value));
373 return;
374 }
370 }375 }
376
377 try out_stream.writeAll("(");
378 try formatType(@enumToInt(value), fmt, options, out_stream, max_depth);
379 try out_stream.writeAll(")");
371 },380 },
372 .Union => {381 .Union => {
373 try out_stream.writeAll(@typeName(T));382 try out_stream.writeAll(@typeName(T));
...@@ -1308,6 +1317,8 @@ test "enum" {...@@ -1308,6 +1317,8 @@ test "enum" {
1308 const value = Enum.Two;1317 const value = Enum.Two;
1309 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{value});1318 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{value});
1310 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});1319 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{&value});
1320 try testFmt("enum: Enum.One\n", "enum: {x}\n", .{Enum.One});
1321 try testFmt("enum: Enum.Two\n", "enum: {X}\n", .{Enum.Two});
1311}1322}
13121323
1313test "non-exhaustive enum" {1324test "non-exhaustive enum" {
...@@ -1316,11 +1327,12 @@ test "non-exhaustive enum" {...@@ -1316,11 +1327,12 @@ test "non-exhaustive enum" {
1316 Two = 0xbeef,1327 Two = 0xbeef,
1317 _,1328 _,
1318 };1329 };
1319 try testFmt("enum: Enum(15)\n", "enum: {}\n", .{Enum.One});1330 try testFmt("enum: Enum.One\n", "enum: {}\n", .{Enum.One});
1320 try testFmt("enum: Enum(48879)\n", "enum: {}\n", .{Enum.Two});1331 try testFmt("enum: Enum.Two\n", "enum: {}\n", .{Enum.Two});
1321 try testFmt("enum: Enum(4660)\n", "enum: {}\n", .{@intToEnum(Enum, 0x1234)});1332 try testFmt("enum: Enum(4660)\n", "enum: {}\n", .{@intToEnum(Enum, 0x1234)});
1322 try testFmt("enum: Enum(f)\n", "enum: {x}\n", .{Enum.One});1333 try testFmt("enum: Enum.One\n", "enum: {x}\n", .{Enum.One});
1323 try testFmt("enum: Enum(beef)\n", "enum: {x}\n", .{Enum.Two});1334 try testFmt("enum: Enum.Two\n", "enum: {x}\n", .{Enum.Two});
1335 try testFmt("enum: Enum.Two\n", "enum: {X}\n", .{Enum.Two});
1324 try testFmt("enum: Enum(1234)\n", "enum: {x}\n", .{@intToEnum(Enum, 0x1234)});1336 try testFmt("enum: Enum(1234)\n", "enum: {x}\n", .{@intToEnum(Enum, 0x1234)});
1325}1337}
13261338