aboutsummaryrefslogtreecommitdiff
path: root/lib/std/debug.zig
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/debug.zig')
-rw-r--r--lib/std/debug.zig123
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.