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.
| Function | Behaviour |
|---|---|
panic | Panic with the message and source location. |
assert | Panic when the condition is false. Always checked. |
unreachable | For a path you believe cannot be taken. |
todo | For 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;
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;
function assert(condition: boolean, message: string) -> void;Panic if condition is false. Always checked.
function unreachable() -> never;
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;
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.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function parse_digit(c: char) -> i32(parameter) c: charif (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
(parameter) c: charfunction panic(message: string, loc: intrinsics::Location) -> neverPanic 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.
intrinsic function location() -> Locationreturn value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
value as TypeExplicit conversion: between numeric types, or reinterpreting one pointer type as another. Cryo never converts implicitly, and as inserts no range checks - a narrowing cast is the programmer's responsibility.
function main() -> intfunction assert(condition: boolean, message: string) -> voidPanic if condition is false. Always checked.
function parse_digit(c: char) -> i32primitive neverThe type of an expression that does not return. Only valid as a return type, written by functions that always panic, trap, or diverge. 0 bytes.
primitive typefunction unreachable() -> neverMark 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.
std::core::panicfunction todo() -> neverStub a function you'll implement later. Reading better than inlining panic("not yet implemented", ...) with the place is its whole job.
for (item in iterable) { ... }Separates the loop variable from the sequence in a for loop. The sequence is evaluated once; the loop lowers to loop { match (it.next()) { Some(x) => .., None => break } }.
function panic(message: string, loc: intrinsics::Location) -> neverPanic 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.
(parameter) message: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
(parameter) loc: intrinsics::Locationfunction assert(condition: boolean, message: string) -> voidPanic if condition is false. Always checked.
(parameter) condition: booleanfunction unreachable() -> neverMark 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() -> neverStub a function you'll implement later. Reading better than inlining panic("not yet implemented", ...) with the place is its whole job.
function name(count: i32, args...)A variadic parameter: the trailing bucket receives any number of further arguments with C's calling convention, so the function stays ABI-compatible with the printf family. Wrap the bucket in VariadicArguments to read typed values from it.