diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-01-24 02:19:28 +0000 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2025-01-24 19:29:51 +0000 |
| commit | 83991efe10d92c4b920d7b7fc75be98ed7854ad7 (patch) | |
| tree | 7137ee5eb01517aede1c5503c29a2003fca71f80 /lib/std/debug.zig | |
| parent | b3d9b0e3f6bab5d662ec385cf754872f8a90607f (diff) | |
| download | zig-83991efe10d92c4b920d7b7fc75be98ed7854ad7.tar.gz zig-83991efe10d92c4b920d7b7fc75be98ed7854ad7.zip | |
compiler: yet more panic handler changes
* `std.builtin.Panic` -> `std.builtin.panic`, because it is a namespace.
* `root.Panic` -> `root.panic` for the same reason. There are type
checks so that we still allow the legacy `pub fn panic` strategy in
the 0.14.0 release.
* `std.debug.SimplePanic` -> `std.debug.simple_panic`, same reason.
* `std.debug.NoPanic` -> `std.debug.no_panic`, same reason.
* `std.debug.FormattedPanic` is now a function `std.debug.FullPanic`
which takes as input a `panicFn` and returns a namespace with all the
panic functions. This handles the incredibly common case of just
wanting to override how the message is printed, whilst keeping nice
formatted panics.
* Remove `std.builtin.panic.messages`; now, every safety panic has its
own function. This reduces binary bloat, as calls to these functions
no longer need to prepare any arguments (aside from the error return
trace).
* Remove some legacy declarations, since a zig1.wasm update has
happened. Most of these were related to the panic handler, but a quick
grep for "zig1" brought up a couple more results too.
Also, add some missing type checks to Sema.
Resolves: #22584
formatted -> full
Diffstat (limited to 'lib/std/debug.zig')
| -rw-r--r-- | lib/std/debug.zig | 123 |
1 files changed, 119 insertions, 4 deletions
diff --git a/lib/std/debug.zig b/lib/std/debug.zig index 3624d6d3e7..0ff51a7339 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -21,9 +21,124 @@ pub const SelfInfo = @import("debug/SelfInfo.zig"); pub const Info = @import("debug/Info.zig"); pub const Coverage = @import("debug/Coverage.zig"); -pub const FormattedPanic = @import("debug/FormattedPanic.zig"); -pub const SimplePanic = @import("debug/SimplePanic.zig"); -pub const NoPanic = @import("debug/NoPanic.zig"); +pub const simple_panic = @import("debug/simple_panic.zig"); +pub const no_panic = @import("debug/no_panic.zig"); + +/// A fully-featured panic handler namespace which lowers all panics to calls to `panicFn`. +/// Safety panics will use formatted printing to provide a meaningful error message. +/// The signature of `panicFn` should match that of `defaultPanic`. +pub fn FullPanic(comptime panicFn: fn ([]const u8, ?*std.builtin.StackTrace, ?usize) noreturn) type { + return struct { + pub const call = panicFn; + pub fn sentinelMismatch(expected: anytype, found: @TypeOf(expected)) noreturn { + @branchHint(.cold); + std.debug.panicExtra(null, @returnAddress(), "sentinel mismatch: expected {any}, found {any}", .{ + expected, found, + }); + } + pub fn unwrapError(ert: ?*std.builtin.StackTrace, err: anyerror) noreturn { + @branchHint(.cold); + std.debug.panicExtra(ert, @returnAddress(), "attempt to unwrap error: {s}", .{@errorName(err)}); + } + pub fn outOfBounds(index: usize, len: usize) noreturn { + @branchHint(.cold); + std.debug.panicExtra(null, @returnAddress(), "index out of bounds: index {d}, len {d}", .{ index, len }); + } + pub fn startGreaterThanEnd(start: usize, end: usize) noreturn { + @branchHint(.cold); + std.debug.panicExtra(null, @returnAddress(), "start index {d} is larger than end index {d}", .{ start, end }); + } + pub fn inactiveUnionField(active: anytype, accessed: @TypeOf(active)) noreturn { + @branchHint(.cold); + std.debug.panicExtra(null, @returnAddress(), "access of union field '{s}' while field '{s}' is active", .{ + @tagName(accessed), @tagName(active), + }); + } + pub fn reachedUnreachable() noreturn { + @branchHint(.cold); + call("reached unreachable code", null, @returnAddress()); + } + pub fn unwrapNull() noreturn { + @branchHint(.cold); + call("attempt to use null value", null, @returnAddress()); + } + pub fn castToNull() noreturn { + @branchHint(.cold); + call("cast causes pointer to be null", null, @returnAddress()); + } + pub fn incorrectAlignment() noreturn { + @branchHint(.cold); + call("incorrect alignment", null, @returnAddress()); + } + pub fn invalidErrorCode() noreturn { + @branchHint(.cold); + call("invalid error code", null, @returnAddress()); + } + pub fn castTruncatedData() noreturn { + @branchHint(.cold); + call("integer cast truncated bits", null, @returnAddress()); + } + pub fn negativeToUnsigned() noreturn { + @branchHint(.cold); + call("attempt to cast negative value to unsigned integer", null, @returnAddress()); + } + pub fn integerOverflow() noreturn { + @branchHint(.cold); + call("integer overflow", null, @returnAddress()); + } + pub fn shlOverflow() noreturn { + @branchHint(.cold); + call("left shift overflowed bits", null, @returnAddress()); + } + pub fn shrOverflow() noreturn { + @branchHint(.cold); + call("right shift overflowed bits", null, @returnAddress()); + } + pub fn divideByZero() noreturn { + @branchHint(.cold); + call("division by zero", null, @returnAddress()); + } + pub fn exactDivisionRemainder() noreturn { + @branchHint(.cold); + call("exact division produced remainder", null, @returnAddress()); + } + pub fn integerPartOutOfBounds() noreturn { + @branchHint(.cold); + call("integer part of floating point value out of bounds", null, @returnAddress()); + } + pub fn corruptSwitch() noreturn { + @branchHint(.cold); + call("switch on corrupt value", null, @returnAddress()); + } + pub fn shiftRhsTooBig() noreturn { + @branchHint(.cold); + call("shift amount is greater than the type size", null, @returnAddress()); + } + pub fn invalidEnumValue() noreturn { + @branchHint(.cold); + call("invalid enum value", null, @returnAddress()); + } + pub fn forLenMismatch() noreturn { + @branchHint(.cold); + call("for loop over objects with non-equal lengths", null, @returnAddress()); + } + pub fn memcpyLenMismatch() noreturn { + @branchHint(.cold); + call("@memcpy arguments have non-equal lengths", null, @returnAddress()); + } + pub fn memcpyAlias() noreturn { + @branchHint(.cold); + call("@memcpy arguments alias", null, @returnAddress()); + } + pub fn noreturnReturned() noreturn { + @branchHint(.cold); + call("'noreturn' function returned", null, @returnAddress()); + } + + /// To be deleted after zig1.wasm update. + pub const messages = simple_panic.messages; + }; +} /// Unresolved source locations can be represented with a single `usize` that /// corresponds to a virtual memory address of the program counter. Combined @@ -441,7 +556,7 @@ pub fn panicExtra( break :blk &buf; }, }; - std.builtin.Panic.call(msg, trace, ret_addr); + std.builtin.panic.call(msg, trace, ret_addr); } /// Non-zero whenever the program triggered a panic. |
