zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 37a20d39846b9056dbf6aa46cb2bed3196b56b61 (tree)
parent c457939f104595d675974f9e820ccf4187bf8e95
Author: David Gonzalez Martin <davidgmbb@gmail.com>
Date:   Mon, 13 Apr 2026 12:17:51 +0200

Allow the user to override unexpected error trace

This is the only bit left in the standard library where stack trace writing
code is pulled to the binary even if the user doesn't want it

Diffstat:
Mlib/std/os/windows.zig | 4++--
Mlib/std/posix.zig | 14++------------
Mlib/std/std.zig | 10++++++++++
Msrc/link/MachO.zig | 2+-
4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig @@ -3952,7 +3952,7 @@ inline fn MAKELANGID(p: c_ushort, s: c_ushort) LANGID { /// and you get an unexpected error. pub fn unexpectedError(err: Win32Error) UnexpectedError { @branchHint(.cold); - if (std.posix.unexpected_error_tracing) { + if (std.options.unexpected_error_tracing) { std.debug.print("error.Unexpected: GetLastError({d}): {t}\n", .{ err, err }); std.debug.dumpCurrentStackTrace(.{ .first_address = @returnAddress() }); } @@ -3962,7 +3962,7 @@ pub fn unexpectedError(err: Win32Error) UnexpectedError { /// Call this when you made a windows NtDll call /// and you get an unexpected status. pub fn unexpectedStatus(status: NTSTATUS) UnexpectedError { - if (std.posix.unexpected_error_tracing) { + if (std.options.unexpected_error_tracing) { std.debug.print("error.Unexpected NTSTATUS=0x{x} ({s})\n", .{ @intFromEnum(status), std.enums.tagName(NTSTATUS, status) orelse "<unnamed>", diff --git a/lib/std/posix.zig b/lib/std/posix.zig @@ -686,7 +686,7 @@ pub fn munmap(memory: []align(page_size_min) const u8) void { .SUCCESS => return, .INVAL => unreachable, // Invalid parameters. .NOMEM => unreachable, // Attempted to unmap a region in the middle of an existing mapping. - else => |e| if (unexpected_error_tracing) { + else => |e| if (std.options.unexpected_error_tracing) { std.debug.panic("unexpected errno: {d} ({t})", .{ @intFromEnum(e), e }); } else unreachable, } @@ -1662,22 +1662,12 @@ pub fn name_to_handle_atZ( pub const lfs64_abi = native_os == .linux and builtin.link_libc and (builtin.abi.isGnu() or builtin.abi.isAndroid()); -/// Whether or not `error.Unexpected` will print its value and a stack trace. -/// -/// If this happens the fix is to add the error code to the corresponding -/// switch expression, possibly introduce a new error in the error set, and -/// send a patch to Zig. -pub const unexpected_error_tracing = builtin.mode == .Debug and switch (builtin.zig_backend) { - .stage2_llvm, .stage2_x86_64 => true, - else => false, -}; - pub const UnexpectedError = std.Io.UnexpectedError; /// Call this when you made a syscall or something that sets errno /// and you get an unexpected error. pub fn unexpectedErrno(err: E) UnexpectedError { - if (unexpected_error_tracing) { + if (std.options.unexpected_error_tracing) { std.debug.print("unexpected errno: {d}\n", .{@intFromEnum(err)}); std.debug.dumpCurrentStackTrace(.{}); } diff --git a/lib/std/std.zig b/lib/std/std.zig @@ -180,6 +180,16 @@ pub const Options = struct { /// Allows disabling networking in std.Io implementations. networking: bool = true, + /// Whether or not `error.Unexpected` will print its value and a stack trace. + /// + /// If this happens the fix is to add the error code to the corresponding + /// switch expression, possibly introduce a new error in the error set, and + /// send a patch to Zig. + unexpected_error_tracing: bool = @import("builtin").mode == .Debug and switch (@import("builtin").zig_backend) { + .stage2_llvm, .stage2_x86_64 => true, + else => false, + }, + /// TODO This is a separate decl instead of a field as a workaround around /// compilation errors due to zig not being lazy enough. pub const logTerminalMode: fn () Io.Terminal.Mode = log.defaultTerminalMode; diff --git a/src/link/MachO.zig b/src/link/MachO.zig @@ -5133,7 +5133,7 @@ pub fn getKernError(err: std.c.kern_return_t) KernE { } pub fn unexpectedKernError(err: KernE) std.posix.UnexpectedError { - if (std.posix.unexpected_error_tracing) { + if (std.options.unexpected_error_tracing) { std.debug.print("unexpected error: {d}\n", .{@intFromEnum(err)}); std.debug.dumpCurrentStackTrace(.{}); }