| 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 | |
| 9 | const 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. |
| 14 | pub fn call(msg: []const u8, ra: ?usize) noreturn { |
| 15 | @branchHint(.cold); |
| 16 | _ = ra; |
| 17 | const stderr_writer = &std.debug.lockStderr(&.{}).file_writer.interface; |
| 18 | stderr_writer.writeAll(msg) catch {}; |
| 19 | @trap(); |
| 20 | } |
| 21 | |
| 22 | pub fn sentinelMismatch(expected: anytype, found: @TypeOf(expected)) noreturn { |
| 23 | @branchHint(.cold); |
| 24 | _ = found; |
| 25 | call("sentinel mismatch", null); |
| 26 | } |
| 27 | |
| 28 | pub fn unwrapError(err: anyerror) noreturn { |
| 29 | @branchHint(.cold); |
| 30 | _ = &err; |
| 31 | call("attempt to unwrap error", null); |
| 32 | } |
| 33 | |
| 34 | pub fn outOfBounds(index: usize, len: usize) noreturn { |
| 35 | @branchHint(.cold); |
| 36 | _ = index; |
| 37 | _ = len; |
| 38 | call("index out of bounds", null); |
| 39 | } |
| 40 | |
| 41 | pub fn startGreaterThanEnd(start: usize, end: usize) noreturn { |
| 42 | @branchHint(.cold); |
| 43 | _ = start; |
| 44 | _ = end; |
| 45 | call("start index is larger than end index", null); |
| 46 | } |
| 47 | |
| 48 | pub fn inactiveUnionField(active: anytype, accessed: @TypeOf(active)) noreturn { |
| 49 | @branchHint(.cold); |
| 50 | _ = accessed; |
| 51 | call("access of inactive union field", null); |
| 52 | } |
| 53 | |
| 54 | pub fn sliceCastLenRemainder(src_len: usize) noreturn { |
| 55 | @branchHint(.cold); |
| 56 | _ = src_len; |
| 57 | call("slice length does not divide exactly into destination elements", null); |
| 58 | } |
| 59 | |
| 60 | pub fn reachedUnreachable() noreturn { |
| 61 | @branchHint(.cold); |
| 62 | call("reached unreachable code", null); |
| 63 | } |
| 64 | |
| 65 | pub fn unwrapNull() noreturn { |
| 66 | @branchHint(.cold); |
| 67 | call("attempt to use null value", null); |
| 68 | } |
| 69 | |
| 70 | pub fn castToNull() noreturn { |
| 71 | @branchHint(.cold); |
| 72 | call("cast causes pointer to be null", null); |
| 73 | } |
| 74 | |
| 75 | pub fn incorrectAlignment() noreturn { |
| 76 | @branchHint(.cold); |
| 77 | call("incorrect alignment", null); |
| 78 | } |
| 79 | |
| 80 | pub fn invalidErrorCode() noreturn { |
| 81 | @branchHint(.cold); |
| 82 | call("invalid error code", null); |
| 83 | } |
| 84 | |
| 85 | pub fn unexpectedErrorCode(err: anyerror) noreturn { |
| 86 | @branchHint(.cold); |
| 87 | _ = err; |
| 88 | call("unexpected error code", null); |
| 89 | } |
| 90 | |
| 91 | pub fn integerOutOfBounds() noreturn { |
| 92 | @branchHint(.cold); |
| 93 | call("integer does not fit in destination type", null); |
| 94 | } |
| 95 | |
| 96 | pub fn integerOverflow() noreturn { |
| 97 | @branchHint(.cold); |
| 98 | call("integer overflow", null); |
| 99 | } |
| 100 | |
| 101 | pub fn shlOverflow() noreturn { |
| 102 | @branchHint(.cold); |
| 103 | call("left shift overflowed bits", null); |
| 104 | } |
| 105 | |
| 106 | pub fn shrOverflow() noreturn { |
| 107 | @branchHint(.cold); |
| 108 | call("right shift overflowed bits", null); |
| 109 | } |
| 110 | |
| 111 | pub fn divideByZero() noreturn { |
| 112 | @branchHint(.cold); |
| 113 | call("division by zero", null); |
| 114 | } |
| 115 | |
| 116 | pub fn exactDivisionRemainder() noreturn { |
| 117 | @branchHint(.cold); |
| 118 | call("exact division produced remainder", null); |
| 119 | } |
| 120 | |
| 121 | pub fn integerPartOutOfBounds() noreturn { |
| 122 | @branchHint(.cold); |
| 123 | call("integer part of floating point value out of bounds", null); |
| 124 | } |
| 125 | |
| 126 | pub fn corruptSwitch() noreturn { |
| 127 | @branchHint(.cold); |
| 128 | call("switch on corrupt value", null); |
| 129 | } |
| 130 | |
| 131 | pub fn shiftRhsTooBig() noreturn { |
| 132 | @branchHint(.cold); |
| 133 | call("shift amount is greater than the type size", null); |
| 134 | } |
| 135 | |
| 136 | pub fn invalidEnumValue() noreturn { |
| 137 | @branchHint(.cold); |
| 138 | call("invalid enum value", null); |
| 139 | } |
| 140 | |
| 141 | pub fn forLenMismatch() noreturn { |
| 142 | @branchHint(.cold); |
| 143 | call("for loop over objects with non-equal lengths", null); |
| 144 | } |
| 145 | |
| 146 | pub fn copyLenMismatch() noreturn { |
| 147 | @branchHint(.cold); |
| 148 | call("source and destination have non-equal lengths", null); |
| 149 | } |
| 150 | |
| 151 | pub fn memcpyAlias() noreturn { |
| 152 | @branchHint(.cold); |
| 153 | call("@memcpy arguments alias", null); |
| 154 | } |
| 155 | |
| 156 | pub fn noreturnReturned() noreturn { |
| 157 | @branchHint(.cold); |
| 158 | call("'noreturn' function returned", null); |
| 159 | } |
| 160 | |
| 161 | pub fn loadUninstantiableType() noreturn { |
| 162 | call("attempt to load uninstantiable type", null); |
| 163 | } |