zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 8eaf1387c712c62e96fa165ac3390c8f8f924daa (tree)
parent f9e7bd2682a83bad2b74686dd9b860fc2ce7ef8f
Author: tgschultz <tgschultz@gmail.com>
Date:   Wed, 29 May 2019 10:23:21 -0500

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.
Diffstat:
Mstd/fmt.zig | 10++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/std/fmt.zig b/std/fmt.zig @@ -220,8 +220,10 @@ pub fn formatType( if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) { return formatText(value, fmt, context, Errors, output); } - const casted_value = ([]const u8)(value); - return output(context, casted_value); + if (ptr_info.child == u8) { + return formatText(value, fmt, context, Errors, output); + } + return format(context, Errors, output, "{}@{x}", @typeName(ptr_info.child), @ptrToInt(value.ptr)); }, builtin.TypeInfo.Pointer.Size.C => { return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value)); @@ -1010,6 +1012,10 @@ test "fmt.format" { try testFmt("slice: abc\n", "slice: {}\n", value); } { + const value = @intToPtr([*]const []const u8, 0xdeadbeef)[0..0]; + try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", value); + } + { const value = @intToPtr(*i32, 0xdeadbeef); try testFmt("pointer: i32@deadbeef\n", "pointer: {}\n", value); try testFmt("pointer: i32@deadbeef\n", "pointer: {*}\n", value);