Errors and panics

Errors and panics

Panic results, crash and assertion support.

Contents

NameSummary
AssertFailThe assert failure record: PanicLowering's entry point for a failed assert.
unwrap_failedCrashes with a compiler-built message; the UnwrapFailed lang item.
crash_withWrites msg and a newline to stderr (nothing when msg is empty), then aborts.
PanicCatchPanicLowering's catch entry point (the PanicCatch lang item).
PanicRaisePanicLowering's raise entry point (the PanicRaise lang item).
PanicWhat a panic function returns (PANIC.md §1.1): Ok with the declared return type, else one Err per type in the function's panic set.

AssertFail

struct AssertFail

The assert failure record: PanicLowering's entry point for a failed assert.

Compiler-facing lang item; user code does not name it. The file is assert_fail.k because assert is a keyword.

unwrap_failed

fn unwrap_failed(msg: *const u8, len: usize) -> !

Crashes with a compiler-built message; the UnwrapFailed lang item.

Called for unwrap! on null, a checked as into a closed enum that misses, a script global read before its line ran, and an asserting downcast p as *D on the wrong dynamic type. msg[0..len) is a literal the lowering built whole (the expression as written and its file:line:col), so nothing is formatted at crash time. The runtime writes it and a newline to stderr and aborts. -> ! makes the failing arm Never. Not a panic: no try catches it and it is in no panic set.

Parameters

  • msg: Pointer to len bytes of message.

Safety

msg must be valid for reads of len bytes (a null msg is an empty message).

crash_with

fn crash_with(msg: string = "") -> !

Writes msg and a newline to stderr (nothing when msg is empty), then aborts. This is what crash! calls.

A runtime string is fine: its data pointer and length are read once and handed to the runtime, which writes at most KAIRO_RT_CRASH_MAX (4096) bytes of them and runs no string method. A literal costs no allocation. -> !, so a call ends its path for the flow checks. Not a panic.

See also

crash!

PanicCatch

struct PanicCatch

PanicLowering's catch entry point (the PanicCatch lang item).

Compiler-facing; user code does not name it. Layout only: payload is an opaque pointer.

PanicRaise

struct PanicRaise

PanicLowering's raise entry point (the PanicRaise lang item).

Compiler-facing; user code does not name it. Layout only: payload is an opaque pointer.

Panic

enum <T, ...E> Panic

What a panic function returns (PANIC.md §1.1): Ok with the declared return type, else one Err per type in the function's panic set. The compiler forms every instance (the set is deduplicated and sorted by type id) and lays it out itself: each tag is a type id, word 0 holds the tag and the panic site, and the payloads start at max(8, alignof(...)). User code does not name it (SC116E).

Panic.Ok

Ok

The function returned normally; value is its declared return value.

Panic.Err

Err

The function panicked with an error of one of the types in its panic set; one Err case exists per type in E.

Generated from Kairo source, Apache-2.0 WITH AdditionRef-Kairo-Runtime-exception.