authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-09 15:07:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-09 15:07:45-07:00
log2468766a894d66037fb81791dc93fcb69e4c4055
treea60db187d75d41af16facd8934d5a6748963c64f
parent51a9a6aab696b9fbd69dadb5a569cfe184fabecc

std: forbid alignment formatting options sometimes


1 files changed, 26 insertions(+), 3 deletions(-)

lib/std/io/Writer.zig+26-3
......@@ -805,15 +805,18 @@ pub fn printValue(
805805 .pointer => |info| switch (info.size) {
806806 .one, .slice => {
807807 const slice: []const u8 = value;
808 optionsForbidden(options);
808809 return printHex(w, slice, .lower);
809810 },
810811 .many, .c => {
811812 const slice: [:0]const u8 = std.mem.span(value);
813 optionsForbidden(options);
812814 return printHex(w, slice, .lower);
813815 },
814816 },
815817 .array => {
816818 const slice: []const u8 = &value;
819 optionsForbidden(options);
817820 return printHex(w, slice, .lower);
818821 },
819822 .vector => return printVector(w, fmt, options, value, max_depth),
......@@ -827,15 +830,18 @@ pub fn printValue(
827830 .pointer => |info| switch (info.size) {
828831 .one, .slice => {
829832 const slice: []const u8 = value;
833 optionsForbidden(options);
830834 return printHex(w, slice, .upper);
831835 },
832836 .many, .c => {
833837 const slice: [:0]const u8 = std.mem.span(value);
838 optionsForbidden(options);
834839 return printHex(w, slice, .upper);
835840 },
836841 },
837842 .array => {
838843 const slice: []const u8 = &value;
844 optionsForbidden(options);
839845 return printHex(w, slice, .upper);
840846 },
841847 .vector => return printVector(w, fmt, options, value, max_depth),
......@@ -845,15 +851,18 @@ pub fn printValue(
845851 .pointer => |info| switch (info.size) {
846852 .one, .slice => {
847853 const slice: []const u8 = value;
854 optionsForbidden(options); // Alignment not allowed on strings.
848855 return w.writeAll(slice);
849856 },
850857 .many, .c => {
851858 const slice: [:0]const u8 = std.mem.span(value);
859 optionsForbidden(options); // Alignment not allowed on strings.
852860 return w.writeAll(slice);
853861 },
854862 },
855863 .array => {
856864 const slice: []const u8 = &value;
865 optionsForbidden(options); // Alignment not allowed on strings.
857866 return w.writeAll(slice);
858867 },
859868 else => invalidFmtError(fmt, value),
......@@ -900,15 +909,18 @@ pub fn printValue(
900909 .pointer => |info| switch (info.size) {
901910 .one, .slice => {
902911 const slice: []const u8 = value;
912 optionsForbidden(options);
903913 return w.printBase64(slice);
904914 },
905915 .many, .c => {
906916 const slice: [:0]const u8 = std.mem.span(value);
917 optionsForbidden(options);
907918 return w.printBase64(slice);
908919 },
909920 },
910921 .array => {
911922 const slice: []const u8 = &value;
923 optionsForbidden(options);
912924 return w.printBase64(slice);
913925 },
914926 else => invalidFmtError(fmt, value),
......@@ -933,11 +945,12 @@ pub fn printValue(
933945 },
934946 .bool => {
935947 if (!is_any and fmt.len != 0) invalidFmtError(fmt, value);
936 return w.writeAll(if (value) "true" else "false");
948 const string: []const u8 = if (value) "true" else "false";
949 return w.alignBufferOptions(string, options);
937950 },
938951 .void => {
939952 if (!is_any and fmt.len != 0) invalidFmtError(fmt, value);
940 return w.writeAll("void");
953 return w.alignBufferOptions("void", options);
941954 },
942955 .optional => {
943956 const remaining_fmt = comptime if (fmt.len > 0 and fmt[0] == '?')
......@@ -967,10 +980,12 @@ pub fn printValue(
967980 },
968981 .error_set => {
969982 if (!is_any and fmt.len != 0) invalidFmtError(fmt, value);
983 optionsForbidden(options);
970984 return printErrorSet(w, value);
971985 },
972986 .@"enum" => |info| {
973987 if (!is_any and fmt.len != 0) invalidFmtError(fmt, value);
988 optionsForbidden(options);
974989 if (info.is_exhaustive) {
975990 return printEnumExhaustive(w, value);
976991 } else {
......@@ -1067,6 +1082,7 @@ pub fn printValue(
10671082 },
10681083 .many, .c => {
10691084 if (!is_any) @compileError("cannot format pointer without a specifier (i.e. {s} or {*})");
1085 optionsForbidden(options);
10701086 try w.printAddress(value);
10711087 },
10721088 .slice => {
......@@ -1102,21 +1118,28 @@ pub fn printValue(
11021118 .@"fn" => @compileError("unable to format function body type, use '*const " ++ @typeName(T) ++ "' for a function pointer type"),
11031119 .type => {
11041120 if (!is_any and fmt.len != 0) invalidFmtError(fmt, value);
1121 optionsForbidden(options);
11051122 return w.writeAll(@typeName(value));
11061123 },
11071124 .enum_literal => {
11081125 if (!is_any and fmt.len != 0) invalidFmtError(fmt, value);
1126 optionsForbidden(options);
11091127 var vecs: [2][]const u8 = .{ ".", @tagName(value) };
11101128 return w.writeVecAll(&vecs);
11111129 },
11121130 .null => {
11131131 if (!is_any and fmt.len != 0) invalidFmtError(fmt, value);
1114 return w.writeAll("null");
1132 return w.alignBufferOptions("null", options);
11151133 },
11161134 else => @compileError("unable to format type '" ++ @typeName(T) ++ "'"),
11171135 }
11181136}
11191137
1138fn optionsForbidden(options: std.fmt.Options) void {
1139 assert(options.precision == null);
1140 assert(options.width == null);
1141}
1142
11201143fn printErrorSet(w: *Writer, error_set: anyerror) Error!void {
11211144 var vecs: [2][]const u8 = .{ "error.", @errorName(error_set) };
11221145 try w.writeVecAll(&vecs);