Commit 9c8aef55b4

Andrew Kelley <andrew@ziglang.org>
2025-07-04 03:50:16
std.fmt.format: use {t} for tag name rather than {s}
prevents footgun when formatted type changes from string to enum
1 parent 30c2921
lib/std/io/Writer.zig
@@ -851,12 +851,12 @@ pub fn printValue(
             }
         },
         .error_set => {
-            if (fmt.len == 1 and fmt[0] == 's') return w.writeAll(@errorName(value));
+            if (fmt.len == 1 and fmt[0] == 't') return w.writeAll(@errorName(value));
             if (!is_any and fmt.len != 0) invalidFmtError(fmt, value);
             try printErrorSet(w, value);
         },
         .@"enum" => {
-            if (fmt.len == 1 and fmt[0] == 's') {
+            if (fmt.len == 1 and fmt[0] == 't') {
                 try w.writeAll(@tagName(value));
                 return;
             }
@@ -881,7 +881,7 @@ pub fn printValue(
             return;
         },
         .@"union" => |info| {
-            if (fmt.len == 1 and fmt[0] == 's') {
+            if (fmt.len == 1 and fmt[0] == 't') {
                 try w.writeAll(@tagName(value));
                 return;
             }
lib/std/zig/llvm/Builder.zig
@@ -1265,7 +1265,7 @@ pub const Attribute = union(Kind) {
                     try w.writeByte(')');
                 },
                 .alignstack => |alignment| {
-                    try w.print(" {s}", .{attribute});
+                    try w.print(" {t}", .{attribute});
                     const alignment_bytes = alignment.toByteUnits() orelse return;
                     if (data.flags.pound) {
                         try w.print("={d}", .{alignment_bytes});
@@ -1274,7 +1274,7 @@ pub const Attribute = union(Kind) {
                     }
                 },
                 .allockind => |allockind| {
-                    try w.print(" {s}(\"", .{@tagName(attribute)});
+                    try w.print(" {t}(\"", .{attribute});
                     var any = false;
                     inline for (@typeInfo(AllocKind).@"struct".fields) |field| {
                         if (comptime std.mem.eql(u8, field.name, "_")) continue;
@@ -1289,13 +1289,13 @@ pub const Attribute = union(Kind) {
                     try w.writeAll("\")");
                 },
                 .allocsize => |allocsize| {
-                    try w.print(" {s}({d}", .{ @tagName(attribute), allocsize.elem_size });
+                    try w.print(" {t}({d}", .{ attribute, allocsize.elem_size });
                     if (allocsize.num_elems != AllocSize.none)
                         try w.print(",{d}", .{allocsize.num_elems});
                     try w.writeByte(')');
                 },
                 .memory => |memory| {
-                    try w.print(" {s}(", .{@tagName(attribute)});
+                    try w.print(" {t}(", .{attribute});
                     var any = memory.other != .none or
                         (memory.argmem == .none and memory.inaccessiblemem == .none);
                     if (any) try w.writeAll(@tagName(memory.other));
@@ -8445,7 +8445,7 @@ pub const Metadata = enum(u32) {
             }
             fmt_str = fmt_str ++ "(";
             inline for (fields[2..], names) |*field, name| {
-                fmt_str = fmt_str ++ "{[" ++ name ++ "]fS}";
+                fmt_str = fmt_str ++ "{[" ++ name ++ "]f}";
                 field.* = .{
                     .name = name,
                     .type = std.fmt.Formatter(FormatData, format),
lib/std/fmt.zig
@@ -67,6 +67,9 @@ pub const Options = struct {
 /// - `s`:
 ///   - for pointer-to-many and C pointers of u8, print as a C-string using zero-termination
 ///   - for slices of u8, print the entire slice as a string without zero-termination
+/// - `t`:
+///   - for enums and tagged unions: prints the tag name
+///   - for error sets: prints the error name
 /// - `b64`: output string as standard base64
 /// - `e`: output floating point value in scientific notation
 /// - `d`: output numeric value in decimal notation
src/arch/sparc64/CodeGen.zig
@@ -723,7 +723,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
 
         if (std.debug.runtime_safety) {
             if (self.air_bookkeeping < old_air_bookkeeping + 1) {
-                std.debug.panic("in codegen.zig, handling of AIR instruction %{d} ('{s}') did not do proper bookkeeping. Look for a missing call to finishAir.", .{ inst, air_tags[@intFromEnum(inst)] });
+                std.debug.panic("in codegen.zig, handling of AIR instruction %{d} ('{t}') did not do proper bookkeeping. Look for a missing call to finishAir.", .{ inst, air_tags[@intFromEnum(inst)] });
             }
         }
     }
src/arch/wasm/CodeGen.zig
@@ -2046,7 +2046,7 @@ fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) InnerError!void {
         try cg.genInst(inst);
 
         if (std.debug.runtime_safety and cg.air_bookkeeping < old_bookkeeping_value + 1) {
-            std.debug.panic("Missing call to `finishAir` in AIR instruction %{d} ('{s}')", .{
+            std.debug.panic("Missing call to `finishAir` in AIR instruction %{d} ('{t}')", .{
                 inst,
                 cg.air.instructions.items(.tag)[@intFromEnum(inst)],
             });
@@ -3326,7 +3326,7 @@ fn emitUndefined(cg: *CodeGen, ty: Type) InnerError!WValue {
             },
             else => unreachable,
         },
-        else => return cg.fail("Wasm TODO: emitUndefined for type: {s}\n", .{ty.zigTypeTag(zcu)}),
+        else => return cg.fail("Wasm TODO: emitUndefined for type: {t}\n", .{ty.zigTypeTag(zcu)}),
     }
 }
 
src/codegen/c.zig
@@ -1680,7 +1680,7 @@ pub const DeclGen = struct {
                         try w.writeAll("{(");
                         const ptr_ty = ty.slicePtrFieldType(zcu);
                         try dg.renderType(w, ptr_ty);
-                        return w.print("){f}, {0fx}}}", .{
+                        return w.print("){f}, {0f}}}", .{
                             try dg.fmtIntLiteralHex(.undef_usize, .Other),
                         });
                     },
src/link/Lld.zig
@@ -437,7 +437,7 @@ fn coffLink(lld: *Lld, arena: Allocator) !void {
             try argv.append(try allocPrint(arena, "-PDBALTPATH:{s}", .{out_pdb_basename}));
         }
         if (comp.version) |version| {
-            try argv.append(try allocPrint(arena, "-VERSION:{f}.{f}", .{ version.major, version.minor }));
+            try argv.append(try allocPrint(arena, "-VERSION:{d}.{d}", .{ version.major, version.minor }));
         }
 
         if (target_util.llvmMachineAbi(target)) |mabi| {
src/InternPool.zig
@@ -11406,7 +11406,7 @@ pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator)
     var it = instances.iterator();
     while (it.next()) |entry| {
         const generic_fn_owner_nav = ip.getNav(ip.funcDeclInfo(entry.key_ptr.*).owner_nav);
-        try stderr_bw.print("{f} ({f}): \n", .{ generic_fn_owner_nav.name.fmt(ip), entry.value_ptr.items.len });
+        try stderr_bw.print("{f} ({d}): \n", .{ generic_fn_owner_nav.name.fmt(ip), entry.value_ptr.items.len });
         for (entry.value_ptr.items) |index| {
             const unwrapped_index = index.unwrap(ip);
             const func = ip.extraFuncInstance(unwrapped_index.tid, unwrapped_index.getExtra(ip), unwrapped_index.getData(ip));
src/main.zig
@@ -3336,8 +3336,8 @@ fn buildOutputType(
         var hasher = file_writer.interface.hashed(Cache.Hasher.init("0123456789abcdef"), &buffer);
         var stdin_reader = fs.File.stdin().readerStreaming(&.{});
         _ = hasher.writer.sendFileAll(&stdin_reader, .unlimited) catch |err| switch (err) {
-            error.WriteFailed => fatal("failed to write {s}: {s}", .{ dump_path, file_writer.err.? }),
-            else => fatal("failed to pipe stdin to {s}: {s}", .{ dump_path, err }),
+            error.WriteFailed => fatal("failed to write {s}: {t}", .{ dump_path, file_writer.err.? }),
+            else => fatal("failed to pipe stdin to {s}: {t}", .{ dump_path, err }),
         };
         try hasher.writer.flush();
 
src/print_value.zig
@@ -102,7 +102,7 @@ pub fn print(
         .enum_tag => |enum_tag| {
             const enum_type = ip.loadEnumType(val.typeOf(zcu).toIntern());
             if (enum_type.tagValueIndex(ip, val.toIntern())) |tag_index| {
-                return writer.print(".{fi}", .{enum_type.names.get(ip)[tag_index].fmt(ip)});
+                return writer.print(".{f}", .{enum_type.names.get(ip)[tag_index].fmt(ip)});
             }
             if (level == 0) {
                 return writer.writeAll("@enumFromInt(...)");
@@ -207,7 +207,7 @@ fn printAggregate(
             for (0..max_len) |i| {
                 if (i != 0) try writer.writeAll(", ");
                 const field_name = ty.structFieldName(@intCast(i), zcu).unwrap().?;
-                try writer.print(".{fi} = ", .{field_name.fmt(ip)});
+                try writer.print(".{f} = ", .{field_name.fmt(ip)});
                 try print(try val.fieldValue(pt, i), writer, level - 1, pt, opt_sema);
             }
             try writer.writeAll(" }");
@@ -392,14 +392,14 @@ pub fn printPtrDerivation(
             const agg_ty = (try field.parent.ptrType(pt)).childType(zcu);
             switch (agg_ty.zigTypeTag(zcu)) {
                 .@"struct" => if (agg_ty.structFieldName(field.field_idx, zcu).unwrap()) |field_name| {
-                    try writer.print(".{fi}", .{field_name.fmt(ip)});
+                    try writer.print(".{f}", .{field_name.fmt(ip)});
                 } else {
                     try writer.print("[{d}]", .{field.field_idx});
                 },
                 .@"union" => {
                     const tag_ty = agg_ty.unionTagTypeHypothetical(zcu);
                     const field_name = tag_ty.enumFieldName(field.field_idx, zcu);
-                    try writer.print(".{fi}", .{field_name.fmt(ip)});
+                    try writer.print(".{f}", .{field_name.fmt(ip)});
                 },
                 .pointer => switch (field.field_idx) {
                     Value.slice_ptr_index => try writer.writeAll(".ptr"),
src/register_manager.zig
@@ -238,7 +238,7 @@ pub fn RegisterManager(
             if (i < count) return null;
 
             for (regs, insts) |reg, inst| {
-                log.debug("tryAllocReg {} for inst {f}", .{ reg, inst });
+                log.debug("tryAllocReg {} for inst {?f}", .{ reg, inst });
                 self.markRegAllocated(reg);
 
                 if (inst) |tracked_inst| {
@@ -317,7 +317,7 @@ pub fn RegisterManager(
             tracked_index: TrackedIndex,
             inst: ?Air.Inst.Index,
         ) AllocationError!void {
-            log.debug("getReg {} for inst {f}", .{ regAtTrackedIndex(tracked_index), inst });
+            log.debug("getReg {} for inst {?f}", .{ regAtTrackedIndex(tracked_index), inst });
             if (!self.isRegIndexFree(tracked_index)) {
                 // Move the instruction that was previously there to a
                 // stack allocation.
@@ -349,7 +349,7 @@ pub fn RegisterManager(
             tracked_index: TrackedIndex,
             inst: ?Air.Inst.Index,
         ) void {
-            log.debug("getRegAssumeFree {} for inst {f}", .{ regAtTrackedIndex(tracked_index), inst });
+            log.debug("getRegAssumeFree {} for inst {?f}", .{ regAtTrackedIndex(tracked_index), inst });
             self.markRegIndexAllocated(tracked_index);
 
             assert(self.isRegIndexFree(tracked_index));
src/Sema.zig
@@ -20465,7 +20465,7 @@ fn zirIntFromBool(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
     const is_vector = operand_ty.zigTypeTag(zcu) == .vector;
     const operand_scalar_ty = operand_ty.scalarType(zcu);
     if (operand_scalar_ty.toIntern() != .bool_type) {
-        return sema.fail(block, src, "expected 'bool', found '{s}'", .{operand_scalar_ty.zigTypeTag(zcu)});
+        return sema.fail(block, src, "expected 'bool', found '{t}'", .{operand_scalar_ty.zigTypeTag(zcu)});
     }
     const len = if (is_vector) operand_ty.vectorLen(zcu) else undefined;
     const dest_ty: Type = if (is_vector) try pt.vectorType(.{ .child = .u1_type, .len = len }) else .u1;
@@ -23576,7 +23576,7 @@ fn checkNumericType(
         .comptime_float, .float, .comptime_int, .int => {},
         .vector => switch (ty.childType(zcu).zigTypeTag(zcu)) {
             .comptime_float, .float, .comptime_int, .int => {},
-            else => |t| return sema.fail(block, ty_src, "expected number, found '{s}'", .{t}),
+            else => |t| return sema.fail(block, ty_src, "expected number, found '{t}'", .{t}),
         },
         else => return sema.fail(block, ty_src, "expected number, found '{f}'", .{ty.fmt(pt)}),
     }
@@ -37176,7 +37176,14 @@ fn explainWhyValueContainsReferenceToComptimeVar(sema: *Sema, msg: *Zcu.ErrorMsg
     }
 }
 
-fn notePathToComptimeAllocPtr(sema: *Sema, msg: *Zcu.ErrorMsg, src: LazySrcLoc, val: Value, intermediate_value_count: u32, start_value_name: InternPool.NullTerminatedString) Allocator.Error!union(enum) {
+fn notePathToComptimeAllocPtr(
+    sema: *Sema,
+    msg: *Zcu.ErrorMsg,
+    src: LazySrcLoc,
+    val: Value,
+    intermediate_value_count: u32,
+    start_value_name: InternPool.NullTerminatedString,
+) Allocator.Error!union(enum) {
     done,
     new_val: Value,
 } {
@@ -37187,7 +37194,7 @@ fn notePathToComptimeAllocPtr(sema: *Sema, msg: *Zcu.ErrorMsg, src: LazySrcLoc,
 
     var first_path: std.ArrayListUnmanaged(u8) = .empty;
     if (intermediate_value_count == 0) {
-        try first_path.print(arena, "{fi}", .{start_value_name.fmt(ip)});
+        try first_path.print(arena, "{f}", .{start_value_name.fmt(ip)});
     } else {
         try first_path.print(arena, "v{d}", .{intermediate_value_count - 1});
     }
@@ -37283,7 +37290,7 @@ fn notePathToComptimeAllocPtrInner(sema: *Sema, val: Value, path: *std.ArrayList
             const backing_enum = union_ty.unionTagTypeHypothetical(zcu);
             const field_idx = backing_enum.enumTagFieldIndex(.fromInterned(un.tag), zcu).?;
             const field_name = backing_enum.enumFieldName(field_idx, zcu);
-            try path.print(arena, ".{fi}", .{field_name.fmt(ip)});
+            try path.print(arena, ".{f}", .{field_name.fmt(ip)});
             return sema.notePathToComptimeAllocPtrInner(.fromInterned(un.val), path);
         },
         .aggregate => |agg| {
@@ -37308,7 +37315,7 @@ fn notePathToComptimeAllocPtrInner(sema: *Sema, val: Value, path: *std.ArrayList
                     try path.print(arena, "[{d}]", .{elem_idx});
                 } else {
                     const name = agg_ty.structFieldName(elem_idx, zcu).unwrap().?;
-                    try path.print(arena, ".{fi}", .{name.fmt(ip)});
+                    try path.print(arena, ".{f}", .{name.fmt(ip)});
                 },
                 else => unreachable,
             }