Landin library reference discuss source

Shared: available on every enabled target.

The compiler's five panic atoms and their shared panic-kind domain.

Checked range, arithmetic and conversion faults, and an unreachable path, use these atoms.

An entry-module panic handler has the language-specified nonreturning signature. A panic is distinct from a recoverable allocator or I/O error.

panic.require(holds) is C's assert: when holds is false it stops the program as a failed check does, and the stop is reported at the line that called require. The compiler fills its caller parameter, a panic.call_site, with the place of the call. A check of your own takes a caller parameter of that type too and passes it on, panic.require(holds, where: where).

Items

public call_site: type = struct
    file_id: u32
    line: u32
    column: u32
end call_site

Where a call was written: the struct [1040] gives a caller parameter, whose fields the compiler fills, line and column counting from one. Its file is a number that the table written beside the program names. This is not the number a panic handler receives as its site [1670].

public panic_kind: type = out_of_range | overflow | bad_conversion | unreachable | stack_exhausted

The compiler's five panic atoms. A program may supply the specified nonreturning panic handler; these failures are not allocator error returns.

bad_conversion

atom core/panic/panic.ldn:9 ↑ index
public bad_conversion: atom

A checked conversion cannot represent the source value in its target type.

out_of_range

atom core/panic/panic.ldn:5 ↑ index
public out_of_range: atom

A checked index or range is outside its valid extent.

One of the compiler-check reasons. Their representation is the ordinary nonzero atom carrier; compare identities, never assumed integer codes.

public overflow: atom

A checked arithmetic operation cannot represent its result.

stack_exhausted

atom core/panic/panic.ldn:14 ↑ index
public stack_exhausted: atom

A stack switch found the stack pointer below its limit, or a stack was too small to prepare (D451).

public unreachable: atom

Execution reached a path declared unreachable.

public require: (holds: bool, caller where: call_site) -> none

Stop the program, as a failed check does, when holds is false: C's assert, with nothing that turns it off. The stop is reported where require was called, the call site the compiler fills where with (D1959); a routine that checks for its own caller takes a caller parameter too and passes it on, panic.require(holds, where: where).

Example program

This complete program is one of the compiler's runtime tests. View source.

The program, 15 lines
compiler/tests/fixtures/runtime/library-shared-panic/main.ldn15 lines
import core/panic

exercise: () -> (ok: bool) =
    ok = false
    kind: panic.panic_kind = panic.bad_conversion
    ok = kind == panic.bad_conversion and kind <> panic.overflow
end exercise

public main: () -> (code: i32) =
    code = 1
    ok := exercise()
    if ok then
        code = 42
    end if
end main