authorgravatar for jarred@jarredsumner.comJarred Sumner <jarred@jarredsumner.com> 2021-06-08 20:42:29-07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-13 10:33:49+03:00
log540b52931aeeee7c72c73361118d07ab986cf048
tree6530a554de35d5b84fce69ddec0009d2de0301e2
parentb2879825d7e59fc40f3defe272039d723b692f43

Improve error message when std.fmt.format is missing arguments

Use fmt in fmt so the number in the error message is fmt'd

3 files changed, 15 insertions(+), 2 deletions(-)

doc/langref.html.in+1-1
...@@ -6262,7 +6262,7 @@ pub fn printValue(self: *Writer, value: anytype) !void {...@@ -6262,7 +6262,7 @@ pub fn printValue(self: *Writer, value: anytype) !void {
6262 <p>6262 <p>
6263 And now, what happens if we give too many arguments to {#syntax#}printf{#endsyntax#}?6263 And now, what happens if we give too many arguments to {#syntax#}printf{#endsyntax#}?
6264 </p>6264 </p>
6265 {#code_begin|test_err|Unused arguments#}6265 {#code_begin|test_err|Unused argument in "here is a string: '{s}' here is a number: {}#}
6266const print = @import("std").debug.print;6266const print = @import("std").debug.print;
62676267
6268const a_number: i32 = 1234;6268const a_number: i32 = 1234;
lib/std/fmt.zig+6-1
...@@ -359,7 +359,12 @@ pub fn format(...@@ -359,7 +359,12 @@ pub fn format(
359 }359 }
360360
361 if (comptime arg_state.hasUnusedArgs()) {361 if (comptime arg_state.hasUnusedArgs()) {
362 @compileError("Unused arguments");362 const missing_count = arg_state.args_len - @popCount(ArgSetType, arg_state.used_args);
363 switch (missing_count) {
364 0 => unreachable,
365 1 => @compileError("Unused argument in \"" ++ fmt ++ "\""),
366 else => @compileError((comptime comptimePrint("{d}", .{missing_count})) ++ " unused arguments in \"" ++ fmt ++ "\""),
367 }
363 }368 }
364}369}
365370
test/compile_errors.zig+8
...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
2const std = @import("std");2const std = @import("std");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("std.fmt error for unused arguments",
6 \\pub fn main() !void {
7 \\ @import("std").debug.print("{d} {d} {d} {d} {d}", .{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15});
8 \\}
9 , &.{
10 \\error: 10 unused arguments in "{d} {d} {d} {d} {d}"
11 });
12
5 cases.add("lazy pointer with undefined element type",13 cases.add("lazy pointer with undefined element type",
6 \\export fn foo() void {14 \\export fn foo() void {
7 \\ comptime var T: type = undefined;15 \\ comptime var T: type = undefined;