mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
std.fmt: lowercase compile errors
`compileError\("([A-Z])` and `compileError\("\L\1`. It's pretty convenient.
This commit is contained in:
parent
7ba1f9bfb5
commit
4ef7d85810
@ -7228,7 +7228,7 @@ pub fn print(self: *Writer, arg0: []const u8, arg1: i32) !void {
|
|||||||
<p>
|
<p>
|
||||||
And now, what happens if we give too many arguments to {#syntax#}print{#endsyntax#}?
|
And now, what happens if we give too many arguments to {#syntax#}print{#endsyntax#}?
|
||||||
</p>
|
</p>
|
||||||
{#code_begin|test_err|Unused argument in 'here is a string: '{s}' here is a number: {}#}
|
{#code_begin|test_err|unused argument in 'here is a string: '{s}' here is a number: {}#}
|
||||||
const print = @import("std").debug.print;
|
const print = @import("std").debug.print;
|
||||||
|
|
||||||
const a_number: i32 = 1234;
|
const a_number: i32 = 1234;
|
||||||
|
|||||||
@ -83,7 +83,7 @@ pub fn format(
|
|||||||
const ArgsType = @TypeOf(args);
|
const ArgsType = @TypeOf(args);
|
||||||
const args_type_info = @typeInfo(ArgsType);
|
const args_type_info = @typeInfo(ArgsType);
|
||||||
if (args_type_info != .Struct) {
|
if (args_type_info != .Struct) {
|
||||||
@compileError("Expected tuple or struct argument, found " ++ @typeName(ArgsType));
|
@compileError("expected tuple or struct argument, found " ++ @typeName(ArgsType));
|
||||||
}
|
}
|
||||||
|
|
||||||
const fields_info = args_type_info.Struct.fields;
|
const fields_info = args_type_info.Struct.fields;
|
||||||
@ -127,7 +127,7 @@ pub fn format(
|
|||||||
if (i >= fmt.len) break;
|
if (i >= fmt.len) break;
|
||||||
|
|
||||||
if (fmt[i] == '}') {
|
if (fmt[i] == '}') {
|
||||||
@compileError("Missing opening {");
|
@compileError("missing opening {");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get past the {
|
// Get past the {
|
||||||
@ -140,7 +140,7 @@ pub fn format(
|
|||||||
const fmt_end = i;
|
const fmt_end = i;
|
||||||
|
|
||||||
if (i >= fmt.len) {
|
if (i >= fmt.len) {
|
||||||
@compileError("Missing closing }");
|
@compileError("missing closing }");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get past the }
|
// Get past the }
|
||||||
@ -152,7 +152,7 @@ pub fn format(
|
|||||||
.none => null,
|
.none => null,
|
||||||
.number => |pos| pos,
|
.number => |pos| pos,
|
||||||
.named => |arg_name| meta.fieldIndex(ArgsType, arg_name) orelse
|
.named => |arg_name| meta.fieldIndex(ArgsType, arg_name) orelse
|
||||||
@compileError("No argument with name '" ++ arg_name ++ "'"),
|
@compileError("no argument with name '" ++ arg_name ++ "'"),
|
||||||
};
|
};
|
||||||
|
|
||||||
const width = switch (placeholder.width) {
|
const width = switch (placeholder.width) {
|
||||||
@ -160,8 +160,8 @@ pub fn format(
|
|||||||
.number => |v| v,
|
.number => |v| v,
|
||||||
.named => |arg_name| blk: {
|
.named => |arg_name| blk: {
|
||||||
const arg_i = comptime meta.fieldIndex(ArgsType, arg_name) orelse
|
const arg_i = comptime meta.fieldIndex(ArgsType, arg_name) orelse
|
||||||
@compileError("No argument with name '" ++ arg_name ++ "'");
|
@compileError("no argument with name '" ++ arg_name ++ "'");
|
||||||
_ = comptime arg_state.nextArg(arg_i) orelse @compileError("Too few arguments");
|
_ = comptime arg_state.nextArg(arg_i) orelse @compileError("too few arguments");
|
||||||
break :blk @field(args, arg_name);
|
break :blk @field(args, arg_name);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -171,14 +171,14 @@ pub fn format(
|
|||||||
.number => |v| v,
|
.number => |v| v,
|
||||||
.named => |arg_name| blk: {
|
.named => |arg_name| blk: {
|
||||||
const arg_i = comptime meta.fieldIndex(ArgsType, arg_name) orelse
|
const arg_i = comptime meta.fieldIndex(ArgsType, arg_name) orelse
|
||||||
@compileError("No argument with name '" ++ arg_name ++ "'");
|
@compileError("no argument with name '" ++ arg_name ++ "'");
|
||||||
_ = comptime arg_state.nextArg(arg_i) orelse @compileError("Too few arguments");
|
_ = comptime arg_state.nextArg(arg_i) orelse @compileError("too few arguments");
|
||||||
break :blk @field(args, arg_name);
|
break :blk @field(args, arg_name);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const arg_to_print = comptime arg_state.nextArg(arg_pos) orelse
|
const arg_to_print = comptime arg_state.nextArg(arg_pos) orelse
|
||||||
@compileError("Too few arguments");
|
@compileError("too few arguments");
|
||||||
|
|
||||||
try formatType(
|
try formatType(
|
||||||
@field(args, fields_info[arg_to_print].name),
|
@field(args, fields_info[arg_to_print].name),
|
||||||
@ -198,7 +198,7 @@ pub fn format(
|
|||||||
const missing_count = arg_state.args_len - @popCount(ArgSetType, arg_state.used_args);
|
const missing_count = arg_state.args_len - @popCount(ArgSetType, arg_state.used_args);
|
||||||
switch (missing_count) {
|
switch (missing_count) {
|
||||||
0 => unreachable,
|
0 => unreachable,
|
||||||
1 => @compileError("Unused argument in '" ++ fmt ++ "'"),
|
1 => @compileError("unused argument in '" ++ fmt ++ "'"),
|
||||||
else => @compileError((comptime comptimePrint("{d}", .{missing_count})) ++ " unused arguments in '" ++ fmt ++ "'"),
|
else => @compileError((comptime comptimePrint("{d}", .{missing_count})) ++ " unused arguments in '" ++ fmt ++ "'"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -217,7 +217,7 @@ fn parsePlaceholder(comptime str: anytype) Placeholder {
|
|||||||
// Skip the colon, if present
|
// Skip the colon, if present
|
||||||
if (comptime parser.char()) |ch| {
|
if (comptime parser.char()) |ch| {
|
||||||
if (ch != ':') {
|
if (ch != ':') {
|
||||||
@compileError("Expected : or }, found '" ++ [1]u8{ch} ++ "'");
|
@compileError("expected : or }, found '" ++ [1]u8{ch} ++ "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,7 +252,7 @@ fn parsePlaceholder(comptime str: anytype) Placeholder {
|
|||||||
// Skip the dot, if present
|
// Skip the dot, if present
|
||||||
if (comptime parser.char()) |ch| {
|
if (comptime parser.char()) |ch| {
|
||||||
if (ch != '.') {
|
if (ch != '.') {
|
||||||
@compileError("Expected . or }, found '" ++ [1]u8{ch} ++ "'");
|
@compileError("expected . or }, found '" ++ [1]u8{ch} ++ "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +261,7 @@ fn parsePlaceholder(comptime str: anytype) Placeholder {
|
|||||||
@compileError(@errorName(err));
|
@compileError(@errorName(err));
|
||||||
|
|
||||||
if (comptime parser.char()) |ch| {
|
if (comptime parser.char()) |ch| {
|
||||||
@compileError("Extraneous trailing character '" ++ [1]u8{ch} ++ "'");
|
@compileError("extraneous trailing character '" ++ [1]u8{ch} ++ "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Placeholder{
|
return Placeholder{
|
||||||
@ -423,7 +423,7 @@ pub fn formatAddress(value: anytype, options: FormatOptions, writer: anytype) @T
|
|||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
@compileError("Cannot format non-pointer type " ++ @typeName(T) ++ " with * specifier");
|
@compileError("cannot format non-pointer type " ++ @typeName(T) ++ " with * specifier");
|
||||||
}
|
}
|
||||||
|
|
||||||
// This ANY const is a workaround for: https://github.com/ziglang/zig/issues/7948
|
// This ANY const is a workaround for: https://github.com/ziglang/zig/issues/7948
|
||||||
@ -608,7 +608,7 @@ pub fn formatType(
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@compileError("Unknown format string: '" ++ actual_fmt ++ "' for type '" ++ @typeName(T) ++ "'");
|
@compileError("unknown format string: '" ++ actual_fmt ++ "' for type '" ++ @typeName(T) ++ "'");
|
||||||
},
|
},
|
||||||
.Enum, .Union, .Struct => {
|
.Enum, .Union, .Struct => {
|
||||||
return formatType(value.*, actual_fmt, options, writer, max_depth);
|
return formatType(value.*, actual_fmt, options, writer, max_depth);
|
||||||
@ -630,7 +630,7 @@ pub fn formatType(
|
|||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@compileError("Unknown format string: '" ++ actual_fmt ++ "' for type '" ++ @typeName(T) ++ "'");
|
@compileError("unknown format string: '" ++ actual_fmt ++ "' for type '" ++ @typeName(T) ++ "'");
|
||||||
},
|
},
|
||||||
.Slice => {
|
.Slice => {
|
||||||
if (actual_fmt.len == 0)
|
if (actual_fmt.len == 0)
|
||||||
@ -701,7 +701,7 @@ pub fn formatType(
|
|||||||
return formatBuf(buffer, options, writer);
|
return formatBuf(buffer, options, writer);
|
||||||
},
|
},
|
||||||
.Null => return formatBuf("null", options, writer),
|
.Null => return formatBuf("null", options, writer),
|
||||||
else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"),
|
else => @compileError("unable to format type '" ++ @typeName(T) ++ "'"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -747,13 +747,13 @@ pub fn formatIntValue(
|
|||||||
if (@typeInfo(@TypeOf(int_value)).Int.bits <= 8) {
|
if (@typeInfo(@TypeOf(int_value)).Int.bits <= 8) {
|
||||||
return formatAsciiChar(@as(u8, int_value), options, writer);
|
return formatAsciiChar(@as(u8, int_value), options, writer);
|
||||||
} else {
|
} else {
|
||||||
@compileError("Cannot print integer that is larger than 8 bits as an ASCII character");
|
@compileError("cannot print integer that is larger than 8 bits as an ASCII character");
|
||||||
}
|
}
|
||||||
} else if (comptime std.mem.eql(u8, fmt, "u")) {
|
} else if (comptime std.mem.eql(u8, fmt, "u")) {
|
||||||
if (@typeInfo(@TypeOf(int_value)).Int.bits <= 21) {
|
if (@typeInfo(@TypeOf(int_value)).Int.bits <= 21) {
|
||||||
return formatUnicodeCodepoint(@as(u21, int_value), options, writer);
|
return formatUnicodeCodepoint(@as(u21, int_value), options, writer);
|
||||||
} else {
|
} else {
|
||||||
@compileError("Cannot print integer that is larger than 21 bits as an UTF-8 sequence");
|
@compileError("cannot print integer that is larger than 21 bits as an UTF-8 sequence");
|
||||||
}
|
}
|
||||||
} else if (comptime std.mem.eql(u8, fmt, "b")) {
|
} else if (comptime std.mem.eql(u8, fmt, "b")) {
|
||||||
radix = 2;
|
radix = 2;
|
||||||
@ -768,7 +768,7 @@ pub fn formatIntValue(
|
|||||||
radix = 8;
|
radix = 8;
|
||||||
case = .lower;
|
case = .lower;
|
||||||
} else {
|
} else {
|
||||||
@compileError("Unsupported format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'");
|
@compileError("unsupported format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
return formatInt(int_value, radix, case, options, writer);
|
return formatInt(int_value, radix, case, options, writer);
|
||||||
@ -797,7 +797,7 @@ fn formatFloatValue(
|
|||||||
error.NoSpaceLeft => unreachable,
|
error.NoSpaceLeft => unreachable,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
@compileError("Unsupported format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'");
|
@compileError("unsupported format string '" ++ fmt ++ "' for type '" ++ @typeName(@TypeOf(value)) ++ "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
return formatBuf(buf_stream.getWritten(), options, writer);
|
return formatBuf(buf_stream.getWritten(), options, writer);
|
||||||
@ -959,7 +959,7 @@ pub fn fmtIntSizeBin(value: u64) std.fmt.Formatter(formatSizeBin) {
|
|||||||
|
|
||||||
fn checkTextFmt(comptime fmt: []const u8) void {
|
fn checkTextFmt(comptime fmt: []const u8) void {
|
||||||
if (fmt.len != 1)
|
if (fmt.len != 1)
|
||||||
@compileError("Unsupported format string '" ++ fmt ++ "' when formatting text");
|
@compileError("unsupported format string '" ++ fmt ++ "' when formatting text");
|
||||||
switch (fmt[0]) {
|
switch (fmt[0]) {
|
||||||
'x' => @compileError("specifier 'x' has been deprecated, wrap your argument in std.fmt.fmtSliceHexLower instead"),
|
'x' => @compileError("specifier 'x' has been deprecated, wrap your argument in std.fmt.fmtSliceHexLower instead"),
|
||||||
'X' => @compileError("specifier 'X' has been deprecated, wrap your argument in std.fmt.fmtSliceHexUpper instead"),
|
'X' => @compileError("specifier 'X' has been deprecated, wrap your argument in std.fmt.fmtSliceHexUpper instead"),
|
||||||
@ -2377,7 +2377,7 @@ test "custom" {
|
|||||||
} else if (comptime std.mem.eql(u8, fmt, "d")) {
|
} else if (comptime std.mem.eql(u8, fmt, "d")) {
|
||||||
return std.fmt.format(writer, "{d:.3}x{d:.3}", .{ self.x, self.y });
|
return std.fmt.format(writer, "{d:.3}x{d:.3}", .{ self.x, self.y });
|
||||||
} else {
|
} else {
|
||||||
@compileError("Unknown format character: '" ++ fmt ++ "'");
|
@compileError("unknown format character: '" ++ fmt ++ "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -2585,7 +2585,7 @@ test "formatType max_depth" {
|
|||||||
if (fmt.len == 0) {
|
if (fmt.len == 0) {
|
||||||
return std.fmt.format(writer, "({d:.3},{d:.3})", .{ self.x, self.y });
|
return std.fmt.format(writer, "({d:.3},{d:.3})", .{ self.x, self.y });
|
||||||
} else {
|
} else {
|
||||||
@compileError("Unknown format string: '" ++ fmt ++ "'");
|
@compileError("unknown format string: '" ++ fmt ++ "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user