Skip to content
CryoCryo home
Stdlibcore

panic

In the prelude · source

The panic helpers every file reaches bare, through the prelude. panic is typed never, so the compiler treats the call as divergent structurally, with no special-casing on the symbol name; it forwards to the panic intrinsic.

FunctionBehaviour
panicPanic with the message and source location.
assertPanic when the condition is false. Always checked.
unreachableFor a path you believe cannot be taken.
todoFor a function you have not written yet.

Pass intrinsics::location() to panic for the place — written at the call, or, in a ![caller_location] function, its caller's; the helpers do it for you. A panic reports panicked at <file>:<line>:<column> in <function>: <message> on stderr; what happens next is the build's panic strategy. Under unwind, the default for a hosted build, each frame's destructors run on the way out, catch_unwind can stop it, and a panic nothing stops exits with code 101. Under abort the process exits with code 101 at once.

import std::core::intrinsics;

function parse_digit(c: char) -> i32 {
    if (c < '0' || c > '9') {
        panic("not a digit", intrinsics::location());
    }
    return (c as i32) - ('0' as i32);
}

function main() -> int {
    assert(parse_digit('7') == 7, "digits read as themselves");
    return 0;
}

Free Functions

function panic(message: string, loc: intrinsics::Location) -> never;

Panic with message, reporting the place loc: intrinsics::location() written at the call, or in a ![caller_location] function its caller's. The helpers below do that automatically.

function assert(condition: boolean, message: string) -> void;

Panic if condition is false. Always checked.

function unreachable() -> never;

Mark a code path the programmer believes cannot be reached. If it ever is, the program aborts with a clear message rather than executing wrong code.

function todo() -> never;

Stub a function you'll implement later. Reading better than inlining panic("not yet implemented", ...) with the place is its whole job.