Skip to content

Commit 73f2671

Browse files
authored
std.format: properly handle vectors of pointers
1 parent 3a6a8b8 commit 73f2671

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/std/fmt.zig

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,10 @@ const ANY = "any";
456456

457457
pub fn defaultSpec(comptime T: type) [:0]const u8 {
458458
switch (@typeInfo(T)) {
459-
.array => |_| return ANY,
459+
.array, .vector => return ANY,
460460
.pointer => |ptr_info| switch (ptr_info.size) {
461461
.One => switch (@typeInfo(ptr_info.child)) {
462-
.array => |_| return ANY,
462+
.array => return ANY,
463463
else => {},
464464
},
465465
.Many, .C => return "*",
@@ -680,7 +680,7 @@ pub fn formatType(
680680
try writer.writeAll("{ ");
681681
var i: usize = 0;
682682
while (i < info.len) : (i += 1) {
683-
try formatValue(value[i], actual_fmt, options, writer);
683+
try formatType(value[i], actual_fmt, options, writer, max_depth - 1);
684684
if (i < info.len - 1) {
685685
try writer.writeAll(", ");
686686
}
@@ -2608,6 +2608,22 @@ test "vector" {
26082608
try expectFmt("{ -2, -1, +0, +1 }", "{d:5}", .{vi64});
26092609
try expectFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64});
26102610
try expectFmt("{ 3e8, 7d0, bb8, fa0 }", "{x}", .{vu64});
2611+
2612+
const x: [4]u64 = undefined;
2613+
const vp: @Vector(4, *const u64) = [_]*const u64{ &x[0], &x[1], &x[2], &x[3] };
2614+
const vop: @Vector(4, ?*const u64) = [_]?*const u64{ &x[0], null, null, &x[3] };
2615+
2616+
var expect_buffer: [@sizeOf(usize) * 2 * 4 + 64]u8 = undefined;
2617+
try expectFmt(try bufPrint(
2618+
&expect_buffer,
2619+
"{{ {}, {}, {}, {} }}",
2620+
.{ &x[0], &x[1], &x[2], &x[3] },
2621+
), "{}", .{vp});
2622+
try expectFmt(try bufPrint(
2623+
&expect_buffer,
2624+
"{{ {?}, null, null, {?} }}",
2625+
.{ &x[0], &x[3] },
2626+
), "{any}", .{vop});
26112627
}
26122628

26132629
test "enum-literal" {

0 commit comments

Comments
 (0)