commit 073f7ebb0e2168d04212fd855235c3543f86a2be (tree)
parent 9c8dfadbb1b95703a1503ae3a508f5acc29e7695
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Thu, 20 Sep 2018 13:46:20 -0400
fix formatInt to handle upcasting to base int size
Diffstat:
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
@@ -723,7 +723,9 @@ fn formatIntUnsigned(
// max_int_digits accounts for the minus sign. when printing an unsigned
// number we don't need to do that.
var buf: [max_int_digits - 1]u8 = undefined;
- var a = if (@sizeOf(@typeOf(value)) == 1) u8(value) else value;
+ const min_int_bits = comptime math.max(@typeOf(value).bit_count, @typeOf(base).bit_count);
+ const MinInt = @IntType(@typeOf(value).is_signed, min_int_bits);
+ var a: MinInt = value;
var index: usize = buf.len;
while (true) {
@@ -966,6 +968,14 @@ test "fmt.format" {
try testFmt("struct: Struct{ .field = 42 }\n", "struct: {}\n", &value);
}
{
+ const Struct = struct {
+ a: u0,
+ b: u1,
+ };
+ const value = Struct{ .a = 0, .b = 1 };
+ try testFmt("struct: Struct{ .a = 0, .b = 1 }\n", "struct: {}\n", value);
+ }
+ {
const Enum = enum {
One,
Two,