master
  1//! This namespace is the default one used by the Zig compiler to emit various
  2//! kinds of safety panics, due to the logic in `std.builtin.panic`.
  3//!
  4//! Since Zig does not have interfaces, this file serves as an example template
  5//! for users to provide their own alternative panic handling.
  6//!
  7//! As an alternative, see `std.debug.FullPanic`.
  8
  9const std = @import("../std.zig");
 10
 11/// Prints the message to stderr without a newline and then traps.
 12///
 13/// Explicit calls to `@panic` lower to calling this function.
 14pub fn call(msg: []const u8, ra: ?usize) noreturn {
 15    @branchHint(.cold);
 16    _ = ra;
 17    std.debug.lockStdErr();
 18    const stderr: std.fs.File = .stderr();
 19    stderr.writeAll(msg) catch {};
 20    @trap();
 21}
 22
 23pub fn sentinelMismatch(expected: anytype, found: @TypeOf(expected)) noreturn {
 24    _ = found;
 25    call("sentinel mismatch", null);
 26}
 27
 28pub fn unwrapError(err: anyerror) noreturn {
 29    _ = &err;
 30    call("attempt to unwrap error", null);
 31}
 32
 33pub fn outOfBounds(index: usize, len: usize) noreturn {
 34    _ = index;
 35    _ = len;
 36    call("index out of bounds", null);
 37}
 38
 39pub fn startGreaterThanEnd(start: usize, end: usize) noreturn {
 40    _ = start;
 41    _ = end;
 42    call("start index is larger than end index", null);
 43}
 44
 45pub fn inactiveUnionField(active: anytype, accessed: @TypeOf(active)) noreturn {
 46    _ = accessed;
 47    call("access of inactive union field", null);
 48}
 49
 50pub fn sliceCastLenRemainder(src_len: usize) noreturn {
 51    _ = src_len;
 52    call("slice length does not divide exactly into destination elements", null);
 53}
 54
 55pub fn reachedUnreachable() noreturn {
 56    call("reached unreachable code", null);
 57}
 58
 59pub fn unwrapNull() noreturn {
 60    call("attempt to use null value", null);
 61}
 62
 63pub fn castToNull() noreturn {
 64    call("cast causes pointer to be null", null);
 65}
 66
 67pub fn incorrectAlignment() noreturn {
 68    call("incorrect alignment", null);
 69}
 70
 71pub fn invalidErrorCode() noreturn {
 72    call("invalid error code", null);
 73}
 74
 75pub fn integerOutOfBounds() noreturn {
 76    call("integer does not fit in destination type", null);
 77}
 78
 79pub fn integerOverflow() noreturn {
 80    call("integer overflow", null);
 81}
 82
 83pub fn shlOverflow() noreturn {
 84    call("left shift overflowed bits", null);
 85}
 86
 87pub fn shrOverflow() noreturn {
 88    call("right shift overflowed bits", null);
 89}
 90
 91pub fn divideByZero() noreturn {
 92    call("division by zero", null);
 93}
 94
 95pub fn exactDivisionRemainder() noreturn {
 96    call("exact division produced remainder", null);
 97}
 98
 99pub fn integerPartOutOfBounds() noreturn {
100    call("integer part of floating point value out of bounds", null);
101}
102
103pub fn corruptSwitch() noreturn {
104    call("switch on corrupt value", null);
105}
106
107pub fn shiftRhsTooBig() noreturn {
108    call("shift amount is greater than the type size", null);
109}
110
111pub fn invalidEnumValue() noreturn {
112    call("invalid enum value", null);
113}
114
115pub fn forLenMismatch() noreturn {
116    call("for loop over objects with non-equal lengths", null);
117}
118
119pub fn copyLenMismatch() noreturn {
120    call("source and destination have non-equal lengths", null);
121}
122
123pub fn memcpyAlias() noreturn {
124    call("@memcpy arguments alias", null);
125}
126
127pub fn noreturnReturned() noreturn {
128    call("'noreturn' function returned", null);
129}