format struct pointers as "<typename>@<address>" (#752)

This commit is contained in:
Ben Noordhuis 2018-02-07 22:18:48 +01:00 committed by Andrew Kelley
parent 0090c2d70b
commit 79ad1d9610

View File

@ -228,7 +228,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons
if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) {
return output(context, (*value)[0..]);
} else {
@compileError("Unable to format type '" ++ @typeName(T) ++ "'");
return format(context, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
}
},
else => if (@canImplicitCast([]const u8, value)) {
@ -546,6 +546,12 @@ test "parse unsigned comptime" {
}
}
// Dummy field because of https://github.com/zig-lang/zig/issues/557.
// At top level because of https://github.com/zig-lang/zig/issues/675.
const Struct = struct {
unused: u8,
};
test "fmt.format" {
{
var buf1: [32]u8 = undefined;
@ -577,6 +583,14 @@ test "fmt.format" {
const result = try bufPrint(buf1[0..], "u3: {}\n", value);
assert(mem.eql(u8, result, "u3: 5\n"));
}
{
var buf1: [32]u8 = undefined;
const value = Struct {
.unused = 42,
};
const result = try bufPrint(buf1[0..], "pointer: {}\n", &value);
assert(mem.startsWith(u8, result, "pointer: Struct@"));
}
// TODO get these tests passing in release modes
// https://github.com/zig-lang/zig/issues/564