authorgravatar for davidgmbb@gmail.comDavid Gonzalez Martin <davidgmbb@gmail.com> 2026-04-13 12:17:51+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-04-13 19:58:01+02:00
log37a20d39846b9056dbf6aa46cb2bed3196b56b61
tree17600698ce91281031d512267eabf4f33b182104
parentc457939f104595d675974f9e820ccf4187bf8e95

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

4 files changed, 15 insertions(+), 15 deletions(-)

lib/std/os/windows.zig+2-2
......@@ -3952,7 +3952,7 @@ inline fn MAKELANGID(p: c_ushort, s: c_ushort) LANGID {
39523952/// and you get an unexpected error.
39533953pub fn unexpectedError(err: Win32Error) UnexpectedError {
39543954 @branchHint(.cold);
3955 if (std.posix.unexpected_error_tracing) {
3955 if (std.options.unexpected_error_tracing) {
39563956 std.debug.print("error.Unexpected: GetLastError({d}): {t}\n", .{ err, err });
39573957 std.debug.dumpCurrentStackTrace(.{ .first_address = @returnAddress() });
39583958 }
......@@ -3962,7 +3962,7 @@ pub fn unexpectedError(err: Win32Error) UnexpectedError {
39623962/// Call this when you made a windows NtDll call
39633963/// and you get an unexpected status.
39643964pub fn unexpectedStatus(status: NTSTATUS) UnexpectedError {
3965 if (std.posix.unexpected_error_tracing) {
3965 if (std.options.unexpected_error_tracing) {
39663966 std.debug.print("error.Unexpected NTSTATUS=0x{x} ({s})\n", .{
39673967 @intFromEnum(status),
39683968 std.enums.tagName(NTSTATUS, status) orelse "<unnamed>",
lib/std/posix.zig+2-12
......@@ -686,7 +686,7 @@ pub fn munmap(memory: []align(page_size_min) const u8) void {
686686 .SUCCESS => return,
687687 .INVAL => unreachable, // Invalid parameters.
688688 .NOMEM => unreachable, // Attempted to unmap a region in the middle of an existing mapping.
689 else => |e| if (unexpected_error_tracing) {
689 else => |e| if (std.options.unexpected_error_tracing) {
690690 std.debug.panic("unexpected errno: {d} ({t})", .{ @intFromEnum(e), e });
691691 } else unreachable,
692692 }
......@@ -1662,22 +1662,12 @@ pub fn name_to_handle_atZ(
16621662
16631663pub const lfs64_abi = native_os == .linux and builtin.link_libc and (builtin.abi.isGnu() or builtin.abi.isAndroid());
16641664
1665/// Whether or not `error.Unexpected` will print its value and a stack trace.
1666///
1667/// If this happens the fix is to add the error code to the corresponding
1668/// switch expression, possibly introduce a new error in the error set, and
1669/// send a patch to Zig.
1670pub const unexpected_error_tracing = builtin.mode == .Debug and switch (builtin.zig_backend) {
1671 .stage2_llvm, .stage2_x86_64 => true,
1672 else => false,
1673};
1674
16751665pub const UnexpectedError = std.Io.UnexpectedError;
16761666
16771667/// Call this when you made a syscall or something that sets errno
16781668/// and you get an unexpected error.
16791669pub fn unexpectedErrno(err: E) UnexpectedError {
1680 if (unexpected_error_tracing) {
1670 if (std.options.unexpected_error_tracing) {
16811671 std.debug.print("unexpected errno: {d}\n", .{@intFromEnum(err)});
16821672 std.debug.dumpCurrentStackTrace(.{});
16831673 }
lib/std/std.zig+10
......@@ -180,6 +180,16 @@ pub const Options = struct {
180180 /// Allows disabling networking in std.Io implementations.
181181 networking: bool = true,
182182
183 /// Whether or not `error.Unexpected` will print its value and a stack trace.
184 ///
185 /// If this happens the fix is to add the error code to the corresponding
186 /// switch expression, possibly introduce a new error in the error set, and
187 /// send a patch to Zig.
188 unexpected_error_tracing: bool = @import("builtin").mode == .Debug and switch (@import("builtin").zig_backend) {
189 .stage2_llvm, .stage2_x86_64 => true,
190 else => false,
191 },
192
183193 /// TODO This is a separate decl instead of a field as a workaround around
184194 /// compilation errors due to zig not being lazy enough.
185195 pub const logTerminalMode: fn () Io.Terminal.Mode = log.defaultTerminalMode;
src/link/MachO.zig+1-1
......@@ -5133,7 +5133,7 @@ pub fn getKernError(err: std.c.kern_return_t) KernE {
51335133}
51345134
51355135pub fn unexpectedKernError(err: KernE) std.posix.UnexpectedError {
5136 if (std.posix.unexpected_error_tracing) {
5136 if (std.options.unexpected_error_tracing) {
51375137 std.debug.print("unexpected error: {d}\n", .{@intFromEnum(err)});
51385138 std.debug.dumpCurrentStackTrace(.{});
51395139 }