authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-02 13:26:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-02 13:28:31-07:00
log5103053977573131d8040a53d9ab3f2afd1b01b0
tree39919ce5c6497f9583f441d73458b1f7fc5c3da3
parentc5c23db6278332044c0606d78419000b68185e0c

compile errors test harness: support unknown file/line/column

This gets us 2 more passing compile error test cases.

3 files changed, 66 insertions(+), 38 deletions(-)

lib/std/fmt.zig+2-2
......@@ -362,8 +362,8 @@ pub fn format(
362362 const missing_count = arg_state.args_len - @popCount(ArgSetType, arg_state.used_args);
363363 switch (missing_count) {
364364 0 => unreachable,
365 1 => @compileError("Unused argument in \"" ++ fmt ++ "\""),
366 else => @compileError((comptime comptimePrint("{d}", .{missing_count})) ++ " unused arguments in \"" ++ fmt ++ "\""),
365 1 => @compileError("Unused argument in '" ++ fmt ++ "'"),
366 else => @compileError((comptime comptimePrint("{d}", .{missing_count})) ++ " unused arguments in '" ++ fmt ++ "'"),
367367 }
368368 }
369369}
src/test.zig+55-35
......@@ -37,7 +37,11 @@ const ErrorMsg = union(enum) {
3737 src: struct {
3838 src_path: []const u8,
3939 msg: []const u8,
40 // maxint means match anything
41 // this is a workaround for stage1 compiler bug I ran into when making it ?u32
4042 line: u32,
43 // maxint means match anything
44 // this is a workaround for stage1 compiler bug I ran into when making it ?u32
4145 column: u32,
4246 kind: Kind,
4347 },
......@@ -81,13 +85,23 @@ const ErrorMsg = union(enum) {
8185 _ = options;
8286 switch (self) {
8387 .src => |src| {
84 return writer.print("{s}:{d}:{d}: {s}: {s}", .{
85 src.src_path,
86 src.line + 1,
87 src.column + 1,
88 @tagName(src.kind),
89 src.msg,
90 });
88 if (!std.mem.eql(u8, src.src_path, "?") or
89 src.line != std.math.maxInt(u32) or
90 src.column != std.math.maxInt(u32))
91 {
92 try writer.print("{s}:", .{src.src_path});
93 if (src.line != std.math.maxInt(u32)) {
94 try writer.print("{d}:", .{src.line + 1});
95 } else {
96 try writer.writeAll("?:");
97 }
98 if (src.column != std.math.maxInt(u32)) {
99 try writer.print("{d}: ", .{src.column + 1});
100 } else {
101 try writer.writeAll("?: ");
102 }
103 }
104 return writer.print("{s}: {s}", .{ @tagName(src.kind), src.msg });
91105 },
92106 .plain => |plain| {
93107 return writer.print("{s}: {s}", .{ @tagName(plain.kind), plain.msg });
......@@ -220,8 +234,14 @@ pub const TestContext = struct {
220234 const kind_text = it.next() orelse @panic("missing 'error'/'note'");
221235 const msg = it.rest()[1..]; // skip over the space at end of "error: "
222236
223 const line = std.fmt.parseInt(u32, line_text, 10) catch @panic("bad line number");
224 const column = std.fmt.parseInt(u32, col_text, 10) catch @panic("bad column number");
237 const line: ?u32 = if (std.mem.eql(u8, line_text, "?"))
238 null
239 else
240 std.fmt.parseInt(u32, line_text, 10) catch @panic("bad line number");
241 const column: ?u32 = if (std.mem.eql(u8, line_text, "?"))
242 null
243 else
244 std.fmt.parseInt(u32, col_text, 10) catch @panic("bad column number");
225245 const kind: ErrorMsg.Kind = if (std.mem.eql(u8, kind_text, " error"))
226246 .@"error"
227247 else if (std.mem.eql(u8, kind_text, " note"))
......@@ -229,16 +249,28 @@ pub const TestContext = struct {
229249 else
230250 @panic("expected 'error'/'note'");
231251
232 if (line == 0 or column == 0) {
233 @panic("line and column must be specified starting at one");
234 }
252 const line_0based: u32 = if (line) |n| blk: {
253 if (n == 0) {
254 print("{s}: line must be specified starting at one\n", .{self.name});
255 return;
256 }
257 break :blk n - 1;
258 } else std.math.maxInt(u32);
259
260 const column_0based: u32 = if (column) |n| blk: {
261 if (n == 0) {
262 print("{s}: line must be specified starting at one\n", .{self.name});
263 return;
264 }
265 break :blk n - 1;
266 } else std.math.maxInt(u32);
235267
236268 array[i] = .{
237269 .src = .{
238270 .src_path = src_path,
239271 .msg = msg,
240 .line = line - 1,
241 .column = column - 1,
272 .line = line_0based,
273 .column = column_0based,
242274 .kind = kind,
243275 },
244276 };
......@@ -737,7 +769,7 @@ pub const TestContext = struct {
737769 }
738770 var ok = true;
739771 if (case.expect_exact) {
740 var err_iter = ErrLineIter.init(result.stderr);
772 var err_iter = std.mem.split(result.stderr, "\n");
741773 var i: usize = 0;
742774 ok = while (err_iter.next()) |line| : (i += 1) {
743775 if (i >= case_error_list.len) break false;
......@@ -940,8 +972,10 @@ pub const TestContext = struct {
940972 std.mem.eql(u8, case_msg.src.src_path, actual_msg.src_path);
941973
942974 if (src_path_ok and
943 actual_msg.line == case_msg.src.line and
944 actual_msg.column == case_msg.src.column and
975 (case_msg.src.line == std.math.maxInt(u32) or
976 actual_msg.line == case_msg.src.line) and
977 (case_msg.src.column == std.math.maxInt(u32) or
978 actual_msg.column == case_msg.src.column) and
945979 std.mem.eql(u8, case_msg.src.msg, actual_msg.msg) and
946980 case_msg.src.kind == .@"error")
947981 {
......@@ -978,8 +1012,10 @@ pub const TestContext = struct {
9781012 }
9791013 if (ex_tag != .src) continue;
9801014
981 if (actual_msg.line == case_msg.src.line and
982 actual_msg.column == case_msg.src.column and
1015 if ((case_msg.src.line == std.math.maxInt(u32) or
1016 actual_msg.line == case_msg.src.line) and
1017 (case_msg.src.column == std.math.maxInt(u32) or
1018 actual_msg.column == case_msg.src.column) and
9831019 std.mem.eql(u8, case_msg.src.msg, actual_msg.msg) and
9841020 case_msg.src.kind == .note)
9851021 {
......@@ -1162,19 +1198,3 @@ fn dumpArgs(argv: []const []const u8) void {
11621198}
11631199
11641200const tmp_src_path = "tmp.zig";
1165
1166const ErrLineIter = struct {
1167 lines: std.mem.SplitIterator,
1168
1169 fn init(input: []const u8) ErrLineIter {
1170 return ErrLineIter{ .lines = std.mem.split(input, "\n") };
1171 }
1172
1173 fn next(self: *ErrLineIter) ?[]const u8 {
1174 while (self.lines.next()) |line| {
1175 if (std.mem.indexOf(u8, line, tmp_src_path) != null)
1176 return line;
1177 }
1178 return null;
1179 }
1180};
test/compile_errors.zig+9-1
......@@ -2,6 +2,14 @@ const std = @import("std");
22const TestContext = @import("../src/test.zig").TestContext;
33
44pub fn addCases(ctx: *TestContext) !void {
5 ctx.exeErrStage1("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
513 ctx.objErrStage1("lazy pointer with undefined element type",
614 \\export fn foo() void {
715 \\ comptime var T: type = undefined;
......@@ -6120,7 +6128,7 @@ pub fn addCases(ctx: *TestContext) !void {
61206128 \\
61216129 \\export fn entry() usize { return @sizeOf(@TypeOf(resource)); }
61226130 , &[_][]const u8{
6123 "tmp.zig:1:29: error: unable to find 'bogus.txt'",
6131 "tmp.zig:1:29: error: unable to find '",
61246132 });
61256133
61266134 ctx.objErrStage1("non-const expression in struct literal outside function",