From 9e11feb4dfd4ee702162c80e996f25caa5da0a87 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Thu, 29 Oct 2026 22:38:44 -0400 Subject: [PATCH] test/error_traces: convert to new runner --- build.zig | 19 +- lib/std/process.zig | 10 +- test/Runner.zig | 847 +++++++++++------- test/error_traces.zig | 684 -------------- ...k_from_inline_loop_pops_error_return_trace | 27 + test/error_traces/catch_and_re-throw_error | 24 + test/error_traces/continue_in_while_loop | 25 + ...s_trace_preserved_for_duration_of_the_call | 38 + ..._has_trace_preserved_for_duration_of_block | 44 + .../error_union_switch_with_call_operand | 29 + ...ed_in_var_do_not_contribute_to_error_trace | 26 + .../for_loop_pops_error_return_trace | 26 + ..._in_for_loop_pops_stale_error_return_trace | 27 + ...n_while_loop_pops_stale_error_return_trace | 29 + test/error_traces/matrix.zon | 318 +++++++ .../non-error_return_pops_error_trace | 28 + test/error_traces/return | 17 + test/error_traces/trace_through_inline_call | 31 + .../trace_through_inline_call_windows | 31 + test/error_traces/try_return | 24 + .../error_traces/try_return_from_within_catch | 33 + .../try_return_from_within_if-else | 33 + .../try_return_handled_catch_or_if-else | 28 + test/error_traces/try_try_return_return | 38 + .../while_loop_pops_error_return_trace | 28 + test/src/ErrorTrace.zig | 505 ----------- test/tests.zig | 25 - 27 files changed, 1427 insertions(+), 1567 deletions(-) delete mode 100644 test/error_traces.zig create mode 100644 test/error_traces/break_from_inline_loop_pops_error_return_trace create mode 100644 test/error_traces/catch_and_re-throw_error create mode 100644 test/error_traces/continue_in_while_loop create mode 100644 test/error_traces/error_passed_to_function_has_its_trace_preserved_for_duration_of_the_call create mode 100644 test/error_traces/error_stored_in_const_has_trace_preserved_for_duration_of_block create mode 100644 test/error_traces/error_union_switch_with_call_operand create mode 100644 test/error_traces/errors_stored_in_var_do_not_contribute_to_error_trace create mode 100644 test/error_traces/for_loop_pops_error_return_trace create mode 100644 test/error_traces/implicit_continue_in_for_loop_pops_stale_error_return_trace create mode 100644 test/error_traces/implicit_continue_in_while_loop_pops_stale_error_return_trace create mode 100644 test/error_traces/matrix.zon create mode 100644 test/error_traces/non-error_return_pops_error_trace create mode 100644 test/error_traces/return create mode 100644 test/error_traces/trace_through_inline_call create mode 100644 test/error_traces/trace_through_inline_call_windows create mode 100644 test/error_traces/try_return create mode 100644 test/error_traces/try_return_from_within_catch create mode 100644 test/error_traces/try_return_from_within_if-else create mode 100644 test/error_traces/try_return_handled_catch_or_if-else create mode 100644 test/error_traces/try_try_return_return create mode 100644 test/error_traces/while_loop_pops_error_return_trace delete mode 100644 test/src/ErrorTrace.zig diff --git a/build.zig b/build.zig index 67b1fe2b59..5a13e67162 100644 --- a/build.zig +++ b/build.zig @@ -679,21 +679,6 @@ pub fn build(b: *std.Build) !void { .skip_llvm = skip_llvm, .skip_libc = skip_libc, })); - test_step.dependOn(tests.addErrorTraceTests(b, .{ - .test_filters = test_filters, - .test_target_filters = test_target_filters, - .test_extra_targets = test_extra_targets, - .optimize_modes = optimize_modes, - .skip_non_native = skip_non_native, - .skip_freebsd = skip_freebsd, - .skip_netbsd = skip_netbsd, - .skip_openbsd = skip_openbsd, - .skip_windows = skip_windows, - .skip_darwin = skip_darwin, - .skip_linux = skip_linux, - .skip_llvm = skip_llvm, - .skip_libc = skip_libc, - })); test_step.dependOn(tests.addCliTests(b)); try addWasiUpdateStep(b, version); @@ -811,6 +796,10 @@ pub fn build(b: *std.Build) !void { try tests.addRunnerTests(b, runner, test_errors_step, "test/errors", @import("test/errors/matrix.zon"), runner_opts); test_step.dependOn(test_errors_step); + const test_error_traces_step = b.step("test-error-traces", "Run the error trace tests"); + try tests.addRunnerTests(b, runner, test_error_traces_step, "test/error_traces", @import("test/error_traces/matrix.zon"), runner_opts); + test_step.dependOn(test_error_traces_step); + const test_incremental_step = b.step("test-incremental", "Run the incremental compilation tests"); try tests.addRunnerTests(b, runner, test_incremental_step, "test/incremental", @import("test/incremental/matrix.zon"), runner_opts); test_step.dependOn(test_incremental_step); diff --git a/lib/std/process.zig b/lib/std/process.zig index 68ca72aa5f..c31151b56f 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -491,6 +491,10 @@ pub const RunOptions = struct { /// The child's progress tree will be grafted into the parent's progress tree, /// by substituting this node with the child's root node. progress_node: std.Progress.Node = std.Progress.Node.none, + + inherit_dirs: []const Dir = &.{}, + inherit_files: []const File = &.{}, + /// Windows-only. Sets the CREATE_NO_WINDOW flag in CreateProcess. create_no_window: bool = true, timeout: Io.Timeout = .none, @@ -508,15 +512,19 @@ pub fn run(gpa: Allocator, io: Io, options: RunOptions) RunError!RunResult { var child = try spawn(io, .{ .exe = options.exe, .argv = options.argv, + .cwd = options.cwd, .environ_map = options.environ_map, .expand_arg0 = options.expand_arg0, .progress_node = options.progress_node, - .create_no_window = options.create_no_window, .stdin = .ignore, .stdout = .pipe, .stderr = .pipe, + .inherit_dirs = options.inherit_dirs, + .inherit_files = options.inherit_files, + + .create_no_window = options.create_no_window, }); defer child.kill(io); diff --git a/test/Runner.zig b/test/Runner.zig index a0faaf72d2..1d9352ec7f 100644 --- a/test/Runner.zig +++ b/test/Runner.zig @@ -113,6 +113,13 @@ //! //! #skip x86_64-linux-none-baseline-whole-selfhosted-new-pic //! +//! The require command allows reducing the matrix for a given test based on the given condition. +//! These must appear at the top of the file before any non-comment commands, for example: +//! +//! #require windows +//! +//! * `windows` requires a target os tag of `.uefi` or `.windows`. +//! //! The zig command defines the compiler command line arguments that are specific to this case, //! where the command argument chooses the compiler sub-command and the contents contains options //! that can override options selected by the target, for example: @@ -168,13 +175,21 @@ //! Empty contents succeed when the compiler produces no compile errors. //! //! The stdout check succeeds when running the binary produced by the compiler, possibly with an -//! executor like qemu, exits with exit code 0 and produces the given standard output. The contents -//! are a sequence of matches separated by `#...` that are expected to appear in the order given, -//! with arbitrary contents. +//! executor like qemu, exits with code 0 and produces the given standard output. The contents are +//! a sequence of matches separated by `#...` that are expected to appear in the order given, with +//! arbitrary contents. //! //! The exit check succeeds when running the binary as the stdout check does exits with the given //! exit code. The contents are the decimal exit code in ascii with no trailing newline. //! +//! The error-trace check succeed when running the binary as the stdout check does exits with 1, +//! produces no standard output, and produces the given standard error. The contents support a few +//! useful patterns that are not matched exactly: +//! +//! * `test.zig:` matches a path ending with `test.zig` as the final component followed by a `:`. +//! * `0x?` matches any hex address. +//! * ` (test*)` at the end of a line matches either ` (test)`, ` (test.zig)`, or ` (test_zcu.obj)`. +//! //! The llvm check succeeds when the llvm backend produces the given LLVM IR. The contents support //! a variety of useful patterns that are not matched exactly: //! @@ -551,12 +566,12 @@ const Args = struct { gpa: std.mem.Allocator, target: []const u8, skipped: bool, - ) std.mem.Allocator.Error!void { + ) (std.mem.Allocator.Error || error{DuplicatedTarget})!void { try args.target_bytes.ensureUnusedCapacity(gpa, target.len + 1); const gop = try args.targets.getOrPutAdapted(gpa, target, TargetAdapter{ .bytes = args.target_bytes.items, }); - assert(!gop.found_existing); + if (gop.found_existing) return error.DuplicatedTarget; gop.key_ptr.* = @intCast(args.target_bytes.items.len); gop.value_ptr.* = .{ .skipped = skipped }; args.target_bytes.appendSliceAssumeCapacity(target); @@ -661,6 +676,7 @@ pub fn runServer(runner: *Runner) ProtocolError!void { .@"--gdb" = .file, .@"--lldb" = .file, }); + var duplicated_target = false; var input_dir: std.zig.Server.Message.InputDir = .cwd; var args_body_offset: usize = 0; while (args_body.len - args_body_offset > 0) { @@ -685,7 +701,20 @@ pub fn runServer(runner: *Runner) ProtocolError!void { state = .@"--target"; continue; } else if (std.mem.cutPrefix(u8, string, "--target=")) |target| { - try runner.args.addTarget(runner.gpa, target, false); + runner.args.addTarget( + runner.gpa, + target, + false, + ) catch |err| switch (err) { + error.OutOfMemory => |e| return e, + error.DuplicatedTarget => switch (runner.fail( + "duplicated target {q}", + .{target}, + )) { + error.Canceled, error.OutOfMemory => |e| return e, + error.AlreadyReported => duplicated_target = true, + }, + }; } else if (std.mem.eql(u8, string, "--skipped-target")) { state = .@"--skipped-target"; continue; @@ -694,7 +723,20 @@ pub fn runServer(runner: *Runner) ProtocolError!void { string, "--skipped-target=", )) |target| { - try runner.args.addTarget(runner.gpa, target, true); + runner.args.addTarget( + runner.gpa, + target, + true, + ) catch |err| switch (err) { + error.OutOfMemory => |e| return e, + error.DuplicatedTarget => switch (runner.fail( + "duplicated target {q}", + .{target}, + )) { + error.Canceled, error.OutOfMemory => |e| return e, + error.AlreadyReported => duplicated_target = true, + }, + }; } else if (std.mem.eql(u8, string, "--libc-runtimes")) { state = .@"--libc-runtimes"; continue; @@ -721,12 +763,38 @@ pub fn runServer(runner: *Runner) ProtocolError!void { string, ) orelse return runner.fail("invalid log level {q}", .{string}); }, - .@"--target" => try runner.args.addTarget(runner.gpa, string, false), - .@"--skipped-target" => try runner.args.addTarget( - runner.gpa, - string, - true, - ), + .@"--target" => { + runner.args.addTarget( + runner.gpa, + string, + false, + ) catch |err| switch (err) { + error.OutOfMemory => |e| return e, + error.DuplicatedTarget => switch (runner.fail( + "duplicated target {q}", + .{string}, + )) { + error.Canceled, error.OutOfMemory => |e| return e, + error.AlreadyReported => duplicated_target = true, + }, + }; + }, + .@"--skipped-target" => { + runner.args.addTarget( + runner.gpa, + string, + true, + ) catch |err| switch (err) { + error.OutOfMemory => |e| return e, + error.DuplicatedTarget => switch (runner.fail( + "duplicated target {q}", + .{string}, + )) { + error.Canceled, error.OutOfMemory => |e| return e, + error.AlreadyReported => duplicated_target = true, + }, + }; + }, else => return runner.fail( "\"{t}\" expected {t}, got {t}", .{ state, state_expected.get(state), arg }, @@ -901,6 +969,7 @@ pub fn runServer(runner: *Runner) ProtocolError!void { } state = .positional; } + if (duplicated_target) return error.AlreadyReported; }, .query_test_metadata => runner.server.serveTestMetadata(.{ .names = runner.args.targets.keys(), @@ -1170,9 +1239,17 @@ pub const DelimitedReader = struct { } }; +const newline: []const Pattern = &.{ + .{ .@"?" = &.{ + .{ .literal = "\r" }, + } }, + .{ .literal = "\n" }, +}; + const Update = struct { runner: *Runner, test_index: u32, + target: Compiler.Target, skipped: ?bool, invalid_skip: bool, manifest_sr: SourceReader, @@ -1393,6 +1470,137 @@ fn testOne(runner: *Runner, test_index: u32) (error{ServerWriteFailed} || TestEr var update: Update = .{ .runner = runner, .test_index = test_index, + .target = target: { + var component_it = std.mem.splitBackwardsScalar(u8, target_query, '-'); + var component = component_it.first(); + const strip: Target.Strip = + if (std.meta.stringToEnum(Target.Strip, component)) |strip| strip: { + component = component_it.next() orelse return runner.fail( + "target {q} missing components before strip", + .{target_query}, + ); + break :strip strip; + } else .default; + const link_libc: Target.LinkLibc = + if (std.meta.stringToEnum(Target.LinkLibc, component)) |link_libc| link_libc: { + component = component_it.next() orelse return runner.fail( + "target {q} missing components before link libc", + .{target_query}, + ); + break :link_libc link_libc; + } else .default; + const linker: Target.Linker = + if (std.meta.stringToEnum(Target.Linker, component)) |linker| linker: { + component = component_it.next() orelse return runner.fail( + "target {q} missing components before linker", + .{target_query}, + ); + break :linker linker; + } else .default; + const pic: Target.Pic = if (std.meta.stringToEnum(Target.Pic, component)) |pic| pic: { + component = component_it.next() orelse return runner.fail( + "target {q} missing components before pic", + .{target_query}, + ); + break :pic pic; + } else .default; + const optimize = std.meta.stringToEnum(std.lang.Optimize, component) orelse + return runner.fail("target {q} unknown optimize {q}", .{ target_query, component }); + component = component_it.next() orelse return runner.fail( + "target {q} missing components before optimize", + .{target_query}, + ); + const backend = std.meta.stringToEnum(Target.Backend, component) orelse + return runner.fail("target {q} unknown backend {q}", .{ target_query, component }); + component = component_it.next() orelse return runner.fail( + "target {q} missing components before backend", + .{target_query}, + ); + const cache_mode = std.meta.stringToEnum(Target.CacheMode, component) orelse + return runner.fail("target {q} unknown cache mode {q}", .{ + target_query, component, + }); + const triple_cpu = component_it.rest(); + const triple, const cpu = triple_cpu: { + var cpu_pos: usize = 0; + for (0..3) |_| { + const component_start = cpu_pos; + const dash_pos = std.mem.findScalarPos(u8, triple_cpu, cpu_pos, '-') orelse + return runner.fail("target {q} missing cpu component", .{target_query}); + cpu_pos = dash_pos + 1; + if (std.mem.eql(u8, triple_cpu[component_start..dash_pos], "native")) break; + } + break :triple_cpu .{ triple_cpu[0 .. cpu_pos - 1], triple_cpu[cpu_pos..] }; + }; + var query_parse_diags: std.Target.Query.ParseOptions.Diagnostics = undefined; + const resolved = std.zig.system.resolveTargetQuery( + runner.io, + std.Target.Query.parse(.{ + .arch_os_abi = triple, + .cpu_features = cpu, + .object_format = switch (backend) { + .selfhosted, .llvm => null, + .cbe => "c", + }, + .diagnostics = &query_parse_diags, + }) catch |err| switch (err) { + error.UnknownArchitecture => return runner.fail( + "unable to resolve target {q}: unknown architecture: {q}", + .{ target_query, query_parse_diags.unknown_architecture_name.? }, + ), + error.MissingOperatingSystem => return runner.fail( + "unable to resolve target {q}: missing operating system", + .{target_query}, + ), + error.UnknownOperatingSystem => return runner.fail( + "unable to resolve target {q}: unknown operating system: {q}", + .{ target_query, query_parse_diags.os_name.? }, + ), + error.InvalidOperatingSystemVersion => return runner.fail( + "unable to resolve target {q}: invalid operating system version", + .{target_query}, + ), + error.UnknownApplicationBinaryInterface => return runner.fail( + "unable to resolve target {q}: unknown abi", + .{target_query}, + ), + error.InvalidAbiVersion => return runner.fail( + "unable to resolve target {q}: invalid abi version", + .{target_query}, + ), + error.UnexpectedExtraField => unreachable, // validated above + error.UnknownCpuModel => return runner.fail( + "unable to resolve target {q}: unknown cpu: {q}", + .{ target_query, query_parse_diags.cpu_name.? }, + ), + error.UnknownCpuFeature => return runner.fail( + "unable to resolve target {q}: unknown cpu feature: {q}", + .{ target_query, query_parse_diags.unknown_feature_name.? }, + ), + error.UnknownObjectFormat => return runner.fail( + "unable to resolve target {q}: unknown object format", + .{target_query}, + ), + }, + ) catch |err| switch (err) { + error.Canceled => |e| return e, + else => |e| return runner.fail( + "unable to resolve target {q}: {t}", + .{ triple, e }, + ), + }; + break :target .{ + .triple = triple, + .resolved = resolved, + .cache_mode = cache_mode, + .backend = backend, + .optimize = optimize, + .pic = pic, + .linker = linker, + .link_libc = link_libc, + .strip = strip, + }; + }, .skipped = null, .invalid_skip = false, .manifest_sr = .init( @@ -1465,7 +1673,7 @@ fn testOne(runner: *Runner, test_index: u32) (error{ServerWriteFailed} || TestEr const arg = switch (cmd) { .comment, .update => null, - .skip, .zig, .write, .delete, .check => line_it.next() orelse + .skip, .require, .zig, .write, .delete, .check => line_it.next() orelse return update.manifest_sr.fail(line_it.rest()[0..0], "missing argument", .{}), }; @@ -1493,9 +1701,11 @@ fn testOne(runner: *Runner, test_index: u32) (error{ServerWriteFailed} || TestEr } switch (cmd) { .comment, .zig, .write, .update, .check => {}, - .skip, .delete => return update.manifest_sr.fail(contents, "unexpected contents {q}", .{ + .skip, .require, .delete => return update.manifest_sr.fail( contents, - }), + "unexpected contents {q}", + .{contents}, + ), } contents: switch (contents[0]) { else => return update.manifest_sr.fail(contents, "invalid contents syntax", .{}), @@ -1618,7 +1828,8 @@ fn testOne(runner: *Runner, test_index: u32) (error{ServerWriteFailed} || TestEr try comp.exit(); } } -const Command = enum { comment, skip, zig, write, delete, update, check }; +const Command = enum { comment, skip, require, zig, write, delete, update, check }; +const Requirement = enum { windows }; fn handleCommand( runner: *Runner, update: *Update, @@ -1638,10 +1849,10 @@ fn handleCommand( switch (cmd) { .comment => {}, - .skip => if (update.skipped == false) return update.manifest_sr.fail( + .skip, .require => if (update.skipped == false) return update.manifest_sr.fail( cmd_src_loc, - "\"#skip\" must appear before other commands", - .{}, + "\"#{t}\" must appear before other commands", + .{cmd}, ), else => { if (update.invalid_skip) return error.AlreadyReported; @@ -1682,143 +1893,33 @@ fn handleCommand( update.invalid_skip = true; } else return error.SkipTest; }, + .require => { + const negate, const requirement = if (std.mem.cutPrefix(u8, arg.?, "!")) |requirement| + .{ true, requirement } + else + .{ false, arg.? }; + const satisfied = switch (std.meta.stringToEnum(Requirement, requirement) orelse + return update.manifest_sr.fail(requirement, "unknown requirement", .{})) { + .windows => switch (update.target.resolved.os.tag) { + else => false, + .uefi, .windows => true, + }, + }; + if (switch (negate) { + false => !satisfied, + true => satisfied, + }) { + std.log.info("skipped due to unsatisfied requirement {q}", .{arg.?}); + return error.SkipTest; + } + }, .zig => runner.spawnCompiler( &update.compiler, + update.target, update.src_dir, std.meta.stringToEnum(Compiler.Command, arg.?) orelse return update.manifest_sr.fail(arg.?, "unknown compiler command", .{}), contents_r, - target: { - const target_query = runner.args.getTarget(update.test_index); - var component_it = std.mem.splitBackwardsScalar(u8, target_query, '-'); - var component = component_it.first(); - const strip: Target.Strip = - if (std.meta.stringToEnum(Target.Strip, component)) |strip| strip: { - component = component_it.next() orelse return update.fail( - "target {q} missing components before strip", - .{target_query}, - ); - break :strip strip; - } else .default; - const link_libc: Target.LinkLibc = - if (std.meta.stringToEnum(Target.LinkLibc, component)) |link_libc| link_libc: { - component = component_it.next() orelse return update.fail( - "target {q} missing components before link libc", - .{target_query}, - ); - break :link_libc link_libc; - } else .default; - const linker: Target.Linker = - if (std.meta.stringToEnum(Target.Linker, component)) |linker| linker: { - component = component_it.next() orelse return update.fail( - "target {q} missing components before linker", - .{target_query}, - ); - break :linker linker; - } else .default; - const pic: Target.Pic = if (std.meta.stringToEnum(Target.Pic, component)) |pic| pic: { - component = component_it.next() orelse return update.fail( - "target {q} missing components before pic", - .{target_query}, - ); - break :pic pic; - } else .default; - const optimize = std.meta.stringToEnum(std.lang.Optimize, component) orelse - return update.fail("target {q} unknown optimize {q}", .{ target_query, component }); - component = component_it.next() orelse return update.fail( - "target {q} missing components before optimize", - .{target_query}, - ); - const backend = std.meta.stringToEnum(Target.Backend, component) orelse - return update.fail("target {q} unknown backend {q}", .{ target_query, component }); - component = component_it.next() orelse return update.fail( - "target {q} missing components before backend", - .{target_query}, - ); - const cache_mode = std.meta.stringToEnum(Target.CacheMode, component) orelse - return update.fail("target {q} unknown cache mode {q}", .{ - target_query, component, - }); - const triple_cpu = component_it.rest(); - const triple, const cpu = triple_cpu: { - var cpu_pos: usize = 0; - for (0..3) |_| { - const component_start = cpu_pos; - const dash_pos = std.mem.findScalarPos(u8, triple_cpu, cpu_pos, '-') orelse - return update.fail("target {q} missing cpu component", .{target_query}); - cpu_pos = dash_pos + 1; - if (std.mem.eql(u8, triple_cpu[component_start..dash_pos], "native")) break; - } - break :triple_cpu .{ triple_cpu[0 .. cpu_pos - 1], triple_cpu[cpu_pos..] }; - }; - var query_parse_diags: std.Target.Query.ParseOptions.Diagnostics = undefined; - break :target .{ - .triple = triple, - .resolved = std.zig.system.resolveTargetQuery( - runner.io, - std.Target.Query.parse(.{ - .arch_os_abi = triple, - .cpu_features = cpu, - .object_format = switch (backend) { - .selfhosted, .llvm => null, - .cbe => "c", - }, - .diagnostics = &query_parse_diags, - }) catch |err| switch (err) { - error.UnknownArchitecture => return update.fail( - "unable to resolve target {q}: unknown architecture: {q}", - .{ target_query, query_parse_diags.unknown_architecture_name.? }, - ), - error.MissingOperatingSystem => return update.fail( - "unable to resolve target {q}: missing operating system", - .{target_query}, - ), - error.UnknownOperatingSystem => return update.fail( - "unable to resolve target {q}: unknown operating system: {q}", - .{ target_query, query_parse_diags.os_name.? }, - ), - error.InvalidOperatingSystemVersion => return update.fail( - "unable to resolve target {q}: invalid operating system version", - .{target_query}, - ), - error.UnknownApplicationBinaryInterface => return update.fail( - "unable to resolve target {q}: unknown abi", - .{target_query}, - ), - error.InvalidAbiVersion => return update.fail( - "unable to resolve target {q}: invalid abi version", - .{target_query}, - ), - error.UnexpectedExtraField => unreachable, // validated above - error.UnknownCpuModel => return update.fail( - "unable to resolve target {q}: unknown cpu: {q}", - .{ target_query, query_parse_diags.cpu_name.? }, - ), - error.UnknownCpuFeature => return update.fail( - "unable to resolve target {q}: unknown cpu feature: {q}", - .{ target_query, query_parse_diags.unknown_feature_name.? }, - ), - error.UnknownObjectFormat => return update.fail( - "unable to resolve target {q}: unknown object format", - .{target_query}, - ), - }, - ) catch |err| switch (err) { - error.Canceled => |e| return e, - else => |e| return update.fail( - "unable to resolve target {q}: {t}", - .{ triple, e }, - ), - }, - .cache_mode = cache_mode, - .backend = backend, - .optimize = optimize, - .pic = pic, - .linker = linker, - .link_libc = link_libc, - .strip = strip, - }; - }, ) catch |err| switch (err) { error.Canceled, error.OutOfMemory, error.AlreadyReported, error.SkipTest => |e| return e, error.ReadFailed => return error.ContentsReadFailed, @@ -1939,122 +2040,72 @@ fn handleCommand( error.OutOfMemory => |e| return e, error.EndOfStream => return update.fail("error bundle truncated", .{}), }; - switch (check) { - .errors => { - var error_aw: std.Io.Writer.Allocating = .init(runner.gpa); - defer error_aw.deinit(); - eb.renderToWriter(.{ - .include_reference_trace = false, - .include_source_line = false, - .include_log_text = true, - }, &error_aw.writer) catch |err| switch (err) { - error.WriteFailed => return error.OutOfMemory, - }; - try update.checkPattern("errors", expected, error_aw.written(), &.{ - .{ "\n", .anon, newline }, - .{ "/", .anon, &.{ - .{ .any = &.{ - .{ '/', '/' }, - .{ '\\', '\\' }, + if (check == .errors) { + var error_aw: std.Io.Writer.Allocating = .init(runner.gpa); + defer error_aw.deinit(); + eb.renderToWriter(.{ + .include_reference_trace = false, + .include_source_line = false, + .include_log_text = true, + }, &error_aw.writer) catch |err| switch (err) { + error.WriteFailed => return error.OutOfMemory, + }; + try update.checkPattern("errors", expected, error_aw.written(), &.{ + .{ "\n", .anon, newline }, + .{ "/", .anon, &.{ + .{ .any = &.{ + .{ '/', '/' }, + .{ '\\', '\\' }, + } }, + } }, + .{ "builtin.zig:?:?:", .anon, &.{ + .{ .none = &.{ + .{ '\n', '\n' }, + .{ '\r', '\r' }, + .{ ' ', ' ' }, + .{ ':', ':' }, + } }, + .{ .@"*" = &.{ + .{ .none = &.{ + .{ '\n', '\n' }, + .{ '\r', '\r' }, } }, } }, - .{ "builtin.zig:?:?:", .anon, &.{ - .{ .@"?" = &.{ - .{ .none = &.{ - .{ '\n', '\n' }, - .{ '\r', '\r' }, - .{ ' ', ' ' }, - .{ ':', ':' }, - } }, - .{ .@"*" = &.{ - .{ .none = &.{ - .{ '\n', '\n' }, - .{ '\r', '\r' }, - } }, - } }, - } }, - .{ .any = &.{ - .{ '/', '/' }, - .{ '\\', '\\' }, - } }, - .{ .literal = "b" }, - .{ .any = &.{ - .{ '/', '/' }, - .{ '\\', '\\' }, - } }, - .{ .@"+" = &.{ - .{ .any = &.{ - .{ '0', '9' }, - .{ 'a', 'f' }, - } }, - } }, - .{ .any = &.{ - .{ '/', '/' }, - .{ '\\', '\\' }, - } }, - .{ .literal = "builtin.zig:" }, - .{ .@"+" = &.{ - .{ .any = &.{ - .{ '0', '9' }, - } }, - } }, - .{ .literal = ":" }, - .{ .@"+" = &.{ - .{ .any = &.{ - .{ '0', '9' }, - } }, - } }, - .{ .literal = ":" }, + .{ .any = &.{ + .{ '/', '/' }, + .{ '\\', '\\' }, } }, - .{ "lib/std/**.zig:?:?: note: called inline here\n", .anon, &.{ - .{ .@"*" = &[_]Pattern{ - .{ .@"?" = &.{ - .{ .none = &.{ - .{ '\n', '\n' }, - .{ '\r', '\r' }, - .{ ' ', ' ' }, - .{ ':', ':' }, - } }, - .{ .@"*" = &.{ - .{ .none = &.{ - .{ '\n', '\n' }, - .{ '\r', '\r' }, - } }, - } }, - } }, - .{ .literal = "lib" }, - .{ .any = &.{ - .{ '/', '/' }, - .{ '\\', '\\' }, - } }, - .{ .literal = "std" }, - .{ .any = &.{ - .{ '/', '/' }, - .{ '\\', '\\' }, - } }, - .{ .@"+" = &.{ - .{ .none = &.{ - .{ '\n', '\n' }, - .{ '\r', '\r' }, - .{ ':', ':' }, - } }, - } }, - .{ .literal = ".zig:" }, - .{ .@"+" = &.{ - .{ .any = &.{ - .{ '0', '9' }, - } }, - } }, - .{ .literal = ":" }, - .{ .@"+" = &.{ - .{ .any = &.{ - .{ '0', '9' }, - } }, - } }, - .{ .literal = ": note: called inline here" }, - } ++ newline }, + .{ .literal = "b" }, + .{ .any = &.{ + .{ '/', '/' }, + .{ '\\', '\\' }, } }, - .{ "lib/std/**.zig:?:?:", .anon, &.{ + .{ .@"+" = &.{ + .{ .any = &.{ + .{ '0', '9' }, + .{ 'a', 'f' }, + } }, + } }, + .{ .any = &.{ + .{ '/', '/' }, + .{ '\\', '\\' }, + } }, + .{ .literal = "builtin.zig:" }, + .{ .@"+" = &.{ + .{ .any = &.{ + .{ '0', '9' }, + } }, + } }, + .{ .literal = ":" }, + .{ .@"+" = &.{ + .{ .any = &.{ + .{ '0', '9' }, + } }, + } }, + .{ .literal = ":" }, + } }, + .{ "lib/std/**.zig:?:?: note: called inline here\n", .anon, &.{ + .{ .@"*" = &[_]Pattern{ .{ .@"?" = &.{ .{ .none = &.{ .{ '\n', '\n' }, @@ -2098,17 +2149,62 @@ fn handleCommand( .{ '0', '9' }, } }, } }, - .{ .literal = ":" }, + .{ .literal = ": note: called inline here" }, + } ++ newline }, + } }, + .{ "lib/std/**.zig:?:?:", .anon, &.{ + .{ .@"?" = &.{ + .{ .none = &.{ + .{ '\n', '\n' }, + .{ '\r', '\r' }, + .{ ' ', ' ' }, + .{ ':', ':' }, + } }, + .{ .@"*" = &.{ + .{ .none = &.{ + .{ '\n', '\n' }, + .{ '\r', '\r' }, + } }, + } }, } }, - }); - }, - .stdout, .exit, .llvm, .gdb, .lldb => if (eb.errorMessageCount() > 0) { - eb.renderToStderr(runner.io, .{}, .auto) catch |err| switch (err) { - error.Canceled => {}, - else => {}, - }; - return update.fail("unexpected compile errors", .{}); - }, + .{ .literal = "lib" }, + .{ .any = &.{ + .{ '/', '/' }, + .{ '\\', '\\' }, + } }, + .{ .literal = "std" }, + .{ .any = &.{ + .{ '/', '/' }, + .{ '\\', '\\' }, + } }, + .{ .@"+" = &.{ + .{ .none = &.{ + .{ '\n', '\n' }, + .{ '\r', '\r' }, + .{ ':', ':' }, + } }, + } }, + .{ .literal = ".zig:" }, + .{ .@"+" = &.{ + .{ .any = &.{ + .{ '0', '9' }, + } }, + } }, + .{ .literal = ":" }, + .{ .@"+" = &.{ + .{ .any = &.{ + .{ '0', '9' }, + } }, + } }, + .{ .literal = ":" }, + } }, + }); + } else if (eb.errorMessageCount() > 0) { + eb.renderToStderr(runner.io, .{}, .auto) catch |err| switch (err) { + error.Canceled => {}, + else => {}, + }; + return update.fail("unexpected compile errors", .{}); } comp.state = .idle; break :cmd; @@ -2133,10 +2229,10 @@ fn handleCommand( fn spawnCompiler( runner: *Runner, compiler: *?Compiler, + target: Compiler.Target, src_dir: std.Io.Dir, command: Compiler.Command, args_r: *std.Io.Reader, - target: Compiler.Target, ) (std.Io.Reader.ShortError || TestError)!void { const gpa = runner.gpa; const arena = runner.arena.allocator(); @@ -2409,7 +2505,7 @@ const Compiler = struct { ); } - const Check = enum { errors, stdout, exit, llvm, gdb, lldb }; + const Check = enum { errors, stdout, exit, @"error-trace", llvm, gdb, lldb }; fn checkSuccess( comp: *Compiler, update: *const Update, @@ -2466,6 +2562,11 @@ const Compiler = struct { const script_path, const script_file, var script_fw = script: switch (check) { .errors => unreachable, .stdout, .exit => .{ undefined, null, undefined }, + .@"error-trace" => { + _ = environ_map.swapRemove("CLICOLOR_FORCE"); + try environ_map.put("NO_COLOR", "1"); // affects output formatting + break :script .{ undefined, null, undefined }; + }, .llvm => { var emit_fr = emit_file.reader(io, &.{}); const actual = emit_fr.interface.readAllocAll(arena, @intCast( @@ -2885,7 +2986,10 @@ const Compiler = struct { const result = std.process.run(gpa, io, .{ .exe = exe: switch (check) { .errors, .llvm => unreachable, - .stdout, .exit => if (use_executor) .search else .{ .file = emit_file }, + .stdout, .exit, .@"error-trace" => if (use_executor) + .search + else + .{ .file = emit_file }, .gdb, .lldb => { if (use_executor) { std.log.info( @@ -2948,6 +3052,11 @@ const Compiler = struct { .cwd = .{ .dir = update.src_dir }, .environ_map = &environ_map, .progress_node = run_prog_node, + .inherit_files = switch (check) { + .errors, .llvm => unreachable, + .stdout, .exit, .gdb, .lldb => &.{}, + .@"error-trace" => &.{emit_file}, + }, }) catch |err| if (use_executor) { // Chances are the foreign executor isn't available. Skip this evaluation. std.log.info( @@ -2967,7 +3076,7 @@ const Compiler = struct { .errors, .llvm => unreachable, .stdout => { if (code != 0) return update.fail( - "generated executable {q} failed with exit code {d}", + "{q} exited with code {d}, expected code 0", .{ emit_path, code }, ); try update.checkPattern("stdout", expected, result.stdout, &.{ @@ -2979,10 +3088,60 @@ const Compiler = struct { const actual = std.mem.print(&actual_buffer, "{d}", .{code}) catch unreachable; try update.checkPattern("exit code", expected, actual, &.{}); }, + .@"error-trace" => { + if (code != 1) return update.fail( + "{q} exited with code {d}, expected code 1", + .{ emit_path, code }, + ); + try update.checkPattern("stderr", expected, result.stderr, &.{ + .{ "\n", .anon, newline }, + .{ "test.zig:", .anon, &.{ + .{ .none = &.{ + .{ '\n', '\n' }, + .{ '\r', '\r' }, + .{ ' ', ' ' }, + .{ ':', ':' }, + } }, + .{ .@"*" = &.{ + .{ .none = &.{ + .{ '\n', '\n' }, + .{ '\r', '\r' }, + } }, + } }, + .{ .any = &.{ + .{ '/', '/' }, + .{ '\\', '\\' }, + } }, + .{ .literal = "test.zig:" }, + } }, + .{ "0x?", .anon, &.{ + .{ .literal = "0x" }, + .{ .@"+" = &.{ + .{ .any = &.{ + .{ '0', '9' }, + .{ 'a', 'f' }, + } }, + } }, + } }, + .{ " (test*)", .anon, &.{ + .{ .literal = " (test" }, + .{ .@"?" = &.{ + .{ .@"|" = &.{ &.{ + .{ .literal = ".zig" }, + }, &.{ + .{ .literal = "_zcu.obj" }, + } } }, + } }, + .{ .literal = ")" }, + } }, + }); + try update.checkPattern("stdout", "", result.stdout, &.{}); + return; // skip stderr check below + }, .gdb, .lldb => { if (code != debug_success) return update.fail( - "debugging {q} failed with exit code {d}", - .{ emit_path, code }, + "debugging {q} exited with code {d}, expected code {d}", + .{ emit_path, code, debug_success }, ); try update.checkPattern("debugger stdout", expected, result.stdout, &.{ .{ "\n", .anon, newline }, @@ -3043,21 +3202,13 @@ const Compiler = struct { try arena.print("--name {s} {s}", .{ emit_name, c_path }) else c_path); - parent_comp.runner.spawnCompiler(&compiler, src_dir, parent_comp.command, &args_r, .{ - .triple = parent_comp.target.triple, - .resolved = resolved: { - var resolved = parent_comp.target.resolved; - resolved.ofmt = .default(resolved.os.tag, resolved.cpu.arch); - break :resolved resolved; - }, - .cache_mode = .whole, - .backend = null, - .optimize = parent_comp.target.optimize, - .pic = parent_comp.target.pic, - .linker = parent_comp.target.linker, - .link_libc = parent_comp.target.link_libc, - .strip = parent_comp.target.strip, - }) catch |err| switch (err) { + parent_comp.runner.spawnCompiler(&compiler, target: { + var target = parent_comp.target; + target.resolved.ofmt = .default(target.resolved.os.tag, target.resolved.cpu.arch); + target.cache_mode = .whole; + target.backend = null; + break :target target; + }, src_dir, parent_comp.command, &args_r) catch |err| switch (err) { error.Canceled, error.OutOfMemory, error.AlreadyReported, error.SkipTest => |e| return e, error.ReadFailed => unreachable, // .fixed }; @@ -3155,8 +3306,9 @@ const ClientArgs = struct { gdb_exe: ?std.Io.File, lldb_exe: ?std.Io.File, - targets: []const []const u8, matrix: []const Target, + targets: []const []const u8, + target_filters: []const []const u8, skip_matrix: bool, }; pub fn main(init: std.process.Init) (std.mem.Allocator.Error || std.Io.Cancelable)!u8 { @@ -3183,8 +3335,9 @@ pub fn main(init: std.process.Init) (std.mem.Allocator.Error || std.Io.Cancelabl .gdb_exe = null, .lldb_exe = null, - .targets = &.{}, .matrix = &.{}, + .targets = &.{}, + .target_filters = &.{}, .skip_matrix = undefined, }; @@ -3194,9 +3347,11 @@ pub fn main(init: std.process.Init) (std.mem.Allocator.Error || std.Io.Cancelabl var libc_runtimes_path_arg: ?[]const u8 = null; var gdb_path_arg: ?[]const u8 = null; var lldb_path_arg: ?[]const u8 = null; - var targets: std.ArrayList([]const u8) = .empty; - defer targets.deinit(init.gpa); var matrix_path_arg: ?[]const u8 = null; + var target_args: std.ArrayList([]const u8) = .empty; + defer target_args.deinit(init.gpa); + var target_filter_args: std.ArrayList([]const u8) = .empty; + defer target_filter_args.deinit(init.gpa); var skip_matrix_arg: ?bool = null; var arg_it = try init.minimal.args.iterateAllocator(arena); @@ -3268,31 +3423,32 @@ pub fn main(init: std.process.Init) (std.mem.Allocator.Error || std.Io.Cancelabl }, }; return 0; - } else if (std.mem.eql(u8, arg, "--log")) { - const log_level = arg_it.next() orelse return fatal("missing arg after {q}", .{arg}); - args.log_level = std.meta.stringToEnum(std.log.Level, log_level) orelse - return fatal("invalid arg {q} after {q}", .{ log_level, arg }); - } else if (std.mem.cutPrefix(u8, arg, "--log")) |log_level| { + } else if (if (std.mem.eql(u8, arg, "--log")) + arg_it.next() orelse return fatal("missing arg after {q}", .{arg}) + else + std.mem.cutPrefix(u8, arg, "--log=")) |log_level| + { args.log_level = std.meta.stringToEnum(std.log.Level, log_level) orelse return fatal("invalid arg {q} after {q}", .{ log_level, arg }); } else if (std.mem.eql(u8, arg, "--keep-src")) { args.keep_src = true; - } else if (std.mem.eql(u8, arg, "--zig")) { - zig_path_arg = arg_it.next() orelse return fatal("missing arg after {q}", .{arg}); - } else if (std.mem.cutPrefix(u8, arg, "--zig=")) |zig_path| { + } else if (if (std.mem.eql(u8, arg, "--zig")) + arg_it.next() orelse return fatal("missing arg after {q}", .{arg}) + else + std.mem.cutPrefix(u8, arg, "--zig=")) |zig_path| + { zig_path_arg = zig_path; - } else if (std.mem.eql(u8, arg, "--lib")) { - lib_path_arg = arg_it.next() orelse return fatal("missing arg after {q}", .{arg}); - } else if (std.mem.cutPrefix(u8, arg, "--lib=")) |lib_path| { + } else if (if (std.mem.eql(u8, arg, "--lib")) + arg_it.next() orelse return fatal("missing arg after {q}", .{arg}) + else + std.mem.cutPrefix(u8, arg, "--lib=")) |lib_path| + { lib_path_arg = lib_path; - } else if (std.mem.eql(u8, arg, "--target")) { - try targets.append(init.gpa, arg_it.next() orelse - return fatal("missing arg after {q}", .{arg})); - } else if (std.mem.cutPrefix(u8, arg, "--target=")) |target| { - try targets.append(init.gpa, target); - } else if (std.mem.eql(u8, arg, "--libc-runtimes")) { - libc_runtimes_path_arg = arg_it.next() orelse return fatal("missing arg after {q}", .{arg}); - } else if (std.mem.cutPrefix(u8, arg, "--libc-runtimes=")) |libc_runtimes_path| { + } else if (if (std.mem.eql(u8, arg, "--libc-runtimes")) + arg_it.next() orelse return fatal("missing arg after {q}", .{arg}) + else + std.mem.cutPrefix(u8, arg, "--libc-runtimes=")) |libc_runtimes_path| + { libc_runtimes_path_arg = libc_runtimes_path; } else if (std.mem.eql(u8, arg, "-fdarling")) { args.enable_darling = true; @@ -3304,18 +3460,36 @@ pub fn main(init: std.process.Init) (std.mem.Allocator.Error || std.Io.Cancelabl args.enable_wine = true; } else if (std.mem.eql(u8, arg, "-fwasmtime")) { args.enable_wasmtime = true; - } else if (std.mem.eql(u8, arg, "--gdb")) { - gdb_path_arg = arg_it.next() orelse return fatal("missing arg after {q}", .{arg}); - } else if (std.mem.cutPrefix(u8, arg, "--gdb=")) |gdb_path| { + } else if (if (std.mem.eql(u8, arg, "--gdb")) + arg_it.next() orelse return fatal("missing arg after {q}", .{arg}) + else + std.mem.cutPrefix(u8, arg, "--gdb=")) |gdb_path| + { gdb_path_arg = gdb_path; - } else if (std.mem.eql(u8, arg, "--lldb")) { - lldb_path_arg = arg_it.next() orelse return fatal("missing arg after {q}", .{arg}); - } else if (std.mem.cutPrefix(u8, arg, "--lldb=")) |lldb_path| { + } else if (if (std.mem.eql(u8, arg, "--lldb")) + arg_it.next() orelse return fatal("missing arg after {q}", .{arg}) + else + std.mem.cutPrefix(u8, arg, "--lldb=")) |lldb_path| + { lldb_path_arg = lldb_path; - } else if (std.mem.eql(u8, arg, "--matrix")) { - matrix_path_arg = arg_it.next() orelse return fatal("missing arg after {q}", .{arg}); - } else if (std.mem.cutPrefix(u8, arg, "--matrix=")) |matrix_path| { + } else if (if (std.mem.eql(u8, arg, "--matrix")) + arg_it.next() orelse return fatal("missing arg after {q}", .{arg}) + else + std.mem.cutPrefix(u8, arg, "--matrix=")) |matrix_path| + { matrix_path_arg = matrix_path; + } else if (if (std.mem.eql(u8, arg, "--target")) + arg_it.next() orelse return fatal("missing arg after {q}", .{arg}) + else + std.mem.cutPrefix(u8, arg, "--target=")) |target| + { + try target_args.append(init.gpa, target); + } else if (if (std.mem.eql(u8, arg, "--target-filter")) + arg_it.next() orelse return fatal("missing arg after {q}", .{arg}) + else + std.mem.cutPrefix(u8, arg, "--target-filter=")) |target_filter| + { + try target_filter_args.append(init.gpa, target_filter); } else if (std.mem.eql(u8, arg, "--keep-matrix")) { skip_matrix_arg = false; } else if (std.mem.eql(u8, arg, "--skip-matrix")) { @@ -3414,7 +3588,8 @@ pub fn main(init: std.process.Init) (std.mem.Allocator.Error || std.Io.Cancelabl }; } - args.targets = targets.items; + args.targets = target_args.items; + args.target_filters = target_filter_args.items; matrix: { const matrix_path = matrix_path_arg orelse try std.Io.Dir.path.join(arena, &.{ std.Io.Dir.path.dirname(test_path) orelse break :matrix, "matrix.zon", @@ -3518,9 +3693,14 @@ fn runClient( var matrix: std.array_hash_map.String(Target.State) = .empty; try matrix.ensureTotalCapacity(arena, args.matrix.len + args.targets.len); - for (args.matrix) |target| matrix.putAssumeCapacity(try arena.print("{f}", .{target}), .{ - .skipped = args.skip_matrix, - }); + for (args.matrix) |target| { + const target_string = try arena.print("{f}", .{target}); + matrix.putAssumeCapacity(target_string, .{ + .skipped = args.skip_matrix or for (args.target_filters) |target_filter| { + if (std.mem.find(u8, target_string, target_filter)) |_| break false; + } else args.target_filters.len > 0, + }); + } for (args.targets) |target| matrix.putAssumeCapacity(target, .{ .skipped = false }); var argv: std.ArrayList(u8) = .empty; @@ -3713,13 +3893,6 @@ fn runClient( return success; } -const newline: []const Pattern = &.{ - .{ .@"?" = &.{ - .{ .literal = "\r" }, - } }, - .{ .literal = "\n" }, -}; - pub const std_options: std.Options = .{ .logFn = log, }; diff --git a/test/error_traces.zig b/test/error_traces.zig deleted file mode 100644 index dfea162f3f..0000000000 --- a/test/error_traces.zig +++ /dev/null @@ -1,684 +0,0 @@ -const std = @import("std"); -const Context = @import("tests.zig").ErrorTracesContext; - -pub fn addCases(cases: *Context, params: *const Context.CaseParameters, target: *const std.Target) void { - cases.addCase(.{ - .params = params, - .target = target, - .name = "return", - .source = - \\pub fn main() !void { - \\ return error.TheSkyIsFalling; - \\} - , - .expect_error = "TheSkyIsFalling", - .expect_trace = - \\source.zig:2:5: [address] in main - \\ return error.TheSkyIsFalling; - \\ ^ - , - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "try return", - .source = - \\fn foo() !void { - \\ return error.TheSkyIsFalling; - \\} - \\ - \\pub fn main() !void { - \\ try foo(); - \\} - , - .expect_error = "TheSkyIsFalling", - .expect_trace = - \\source.zig:2:5: [address] in foo - \\ return error.TheSkyIsFalling; - \\ ^ - \\source.zig:6:5: [address] in main - \\ try foo(); - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - cases.addCase(.{ - .params = params, - .target = target, - .name = "non-error return pops error trace", - .source = - \\fn bar() !void { - \\ return error.UhOh; - \\} - \\ - \\fn foo() !void { - \\ bar() catch { - \\ return; // non-error result: success - \\ }; - \\} - \\ - \\pub fn main() !void { - \\ try foo(); - \\ return error.UnrelatedError; - \\} - , - .expect_error = "UnrelatedError", - .expect_trace = - \\source.zig:13:5: [address] in main - \\ return error.UnrelatedError; - \\ ^ - , - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "continue in while loop", - .source = - \\fn foo() !void { - \\ return error.UhOh; - \\} - \\ - \\pub fn main() !void { - \\ var i: usize = 0; - \\ while (i < 3) : (i += 1) { - \\ foo() catch continue; - \\ } - \\ return error.UnrelatedError; - \\} - , - .expect_error = "UnrelatedError", - .expect_trace = - \\source.zig:10:5: [address] in main - \\ return error.UnrelatedError; - \\ ^ - , - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "for loop pops error return trace", - .source = - \\fn foo() !void { return error.FooError; } - \\ - \\pub fn main() !void { - \\ for (0..2) |_| { - \\ const f = foo(); - \\ f catch {}; - \\ } else { - \\ const f = foo(); - \\ f catch {}; - \\ } - \\ return error.Stop; - \\} - , - .expect_error = "Stop", - .expect_trace = - \\source.zig:11:5: [address] in main - \\ return error.Stop; - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "implicit continue in for loop pops stale error return trace", - .source = - \\fn foo() !void { return error.FooError; } - \\ - \\pub fn main() !void { - \\ for (0..2) |i| { - \\ const f = foo(); - \\ f catch {}; - \\ - \\ if (i == 1) return error.Stop; - \\ } - \\} - , - .expect_error = "Stop", - .expect_trace = - \\source.zig:1:18: [address] in foo - \\fn foo() !void { return error.FooError; } - \\ ^ - \\source.zig:8:21: [address] in main - \\ if (i == 1) return error.Stop; - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "while loop pops error return trace", - .source = - \\fn foo() !void { return error.FooError; } - \\ - \\pub fn main() !void { - \\ var i: usize = 0; - \\ while (i < 2) { - \\ const f = foo(); - \\ f catch {}; - \\ i += 1; - \\ } else { - \\ const f = foo(); - \\ f catch {}; - \\ } - \\ return error.Stop; - \\} - , - .expect_error = "Stop", - .expect_trace = - \\source.zig:13:5: [address] in main - \\ return error.Stop; - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "implicit continue in while loop pops stale error return trace", - .source = - \\fn foo() !void { return error.FooError; } - \\ - \\pub fn main() !void { - \\ var i: usize = 0; - \\ while (i < 2) { - \\ const f = foo(); - \\ f catch {}; - \\ - \\ if (i == 1) return error.Stop; - \\ i += 1; - \\ } - \\} - , - .expect_error = "Stop", - .expect_trace = - \\source.zig:1:18: [address] in foo - \\fn foo() !void { return error.FooError; } - \\ ^ - \\source.zig:9:21: [address] in main - \\ if (i == 1) return error.Stop; - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "try return + handled catch/if-else", - .source = - \\fn foo() !void { - \\ return error.TheSkyIsFalling; - \\} - \\ - \\pub fn main() !void { - \\ foo() catch {}; // should not affect error trace - \\ if (foo()) |_| {} else |_| { - \\ // should also not affect error trace - \\ } - \\ try foo(); - \\} - , - .expect_error = "TheSkyIsFalling", - .expect_trace = - \\source.zig:2:5: [address] in foo - \\ return error.TheSkyIsFalling; - \\ ^ - \\source.zig:10:5: [address] in main - \\ try foo(); - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "break from inline loop pops error return trace", - .source = - \\fn foo() !void { return error.FooBar; } - \\ - \\pub fn main() !void { - \\ comptime var i: usize = 0; - \\ b: inline while (i < 5) : (i += 1) { - \\ foo() catch { - \\ break :b; // non-error break, success - \\ }; - \\ } - \\ // foo() was successfully handled, should not appear in trace - \\ - \\ return error.BadTime; - \\} - , - .expect_error = "BadTime", - .expect_trace = - \\source.zig:12:5: [address] in main - \\ return error.BadTime; - \\ ^ - , - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "catch and re-throw error", - .source = - \\fn foo() !void { - \\ return error.TheSkyIsFalling; - \\} - \\ - \\pub fn main() !void { - \\ return foo() catch error.AndMyCarIsOutOfGas; - \\} - , - .expect_error = "AndMyCarIsOutOfGas", - .expect_trace = - \\source.zig:2:5: [address] in foo - \\ return error.TheSkyIsFalling; - \\ ^ - \\source.zig:6:5: [address] in main - \\ return foo() catch error.AndMyCarIsOutOfGas; - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "errors stored in var do not contribute to error trace", - .source = - \\fn foo() !void { - \\ return error.TheSkyIsFalling; - \\} - \\ - \\pub fn main() !void { - \\ // Once an error is stored in a variable, it is popped from the trace - \\ var x = foo(); - \\ x = {}; - \\ - \\ // As a result, this error trace will still be clean - \\ return error.SomethingUnrelatedWentWrong; - \\} - , - .expect_error = "SomethingUnrelatedWentWrong", - .expect_trace = - \\source.zig:11:5: [address] in main - \\ return error.SomethingUnrelatedWentWrong; - \\ ^ - , - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "error stored in const has trace preserved for duration of block", - .source = - \\fn foo() !void { return error.TheSkyIsFalling; } - \\fn bar() !void { return error.InternalError; } - \\fn baz() !void { return error.UnexpectedReality; } - \\ - \\pub fn main() !void { - \\ const x = foo(); - \\ const y = b: { - \\ if (true) - \\ break :b bar(); - \\ - \\ break :b {}; - \\ }; - \\ x catch {}; - \\ y catch {}; - \\ // foo()/bar() error traces not popped until end of block - \\ - \\ { - \\ const z = baz(); - \\ z catch {}; - \\ // baz() error trace still alive here - \\ } - \\ // baz() error trace popped, foo(), bar() still alive - \\ return error.StillUnresolved; - \\} - , - .expect_error = "StillUnresolved", - .expect_trace = - \\source.zig:1:18: [address] in foo - \\fn foo() !void { return error.TheSkyIsFalling; } - \\ ^ - \\source.zig:2:18: [address] in bar - \\fn bar() !void { return error.InternalError; } - \\ ^ - \\source.zig:23:5: [address] in main - \\ return error.StillUnresolved; - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "error passed to function has its trace preserved for duration of the call", - .source = - \\pub fn expectError(expected_error: anyerror, actual_error: anyerror!void) !void { - \\ actual_error catch |err| { - \\ if (err == expected_error) return {}; - \\ }; - \\ return error.TestExpectedError; - \\} - \\ - \\fn alwaysErrors() !void { return error.ThisErrorShouldNotAppearInAnyTrace; } - \\fn foo() !void { return error.Foo; } - \\ - \\pub fn main() !void { - \\ try expectError(error.ThisErrorShouldNotAppearInAnyTrace, alwaysErrors()); - \\ try expectError(error.ThisErrorShouldNotAppearInAnyTrace, alwaysErrors()); - \\ try expectError(error.Foo, foo()); - \\ - \\ // Only the error trace for this failing check should appear: - \\ try expectError(error.Bar, foo()); - \\} - , - .expect_error = "TestExpectedError", - .expect_trace = - \\source.zig:9:18: [address] in foo - \\fn foo() !void { return error.Foo; } - \\ ^ - \\source.zig:5:5: [address] in expectError - \\ return error.TestExpectedError; - \\ ^ - \\source.zig:17:5: [address] in main - \\ try expectError(error.Bar, foo()); - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "try return from within catch", - .source = - \\fn foo() !void { - \\ return error.TheSkyIsFalling; - \\} - \\ - \\fn bar() !void { - \\ return error.AndMyCarIsOutOfGas; - \\} - \\ - \\pub fn main() !void { - \\ foo() catch { // error trace should include foo() - \\ try bar(); - \\ }; - \\} - , - .expect_error = "AndMyCarIsOutOfGas", - .expect_trace = - \\source.zig:2:5: [address] in foo - \\ return error.TheSkyIsFalling; - \\ ^ - \\source.zig:6:5: [address] in bar - \\ return error.AndMyCarIsOutOfGas; - \\ ^ - \\source.zig:11:9: [address] in main - \\ try bar(); - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "try return from within if-else", - .source = - \\fn foo() !void { - \\ return error.TheSkyIsFalling; - \\} - \\ - \\fn bar() !void { - \\ return error.AndMyCarIsOutOfGas; - \\} - \\ - \\pub fn main() !void { - \\ if (foo()) |_| {} else |_| { // error trace should include foo() - \\ try bar(); - \\ } - \\} - , - .expect_error = "AndMyCarIsOutOfGas", - .expect_trace = - \\source.zig:2:5: [address] in foo - \\ return error.TheSkyIsFalling; - \\ ^ - \\source.zig:6:5: [address] in bar - \\ return error.AndMyCarIsOutOfGas; - \\ ^ - \\source.zig:11:9: [address] in main - \\ try bar(); - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "try try return return", - .source = - \\fn foo() !void { - \\ try bar(); - \\} - \\ - \\fn bar() !void { - \\ return make_error(); - \\} - \\ - \\fn make_error() !void { - \\ return error.TheSkyIsFalling; - \\} - \\ - \\pub fn main() !void { - \\ try foo(); - \\} - , - .expect_error = "TheSkyIsFalling", - .expect_trace = - \\source.zig:10:5: [address] in make_error - \\ return error.TheSkyIsFalling; - \\ ^ - \\source.zig:6:5: [address] in bar - \\ return make_error(); - \\ ^ - \\source.zig:2:5: [address] in foo - \\ try bar(); - \\ ^ - \\source.zig:14:5: [address] in main - \\ try foo(); - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "error union switch with call operand", - .source = - \\pub fn main() !void { - \\ try foo(); - \\ return error.TheSkyIsFalling; - \\} - \\ - \\noinline fn failure() error{ Fatal, NonFatal }!void { - \\ return error.NonFatal; - \\} - \\ - \\fn foo() error{Fatal}!void { - \\ return failure() catch |err| switch (err) { - \\ error.Fatal => return error.Fatal, - \\ error.NonFatal => return, - \\ }; - \\} - , - .expect_error = "TheSkyIsFalling", - .expect_trace = - \\source.zig:3:5: [address] in main - \\ return error.TheSkyIsFalling; - \\ ^ - , - .disable_trace_optimized = &.{ - .{ .x86_64, .freebsd }, - .{ .x86_64, .netbsd }, - .{ .x86_64, .linux }, - .{ .x86, .linux }, - .{ .aarch64, .freebsd }, - .{ .aarch64, .netbsd }, - .{ .aarch64, .linux }, - .{ .loongarch64, .linux }, - .{ .powerpc64le, .linux }, - .{ .riscv64, .linux }, - .{ .s390x, .linux }, - .{ .x86_64, .openbsd }, - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); - - cases.addCase(.{ - .params = params, - .target = target, - .name = "trace through inline call", - // The main function has two inline calls to ensure - // that inlinees in PDBs are properly deduplicated. - .source = - \\pub fn main() !void { - \\ try foo(false); - \\ try foo(true); - \\} - \\inline fn foo(b: bool) !void { - \\ if (b) try bar(); - \\} - \\fn bar() !void { - \\ return error.ThisIsSoSad; - \\} - , - .expect_error = "ThisIsSoSad", - .expect_trace = switch (target.os.tag) { - // LLVM doesn't emit column info in the binary annotations for inlinee callees in PDBs, - // so our expected result is slightly different for Windows than on other operating - // systems. - .windows => - \\source.zig:9:5: [address] in bar - \\ return error.ThisIsSoSad; - \\ ^ - \\source.zig:6: [address] in foo - \\ if (b) try bar(); - \\ - \\source.zig:3:5: [address] in main - \\ try foo(true); - \\ ^ - , - else => - \\source.zig:9:5: [address] in bar - \\ return error.ThisIsSoSad; - \\ ^ - \\source.zig:6:12: [address] in foo - \\ if (b) try bar(); - \\ ^ - \\source.zig:3:5: [address] in main - \\ try foo(true); - \\ ^ - , - }, - .disable_trace_optimized = &.{ - .{ .x86_64, .freebsd }, - .{ .x86_64, .netbsd }, - .{ .x86_64, .linux }, - .{ .x86, .linux }, - .{ .aarch64, .freebsd }, - .{ .aarch64, .netbsd }, - .{ .aarch64, .openbsd }, - .{ .aarch64, .linux }, - .{ .loongarch64, .linux }, - .{ .powerpc64le, .linux }, - .{ .riscv64, .linux }, - .{ .s390x, .linux }, - .{ .x86_64, .openbsd }, - .{ .x86_64, .windows }, - .{ .x86, .windows }, - .{ .x86_64, .macos }, - .{ .aarch64, .macos }, - }, - }); -} diff --git a/test/error_traces/break_from_inline_loop_pops_error_return_trace b/test/error_traces/break_from_inline_loop_pops_error_return_trace new file mode 100644 index 0000000000..179588b7cc --- /dev/null +++ b/test/error_traces/break_from_inline_loop_pops_error_return_trace @@ -0,0 +1,27 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { return error.FooBar; } + +pub fn main() !void { + comptime var i: usize = 0; + b: inline while (i < 5) : (i += 1) { + foo() catch { + break :b; // non-error break, success + }; + } + // foo() was successfully handled, should not appear in trace (test*) + + return error.BadTime; +} +#} +#update +#check error-trace { +error: BadTime +test.zig:12:5: 0x? in main (test*) + return error.BadTime; + ^ +#} diff --git a/test/error_traces/catch_and_re-throw_error b/test/error_traces/catch_and_re-throw_error new file mode 100644 index 0000000000..19702dabd3 --- /dev/null +++ b/test/error_traces/catch_and_re-throw_error @@ -0,0 +1,24 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { + return error.TheSkyIsFalling; +} + +pub fn main() !void { + return foo() catch error.AndMyCarIsOutOfGas; +} +#} +#update +#check error-trace { +error: AndMyCarIsOutOfGas +test.zig:2:5: 0x? in foo (test*) + return error.TheSkyIsFalling; + ^ +test.zig:6:5: 0x? in main (test*) + return foo() catch error.AndMyCarIsOutOfGas; + ^ +#} diff --git a/test/error_traces/continue_in_while_loop b/test/error_traces/continue_in_while_loop new file mode 100644 index 0000000000..112aa11b89 --- /dev/null +++ b/test/error_traces/continue_in_while_loop @@ -0,0 +1,25 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { + return error.UhOh; +} + +pub fn main() !void { + var i: usize = 0; + while (i < 3) : (i += 1) { + foo() catch continue; + } + return error.UnrelatedError; +} +#} +#update +#check error-trace { +error: UnrelatedError +test.zig:10:5: 0x? in main (test*) + return error.UnrelatedError; + ^ +#} diff --git a/test/error_traces/error_passed_to_function_has_its_trace_preserved_for_duration_of_the_call b/test/error_traces/error_passed_to_function_has_its_trace_preserved_for_duration_of_the_call new file mode 100644 index 0000000000..6bb391be7a --- /dev/null +++ b/test/error_traces/error_passed_to_function_has_its_trace_preserved_for_duration_of_the_call @@ -0,0 +1,38 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +pub fn expectError(expected_error: anyerror, actual_error: anyerror!void) !void { + actual_error catch |err| { + if (err == expected_error) return {}; + }; + return error.TestExpectedError; +} + +fn alwaysErrors() !void { return error.ThisErrorShouldNotAppearInAnyTrace; } +fn foo() !void { return error.Foo; } + +pub fn main() !void { + try expectError(error.ThisErrorShouldNotAppearInAnyTrace, alwaysErrors()); + try expectError(error.ThisErrorShouldNotAppearInAnyTrace, alwaysErrors()); + try expectError(error.Foo, foo()); + + // Only the error trace for this failing check should appear: + try expectError(error.Bar, foo()); +} +#} +#update +#check error-trace { +error: TestExpectedError +test.zig:9:18: 0x? in foo (test*) +fn foo() !void { return error.Foo; } + ^ +test.zig:5:5: 0x? in expectError (test*) + return error.TestExpectedError; + ^ +test.zig:17:5: 0x? in main (test*) + try expectError(error.Bar, foo()); + ^ +#} diff --git a/test/error_traces/error_stored_in_const_has_trace_preserved_for_duration_of_block b/test/error_traces/error_stored_in_const_has_trace_preserved_for_duration_of_block new file mode 100644 index 0000000000..08ee4e57a5 --- /dev/null +++ b/test/error_traces/error_stored_in_const_has_trace_preserved_for_duration_of_block @@ -0,0 +1,44 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { return error.TheSkyIsFalling; } +fn bar() !void { return error.InternalError; } +fn baz() !void { return error.UnexpectedReality; } + +pub fn main() !void { + const x = foo(); + const y = b: { + if (true) + break :b bar(); + + break :b {}; + }; + x catch {}; + y catch {}; + // foo()/bar() error traces not popped until end of block + + { + const z = baz(); + z catch {}; + // baz() error trace still alive here + } + // baz() error trace popped, foo(), bar() still alive + return error.StillUnresolved; +} +#} +#update +#check error-trace { +error: StillUnresolved +test.zig:1:18: 0x? in foo (test*) +fn foo() !void { return error.TheSkyIsFalling; } + ^ +test.zig:2:18: 0x? in bar (test*) +fn bar() !void { return error.InternalError; } + ^ +test.zig:23:5: 0x? in main (test*) + return error.StillUnresolved; + ^ +#} diff --git a/test/error_traces/error_union_switch_with_call_operand b/test/error_traces/error_union_switch_with_call_operand new file mode 100644 index 0000000000..09d84d2f5b --- /dev/null +++ b/test/error_traces/error_union_switch_with_call_operand @@ -0,0 +1,29 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +pub fn main() !void { + try foo(); + return error.TheSkyIsFalling; +} + +noinline fn failure() error{ Fatal, NonFatal }!void { + return error.NonFatal; +} + +fn foo() error{Fatal}!void { + return failure() catch |err| switch (err) { + error.Fatal => return error.Fatal, + error.NonFatal => return, + }; +} +#} +#update +#check error-trace { +error: TheSkyIsFalling +test.zig:3:5: 0x? in main (test*) + return error.TheSkyIsFalling; + ^ +#} diff --git a/test/error_traces/errors_stored_in_var_do_not_contribute_to_error_trace b/test/error_traces/errors_stored_in_var_do_not_contribute_to_error_trace new file mode 100644 index 0000000000..5591466e33 --- /dev/null +++ b/test/error_traces/errors_stored_in_var_do_not_contribute_to_error_trace @@ -0,0 +1,26 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { + return error.TheSkyIsFalling; +} + +pub fn main() !void { + // Once an error is stored in a variable, it is popped from the trace + var x = foo(); + x = {}; + + // As a result, this error trace will still be clean + return error.SomethingUnrelatedWentWrong; +} +#} +#update +#check error-trace { +error: SomethingUnrelatedWentWrong +test.zig:11:5: 0x? in main (test*) + return error.SomethingUnrelatedWentWrong; + ^ +#} diff --git a/test/error_traces/for_loop_pops_error_return_trace b/test/error_traces/for_loop_pops_error_return_trace new file mode 100644 index 0000000000..a457ec7e99 --- /dev/null +++ b/test/error_traces/for_loop_pops_error_return_trace @@ -0,0 +1,26 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { return error.FooError; } + +pub fn main() !void { + for (0..2) |_| { + const f = foo(); + f catch {}; + } else { + const f = foo(); + f catch {}; + } + return error.Stop; +} +#} +#update +#check error-trace { +error: Stop +test.zig:11:5: 0x? in main (test*) + return error.Stop; + ^ +#} diff --git a/test/error_traces/implicit_continue_in_for_loop_pops_stale_error_return_trace b/test/error_traces/implicit_continue_in_for_loop_pops_stale_error_return_trace new file mode 100644 index 0000000000..65b87c71ab --- /dev/null +++ b/test/error_traces/implicit_continue_in_for_loop_pops_stale_error_return_trace @@ -0,0 +1,27 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { return error.FooError; } + +pub fn main() !void { + for (0..2) |i| { + const f = foo(); + f catch {}; + + if (i == 1) return error.Stop; + } +} +#} +#update +#check error-trace { +error: Stop +test.zig:1:18: 0x? in foo (test*) +fn foo() !void { return error.FooError; } + ^ +test.zig:8:21: 0x? in main (test*) + if (i == 1) return error.Stop; + ^ +#} diff --git a/test/error_traces/implicit_continue_in_while_loop_pops_stale_error_return_trace b/test/error_traces/implicit_continue_in_while_loop_pops_stale_error_return_trace new file mode 100644 index 0000000000..b4e11a4690 --- /dev/null +++ b/test/error_traces/implicit_continue_in_while_loop_pops_stale_error_return_trace @@ -0,0 +1,29 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { return error.FooError; } + +pub fn main() !void { + var i: usize = 0; + while (i < 2) { + const f = foo(); + f catch {}; + + if (i == 1) return error.Stop; + i += 1; + } +} +#} +#update +#check error-trace { +error: Stop +test.zig:1:18: 0x? in foo (test*) +fn foo() !void { return error.FooError; } + ^ +test.zig:9:21: 0x? in main (test*) + if (i == 1) return error.Stop; + ^ +#} diff --git a/test/error_traces/matrix.zon b/test/error_traces/matrix.zon new file mode 100644 index 0000000000..da4e7130fc --- /dev/null +++ b/test/error_traces/matrix.zon @@ -0,0 +1,318 @@ +// Only add a non-native target to this set if there's a way to emulate it, or if it can built on a +// platform with the same arch/OS but has a different ABI. For example, it makes little sense to +// add `riscv64-netbsd` here because there's no QEMU user-mode emulation for it anyway, and it'll +// still be covered by the native entries when run on a real `riscv64-netbsd` system. But adding +// `aarch64-windows-msvc` is valuable because the native entries will default to `gnu` on Windows. +.{ + .{ + .cache_mode = .whole, + .backend = .llvm, + }, + + // FreeBSD Targets + + .{ + .cpu = .{ .arm = .{} }, + .os = .{ .freebsd = .{} }, + .abi = .{ .eabihf = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .x86 = .{} }, + .os = .{ .freebsd = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + // Linux Targets + + .{ + .cpu = .{ .aarch64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .aarch64_be = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .arm = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .eabihf = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .armeb = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .eabihf = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .hexagon = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .loongarch32 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .loongarch64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .mips = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .eabihf = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .mipsel = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .eabihf = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .mips64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + .{ + .cpu = .{ .mips64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .abin32 = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .mips64el = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + .{ + .cpu = .{ .mips64el = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .abin32 = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .powerpc = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .eabihf = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .powerpc64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .powerpc64le = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .riscv32 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .riscv64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .s390x = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .sparc64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .thumb = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .eabihf = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .thumbeb = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .eabihf = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .x86 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .x86_64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .selfhosted, + .linker = .old, + }, + .{ + .cpu = .{ .x86_64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .selfhosted, + .linker = .new, + }, + .{ + .cpu = .{ .x86_64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + .linker = .lld, + }, + .{ + .cpu = .{ .x86_64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + .linker = .new, + }, + .{ + .cpu = .{ .x86_64 = .{} }, + .os = .{ .linux = .{} }, + .abi = .{ .x32 = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + // NetBSD Targets + + .{ + .cpu = .{ .riscv32 = .{} }, + .os = .{ .netbsd = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .x86 = .{} }, + .os = .{ .netbsd = .{} }, + .abi = .{ .none = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + // Windows Targets + + .{ + .cpu = .{ .aarch64 = .{} }, + .os = .{ .windows = .{} }, + .abi = .{ .msvc = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + .{ + .cpu = .{ .aarch64 = .{} }, + .os = .{ .windows = .{} }, + .abi = .{ .gnu = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .x86 = .{} }, + .os = .{ .windows = .{} }, + .abi = .{ .msvc = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + .{ + .cpu = .{ .x86 = .{} }, + .os = .{ .windows = .{} }, + .abi = .{ .gnu = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + + .{ + .cpu = .{ .x86_64 = .{} }, + .os = .{ .windows = .{} }, + .abi = .{ .msvc = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, + .{ + .cpu = .{ .x86_64 = .{} }, + .os = .{ .windows = .{} }, + .abi = .{ .gnu = .{} }, + .cache_mode = .whole, + .backend = .llvm, + }, +} diff --git a/test/error_traces/non-error_return_pops_error_trace b/test/error_traces/non-error_return_pops_error_trace new file mode 100644 index 0000000000..06dcf5cf5d --- /dev/null +++ b/test/error_traces/non-error_return_pops_error_trace @@ -0,0 +1,28 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn bar() !void { + return error.UhOh; +} + +fn foo() !void { + bar() catch { + return; // non-error result: success + }; +} + +pub fn main() !void { + try foo(); + return error.UnrelatedError; +} +#} +#update +#check error-trace { +error: UnrelatedError +test.zig:13:5: 0x? in main (test*) + return error.UnrelatedError; + ^ +#} diff --git a/test/error_traces/return b/test/error_traces/return new file mode 100644 index 0000000000..e1f95e774f --- /dev/null +++ b/test/error_traces/return @@ -0,0 +1,17 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +pub fn main() !void { + return error.TheSkyIsFalling; +} +#} +#update +#check error-trace { +error: TheSkyIsFalling +test.zig:2:5: 0x? in main (test*) + return error.TheSkyIsFalling; + ^ +#} diff --git a/test/error_traces/trace_through_inline_call b/test/error_traces/trace_through_inline_call new file mode 100644 index 0000000000..1cf73b45d1 --- /dev/null +++ b/test/error_traces/trace_through_inline_call @@ -0,0 +1,31 @@ +#require !windows +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +pub fn main() !void { + try foo(false); + try foo(true); +} +inline fn foo(b: bool) !void { + if (b) try bar(); +} +fn bar() !void { + return error.ThisIsSoSad; +} +#} +#update +#check error-trace { +error: ThisIsSoSad +test.zig:9:5: 0x? in bar (test*) + return error.ThisIsSoSad; + ^ +test.zig:6:12: 0x? in foo (test*) + if (b) try bar(); + ^ +test.zig:3:5: 0x? in main (test*) + try foo(true); + ^ +#} diff --git a/test/error_traces/trace_through_inline_call_windows b/test/error_traces/trace_through_inline_call_windows new file mode 100644 index 0000000000..7c7676d9e6 --- /dev/null +++ b/test/error_traces/trace_through_inline_call_windows @@ -0,0 +1,31 @@ +#require windows +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +pub fn main() !void { + try foo(false); + try foo(true); +} +inline fn foo(b: bool) !void { + if (b) try bar(); +} +fn bar() !void { + return error.ThisIsSoSad; +} +#} +#update +#check error-trace { +error: ThisIsSoSad +test.zig:9:5: 0x? in bar (test*) + return error.ThisIsSoSad; + ^ +test.zig:6: 0x? in foo (test*) + if (b) try bar(); + +test.zig:3:5: 0x? in main (test*) + try foo(true); + ^ +#} diff --git a/test/error_traces/try_return b/test/error_traces/try_return new file mode 100644 index 0000000000..a474ceb05a --- /dev/null +++ b/test/error_traces/try_return @@ -0,0 +1,24 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { + return error.TheSkyIsFalling; +} + +pub fn main() !void { + try foo(); +} +#} +#update +#check error-trace { +error: TheSkyIsFalling +test.zig:2:5: 0x? in foo (test*) + return error.TheSkyIsFalling; + ^ +test.zig:6:5: 0x? in main (test*) + try foo(); + ^ +#} diff --git a/test/error_traces/try_return_from_within_catch b/test/error_traces/try_return_from_within_catch new file mode 100644 index 0000000000..3fba08f79d --- /dev/null +++ b/test/error_traces/try_return_from_within_catch @@ -0,0 +1,33 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { + return error.TheSkyIsFalling; +} + +fn bar() !void { + return error.AndMyCarIsOutOfGas; +} + +pub fn main() !void { + foo() catch { // error trace should include foo() + try bar(); + }; +} +#} +#update +#check error-trace { +error: AndMyCarIsOutOfGas +test.zig:2:5: 0x? in foo (test*) + return error.TheSkyIsFalling; + ^ +test.zig:6:5: 0x? in bar (test*) + return error.AndMyCarIsOutOfGas; + ^ +test.zig:11:9: 0x? in main (test*) + try bar(); + ^ +#} diff --git a/test/error_traces/try_return_from_within_if-else b/test/error_traces/try_return_from_within_if-else new file mode 100644 index 0000000000..cac373a1af --- /dev/null +++ b/test/error_traces/try_return_from_within_if-else @@ -0,0 +1,33 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { + return error.TheSkyIsFalling; +} + +fn bar() !void { + return error.AndMyCarIsOutOfGas; +} + +pub fn main() !void { + if (foo()) |_| {} else |_| { // error trace should include foo() + try bar(); + } +} +#} +#update +#check error-trace { +error: AndMyCarIsOutOfGas +test.zig:2:5: 0x? in foo (test*) + return error.TheSkyIsFalling; + ^ +test.zig:6:5: 0x? in bar (test*) + return error.AndMyCarIsOutOfGas; + ^ +test.zig:11:9: 0x? in main (test*) + try bar(); + ^ +#} diff --git a/test/error_traces/try_return_handled_catch_or_if-else b/test/error_traces/try_return_handled_catch_or_if-else new file mode 100644 index 0000000000..6f69070502 --- /dev/null +++ b/test/error_traces/try_return_handled_catch_or_if-else @@ -0,0 +1,28 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { + return error.TheSkyIsFalling; +} + +pub fn main() !void { + foo() catch {}; // should not affect error trace + if (foo()) |_| {} else |_| { + // should also not affect error trace + } + try foo(); +} +#} +#update +#check error-trace { +error: TheSkyIsFalling +test.zig:2:5: 0x? in foo (test*) + return error.TheSkyIsFalling; + ^ +test.zig:10:5: 0x? in main (test*) + try foo(); + ^ +#} diff --git a/test/error_traces/try_try_return_return b/test/error_traces/try_try_return_return new file mode 100644 index 0000000000..c6dfcac03f --- /dev/null +++ b/test/error_traces/try_try_return_return @@ -0,0 +1,38 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { + try bar(); +} + +fn bar() !void { + return make_error(); +} + +fn make_error() !void { + return error.TheSkyIsFalling; +} + +pub fn main() !void { + try foo(); +} +#} +#update +#check error-trace { +error: TheSkyIsFalling +test.zig:10:5: 0x? in make_error (test*) + return error.TheSkyIsFalling; + ^ +test.zig:6:5: 0x? in bar (test*) + return make_error(); + ^ +test.zig:2:5: 0x? in foo (test*) + try bar(); + ^ +test.zig:14:5: 0x? in main (test*) + try foo(); + ^ +#} diff --git a/test/error_traces/while_loop_pops_error_return_trace b/test/error_traces/while_loop_pops_error_return_trace new file mode 100644 index 0000000000..c73b6089c3 --- /dev/null +++ b/test/error_traces/while_loop_pops_error_return_trace @@ -0,0 +1,28 @@ +#zig build-exe { +--name test +-Mroot=test.zig +#} + +#write test.zig { +fn foo() !void { return error.FooError; } + +pub fn main() !void { + var i: usize = 0; + while (i < 2) { + const f = foo(); + f catch {}; + i += 1; + } else { + const f = foo(); + f catch {}; + } + return error.Stop; +} +#} +#update +#check error-trace { +error: Stop +test.zig:13:5: 0x? in main (test*) + return error.Stop; + ^ +#} diff --git a/test/src/ErrorTrace.zig b/test/src/ErrorTrace.zig deleted file mode 100644 index 8a09ccee40..0000000000 --- a/test/src/ErrorTrace.zig +++ /dev/null @@ -1,505 +0,0 @@ -const ErrorTrace = @This(); - -const builtin = @import("builtin"); - -const std = @import("std"); -const Step = std.Build.Step; -const OptimizeMode = std.lang.Optimize; -const mem = std.mem; - -const tests = @import("../tests.zig"); -const error_traces_cases = @import("../error_traces.zig"); - -b: *std.Build, -step: *Step, -options: Options, -convert_exe: *std.Build.Step.Compile, - -pub const Options = struct { - test_filters: []const []const u8, - test_target_filters: []const []const u8, - test_extra_targets: bool, - optimize_modes: []const OptimizeMode, - skip_non_native: bool, - skip_freebsd: bool, - skip_netbsd: bool, - skip_openbsd: bool, - skip_windows: bool, - skip_darwin: bool, - skip_linux: bool, - skip_llvm: bool, - skip_libc: bool, -}; - -pub const CaseParameters = struct { - target: std.Target.Query = .{}, - optimize: OptimizeMode = .debug, - use_llvm: ?bool = null, - use_lld: ?bool = null, - use_new_linker: ?bool = null, - - // This is intended for targets that, for any reason, shouldn't be run as part of a normal test - // invocation. This could be because of a slow backend, requiring a newer LLVM version, being - // too niche, etc. - extra_target: bool = false, -}; - -/// See the comment in `StackTrace.zig`. -pub const param_sets = [_]CaseParameters{ - .{}, - - // FreeBSD Targets - - .{ - .target = .{ - .cpu_arch = .arm, - .os_tag = .freebsd, - .abi = .eabihf, - }, - }, - - .{ - .target = .{ - .cpu_arch = .x86, - .os_tag = .freebsd, - .abi = .none, - }, - }, - - // Linux Targets - - .{ - .target = .{ - .cpu_arch = .aarch64, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .aarch64_be, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .arm, - .os_tag = .linux, - .abi = .eabihf, - }, - }, - - .{ - .target = .{ - .cpu_arch = .armeb, - .os_tag = .linux, - .abi = .eabihf, - }, - }, - - .{ - .target = .{ - .cpu_arch = .hexagon, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .loongarch32, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .loongarch64, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .mips, - .os_tag = .linux, - .abi = .eabihf, - }, - }, - - .{ - .target = .{ - .cpu_arch = .mipsel, - .os_tag = .linux, - .abi = .eabihf, - }, - }, - - .{ - .target = .{ - .cpu_arch = .mips64, - .os_tag = .linux, - .abi = .none, - }, - }, - .{ - .target = .{ - .cpu_arch = .mips64, - .os_tag = .linux, - .abi = .abin32, - }, - }, - - .{ - .target = .{ - .cpu_arch = .mips64el, - .os_tag = .linux, - .abi = .none, - }, - }, - .{ - .target = .{ - .cpu_arch = .mips64el, - .os_tag = .linux, - .abi = .abin32, - }, - }, - - .{ - .target = .{ - .cpu_arch = .powerpc, - .os_tag = .linux, - .abi = .eabihf, - }, - }, - - .{ - .target = .{ - .cpu_arch = .powerpc64, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .powerpc64le, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .riscv32, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .riscv64, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .s390x, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .sparc64, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .thumb, - .os_tag = .linux, - .abi = .eabihf, - }, - }, - - .{ - .target = .{ - .cpu_arch = .thumbeb, - .os_tag = .linux, - .abi = .eabihf, - }, - }, - - .{ - .target = .{ - .cpu_arch = .x86, - .os_tag = .linux, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .x86_64, - .os_tag = .linux, - .abi = .none, - }, - }, - .{ - .target = .{ - .cpu_arch = .x86_64, - .os_tag = .linux, - .abi = .none, - }, - .use_new_linker = true, - }, - .{ - .target = .{ - .cpu_arch = .x86_64, - .os_tag = .linux, - .abi = .none, - }, - .use_llvm = true, - .use_lld = true, - }, - .{ - .target = .{ - .cpu_arch = .x86_64, - .os_tag = .linux, - .abi = .none, - }, - .use_llvm = true, - .use_new_linker = true, - }, - .{ - .target = .{ - .cpu_arch = .x86_64, - .os_tag = .linux, - .abi = .x32, - }, - }, - - // NetBSD Targets - - .{ - .target = .{ - .cpu_arch = .riscv32, - .os_tag = .netbsd, - .abi = .none, - }, - }, - - .{ - .target = .{ - .cpu_arch = .x86, - .os_tag = .netbsd, - .abi = .none, - }, - }, - - // Windows Targets - - .{ - .target = .{ - .cpu_arch = .aarch64, - .os_tag = .windows, - .abi = .msvc, - }, - }, - .{ - .target = .{ - .cpu_arch = .aarch64, - .os_tag = .windows, - .abi = .gnu, - }, - }, - - .{ - .target = .{ - .cpu_arch = .x86, - .os_tag = .windows, - .abi = .msvc, - }, - }, - .{ - .target = .{ - .cpu_arch = .x86, - .os_tag = .windows, - .abi = .gnu, - }, - }, - - .{ - .target = .{ - .cpu_arch = .x86_64, - .os_tag = .windows, - .abi = .msvc, - }, - }, - .{ - .target = .{ - .cpu_arch = .x86_64, - .os_tag = .windows, - .abi = .gnu, - }, - }, -}; - -pub const Case = struct { - params: *const CaseParameters, - target: *const std.Target, - name: []const u8, - source: []const u8, - expect_error: []const u8, - expect_trace: []const u8, - /// On these arch/OS pairs we will not test the error trace on optimized LLVM builds because the - /// optimizations break the error trace. We will test the binary with error tracing disabled, - /// just to ensure that the expected error is still returned from `main`. - /// - /// LLVM ReleaseSmall builds always have the trace disabled regardless of this field, because it - /// seems that LLVM is particularly good at optimizing traces away in those. - disable_trace_optimized: []const DisableConfig = &.{}, - - pub const DisableConfig = struct { std.Target.Cpu.Arch, std.Target.Os.Tag }; - pub const Backend = enum { llvm, selfhosted }; -}; - -pub fn addCases(self: *ErrorTrace) void { - const b = self.b; - - for (¶m_sets) |*params| { - const resolved_target = b.resolveTargetQuery(params.target); - const target = &resolved_target.result; - - if (!self.options.test_extra_targets and params.extra_target) continue; - - if (self.options.skip_non_native and !std.zig.target.isNative(&resolved_target.query, &resolved_target.result, &b.graph.host.result)) continue; - - if (self.options.skip_freebsd and target.os.tag == .freebsd) continue; - if (self.options.skip_netbsd and target.os.tag == .netbsd) continue; - if (self.options.skip_openbsd and target.os.tag == .openbsd) continue; - if (self.options.skip_windows and target.os.tag == .windows) continue; - if (self.options.skip_darwin and target.os.tag.isDarwin()) continue; - if (self.options.skip_linux and target.os.tag == .linux) continue; - - const would_use_llvm = tests.wouldUseLlvm(params.use_llvm, params.target, params.optimize); - if (self.options.skip_llvm and would_use_llvm) continue; - - const triple_txt = resolved_target.query.zigTriple(b.allocator) catch @panic("OOM"); - - if (self.options.test_target_filters.len > 0) { - for (self.options.test_target_filters) |filter| { - if (std.mem.find(u8, triple_txt, filter) != null) break; - } else continue; - } - - if (self.options.skip_libc and std.os.targetRequiresLibC(target)) - continue; - - for (self.options.optimize_modes) |optimize| { - if (optimize == params.optimize) break; - } else return; - - error_traces_cases.addCases(self, params, &resolved_target.result); - } -} - -/// Called from test/error_traces.zig -pub fn addCase(self: *ErrorTrace, case: Case) void { - const b = self.b; - const params = case.params; - const target = case.target; - const target_query = params.target; - - const triple: ?[]const u8 = if (target_query.isNative()) null else t: { - break :t target_query.zigTriple(self.b.graph.arena) catch @panic("OOM"); - }; - - const error_tracing: bool = tracing: { - if (params.optimize == .debug) break :tracing true; - if (params.use_llvm == false) break :tracing true; - if (params.optimize == .small) break :tracing false; - for (case.disable_trace_optimized) |disable| { - const d_arch, const d_os = disable; - if (target.cpu.arch == d_arch and target.os.tag == d_os) { - // This particular configuration cannot do error tracing in optimized LLVM builds. - break :tracing false; - } - } - break :tracing true; - }; - - const backend_string = if (params.use_llvm == true) - " llvm" - else if (params.use_llvm == false) - " selfhosted" - else - ""; - - const annotated_case_name = b.fmt("check {s} ({s} {t}{s}{s})", .{ - case.name, - triple orelse "native", - params.optimize, - backend_string, - if (params.use_new_linker == true) - " new_linker" - else if (params.use_lld == true) - " lld" - else - "", - }); - if (self.options.test_filters.len > 0) { - for (self.options.test_filters) |test_filter| { - if (mem.find(u8, annotated_case_name, test_filter)) |_| break; - } else return; - } - - const write_files = b.addWriteFiles(); - const source_zig = write_files.add("source.zig", case.source); - const exe = b.addExecutable(.{ - .name = "test", - .root_module = b.createModule(.{ - .root_source_file = source_zig, - .optimize = params.optimize, - .target = .{ .result = target.*, .query = target_query }, - .error_tracing = error_tracing, - .strip = false, - }), - .use_llvm = params.use_llvm, - .use_lld = params.use_lld, - }); - exe.use_new_linker = params.use_new_linker; - exe.bundle_ubsan_rt = false; - - const run = b.addRunArtifact(exe); - run.skip_foreign_checks = true; - run.removeEnvironmentVariable("CLICOLOR_FORCE"); - run.setEnvironmentVariable("NO_COLOR", "1"); - run.expectExitCode(1); - run.expectStdOutEqual(""); - - const expected_stderr = switch (error_tracing) { - true => b.fmt("error: {s}\n{s}\n", .{ case.expect_error, case.expect_trace }), - false => b.fmt("error: {s}\n", .{case.expect_error}), - }; - - const check_run = b.addRunArtifact(self.convert_exe); - check_run.skip_foreign_checks = true; - check_run.setName(annotated_case_name); - check_run.addFileArg(run.captureStdErr(.{})); - check_run.expectStdOutEqual(expected_stderr); - - self.step.dependOn(&check_run.step); -} diff --git a/test/tests.zig b/test/tests.zig index e396bb4628..ea44db7385 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -10,7 +10,6 @@ const libc = @import("libc.zig"); const link = @import("link.zig"); // Implementations -pub const ErrorTracesContext = @import("src/ErrorTrace.zig"); pub const StackTracesContext = @import("src/StackTrace.zig"); pub const LibcContext = @import("src/Libc.zig"); pub const LinkContext = @import("src/Link.zig"); @@ -2274,30 +2273,6 @@ pub fn addStackTraceTests(b: *std.Build, options: StackTracesContext.Options) *S return step; } -pub fn addErrorTraceTests(b: *std.Build, options: ErrorTracesContext.Options) *Step { - const step = b.step("test-error-traces", "Run the error trace tests"); - - const convert_exe = b.addExecutable(.{ - .name = "convert-stack-trace", - .root_module = b.createModule(.{ - .root_source_file = b.path("test/src/convert-stack-trace.zig"), - .target = b.graph.host, - .optimize = .debug, - }), - }); - - const error_traces_context = b.allocator.create(ErrorTracesContext) catch @panic("OOM"); - error_traces_context.* = .{ - .b = b, - .step = step, - .options = options, - .convert_exe = convert_exe, - }; - error_traces_context.addCases(); - - return step; -} - pub fn addStandaloneTests( b: *std.Build, optimize_modes: []const OptimizeMode,