authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2019-05-29 10:23:21-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-29 20:46:08-04:00
log8eaf1387c712c62e96fa165ac3390c8f8f924daa
treebdf7b260a2103437fea29355d0dd779504aa5dc0
parentf9e7bd2682a83bad2b74686dd9b860fc2ce7ef8f

Fix fmt.zig handling of slices of slices

Discovered while calling testing.expectError on an error union with payload [][]const u8. Even though the payload was not going to be printed, the current format handling caused a compile error anyway because it couldn't be casted to []const u8. The new handling treats a []u8 as a string but otherwise treats the slice as a pointer.

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

std/fmt.zig+8-2
......@@ -220,8 +220,10 @@ pub fn formatType(
220220 if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) {
221221 return formatText(value, fmt, context, Errors, output);
222222 }
223 const casted_value = ([]const u8)(value);
224 return output(context, casted_value);
223 if (ptr_info.child == u8) {
224 return formatText(value, fmt, context, Errors, output);
225 }
226 return format(context, Errors, output, "{}@{x}", @typeName(ptr_info.child), @ptrToInt(value.ptr));
225227 },
226228 builtin.TypeInfo.Pointer.Size.C => {
227229 return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
......@@ -1009,6 +1011,10 @@ test "fmt.format" {
10091011 const value: []const u8 = "abc";
10101012 try testFmt("slice: abc\n", "slice: {}\n", value);
10111013 }
1014 {
1015 const value = @intToPtr([*]const []const u8, 0xdeadbeef)[0..0];
1016 try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", value);
1017 }
10121018 {
10131019 const value = @intToPtr(*i32, 0xdeadbeef);
10141020 try testFmt("pointer: i32@deadbeef\n", "pointer: {}\n", value);