The same program, the same machine, the same bug — a field read through a pointer whose cell was already released — compiled twice:
--optimize release,full_optimizations,checked
Mica runtime failure: reason=use_after_dispose (11)
Mica runtime context: file=Workbench.mica, line=22, column=14
Mica runtime source: seen := q.item;
exit=1
--optimize release,full_optimizations
free(): double free detected in tcache 2
Aborted
exit=134Read the two reports closely, because the difference is sharper than
“one has more detail”. The checked build names the first wrong act — the
read through the released cell — with its reason, file, line, column, and the
offending source line printed back. The build without checked reports the second
symptom: by the time glibc notices anything, the wrong read has long since
happened, and what the allocator reports is a later consequence, somewhere
else, with no line and no name. One is a compiler telling you what you did.
The other is an allocator telling you it noticed something, somewhere, after
the fact.
This page is the reference behind that difference: what each tier proves and guards, and the complete list of runtime traps — every reason the runtime can name, its trigger, and the tier it runs on.
The tiers
The command line defines the spellings; this table defines the semantics. Every tier compiles the same language and refuses the same programs — the compile-time proofs run everywhere. The tiers differ only in what is guarded at run time and what the generated code spends on it.
debug (O0) | debug,checked (O0,checked) | O1, O2 | O1,checked, O2,checked | |
|---|---|---|---|---|
| Compile-time proofs | all | all | all | all |
| Always-on traps (below) | yes | yes | yes | yes |
| Checked-tier traps (below) | no | yes | no | yes |
| Optimization | none | none | standard (O1) or full (O2) | standard or full |
| Debuggability | every variable observable, every line breakable | same | optimized | optimized |
Four rules complete the model:
checkedis a flag, not a profile. It joins whichever profile the set names:O2,checkedis the shipped program with every guard on, anddebug,checked(orO0,checked) the build where every variable is observable at the trap. Alone,--optimize checkedmeansrelease,checked, as every set that names no profile means release. A trap names the same act at the same source position on every tier: the tested program is the shipped program, at the guards’ own price. The guards and the optimizer do not contend — the middle end proves a transformation trap-free before it applies it, and a guard it cannot prove away stays.debugalone carries none of the heap guards: run a program atdebug,checkedbefore you trust it.- A value always belongs to its type. A subrange, an enumeration,
boolandunicodename the values they hold, and no tier lets a variable of one hold any other. A conversion into one, aSucc,Pred,IncorDecpast its edge, a value that enters one from C or out of a bit record, a read of a device register, and a read of memory C may write after a call returned stop the program withordinal_out_of_domainwhere the value would enter or is read, with or withoutchecked. An integer type holds every bit pattern of its width, so its arithmetic and its narrowing casts have an answer to give. checkedalso refuses the answers that change a value. What the checked tier guards, the tiers withoutcheckedlet through with an answer of their own: an add, subtract or multiply that leaves its type wraps, signed or unsigned, ondebugas onO1andO2, a narrowing cast keeps the low bits, a float operation that produces NaN propagates it, and a bit shift past the width moves every bit out. The last column of the checked-tier table below names each answer, and only its heap rows are left undefined. What is never left undefined, on any tier, is the list in the next section.O3is refused by name. The transformations that would distinguish it fromO2— vectorization, loop unrolling — do not exist yet, and an alias that silently meantO2would be a promise the compiler does not keep. You get an explanation, not a build.
Traps on every tier
These guards are part of the language’s semantics, not a diagnostic mode.
They run in debug, checked, O1, and O2 alike, because for each of
them the alternative is not “a faster program” but a program whose behaviour
has no defined reading at all — or one that differs between architectures.
| Reason | Fires when |
|---|---|
division_by_zero | an integer division or modulo meets a zero divisor. Guarded on every tier because the two architectures disagree without it — one faults, one silently yields zero — and Mica’s answer is the same trap on both |
signed_arithmetic_overflow | the one always-on case: MIN / -1, the single signed division that cannot be represented. All other overflow guarding is checked-tier |
nil_dereference | a read or write through a nil pointer. The compiler owns this check rather than borrowing the MMU’s page fault, so the answer is the same on hosted and freestanding targets |
index_out_of_range | a variable index misses its array’s bounds. Constant misses were already compile errors |
ordinal_out_of_domain | a value would enter a subrange, an enumeration, bool or unicode outside its domain: a conversion with as, from an integer or a fraction; Chr of a number that is no Unicode scalar value, the surrogate window among them; Succ, Pred, Inc or Dec past the domain’s edge; a C routine’s answer, or what it wrote through an address the program handed it; an argument handed to an exported routine or a callback from outside the unit, by a C caller or a C library; a read through a pointer to memory the C side owns; a bit record field whose bits spell a value outside its type; a read of a device register declared absolute; a read of a variable whose address the program handed to C, or of memory behind a pointer it handed to C, since C may keep the address and write there after the call returned. The program stops where the value would enter: at the conversion, the step, the call, the parameter or the read. Every road in is checked, so a case without an else whose arms cover the whole domain always runs one of its arms |
tensor_shape_mismatch | two tensor operands whose extents live in their values disagree at an operation — unequal elementwise extents, a matrix pair sharing a total but not a shape, a product’s inner dimensions, a dot’s lengths, or a dynamic value against the static shape it meets. Always on, one compare on the agreeing road: the mismatch that would otherwise read past the shorter backing |
tape_misuse | the order of a tape’s acts is broken while the program runs: a window opened while another is recording, or closed while none is; Backward called inside the window, twice on one window, or on a value the window never recorded; Gradient read before Backward walked the window, or at extents the window never recorded for the value; a window that records more than the tape holds. A line of its own names the act, as in Mica runtime message: Backward was called twice on one tape window |
allocation_failure | the allocator cannot satisfy a request — including a fixed-arena budget exceeded, and a stream’s ring whose computed capacity is below one or larger than memory can hold. An allocation that failed has no value to continue with. A checked build also stops here at the first allocation or release a heapless routine reaches, C code behind its contracts included, and the message names the routine and what it reached |
map_key_missing | a map read names a key the map does not hold |
file_read_past_end | a typed read reaches past the end of its file |
file_write_failed | the operating system refuses a write |
stack_overflow | recursion exhausts the stack. Reported by name, as a trap, instead of the anonymous segmentation fault other toolchains leave you |
explicit_panic | the program says so |
library_body_reentered | a C function that a library’s body calls comes back, on the thread that runs the body, to the body entry of that library or of a library built against it, in a library built with --tasking multicore, where the entry waits for a body another thread runs. The message line names the library. Where the way back passes another thread, the threads wait for each other instead, as the C boundary describes |
callback_foreign_thread | a C function runs a callback the program handed it on a thread other than the one that called the C function, in a build with one carrier (--tasking single); the message line names the callback and the C function. Under a multicore program, a C library runs a callback of a Mica library built for one carrier on a thread that runs no task of the program, such as a thread the C library started for itself; the message line names the callback. A program or library built with --tasking multicore runs each such thread as a task of its own instead |
A value outside its type stops where it would enter, so every tier names the
same first wrong act. This program converts a 7 into Small = 1..3:
program OutOfDomain;
imp
WriteLn : std;
const
Outside = 7;
type
Small = 1..3;
var
raw : int64;
small : Small;
begin
raw := Outside;
small := raw as Small;
case small of
1: WriteLn("one");
2: WriteLn("two");
3: WriteLn("three")
end;
WriteLn("after the case");
end.--optimize debug
Mica runtime failure: reason=ordinal_out_of_domain (20)
Mica runtime context: file=OutOfDomain.mica, line=18, column=18
Mica runtime source: small := raw as Small;
exit=1
--optimize release,full_optimizations
Mica runtime failure: reason=ordinal_out_of_domain (20)
Mica runtime context: file=OutOfDomain.mica, line=18, column=18
Mica runtime source: small := raw as Small;
exit=1The debug build and the optimized one stop at the conversion, as both checked builds do, and none of them runs an arm of the case or the statement after it.
Traps on the checked tier
These guard the acts the tiers without checked let through, debug among
them, with the answer the last column names. The checked build refuses the
answer and names the act.
| Reason | Fires when | Without checked instead |
|---|---|---|
signed_arithmetic_overflow | an add, subtract, or multiply leaves its type’s range | wrap |
unsigned_arithmetic_wrap | an unsigned add, subtract, or multiply leaves its type’s range | wrap |
bit_shift_out_of_range | a shift count reaches the operand’s width | every bit moves out: zero, or minus one when shr shifts a negative signed value |
cast_out_of_range | a narrowing integer cast loses the value, or a float-to-integer cast misses the target’s range | truncate; the float source saturates |
cast_invalid | a float-to-integer cast meets NaN | yield zero (the saturating conversion’s answer) |
floating_arithmetic_invalid | a float operation produces NaN from non-NaN operands | propagate the NaN |
floating_arithmetic_overflow | a float operation produces an infinity from finite operands | propagate the infinity |
use_after_dispose | a read or write through a pointer whose cell was released | undefined |
double_dispose | a cell is released twice | undefined |
foreign_pointer_release | a dispose meets a pointer that Mica’s allocator did not produce, where the compiler cannot tell what the variable holds | undefined: the allocator reads a cell header that is not there |
cyclic_ownership | a cycle of owners reaches the drain | the drain follows the cycle until the stack runs out, and the always-on guard reports stack_overflow |
assertion_failed | an Assert does not hold | asserts are not compiled |
leaked_cells | the run ends with heap cells it never released, a leak no analysis could prove. A leak report before the reason line names each cell by its type and the place of the new that made it (the leak no analysis can prove) | the run ends without a word, and the cells go back to the operating system with the process |
Two footnotes to that table. Undefined in the heap rows means exactly what the opening demonstration shows — not “probably crashes”, but “whatever the allocator’s internal state makes of it, reported late or never”. And the heap rows are the unprovable remainder: most use-after-dispose and double-dispose programs do not compile at all — the checked-tier trap exists for the cases a runtime condition hides from the compile-time proof.
The foreign-pointer row is a heap row of the same kind. Memory a C library
made belongs to that library, and a dispose the compiler can trace to it does
not compile: a variable or a field whose every value is a pointer a C routine
answered, directly or through any number of copies, is refused with error 5678,
which names the C routine and the routine that releases its memory, and a
dispose of the answer itself, as in dispose Malloc(64 as uint64), with error
5191. This program is refused at its last statement:
program ForeignReleaseCopy;
imp
Mmap, PROT_READ, PROT_WRITE, MAP_PRIVATE, MAP_ANONYMOUS : linux;
const
PageBytes = 4096;
NoFile = -1;
NoOffset = 0;
var
page : pointer uint8;
copy : pointer uint8;
begin
page := Mmap(nil, PageBytes, PROT_READ + PROT_WRITE, MAP_PRIVATE + MAP_ANONYMOUS, NoFile, NoOffset) on fail use nil;
copy := page;
dispose copy;
end.with error 5678:
dispose statement cannot release 'copy': every value it holds is a pointer a C routine answered, the first from 'Mmap', and never a cell Mica's allocator gave out — memory a C library made belongs to that library, which releases it with a routine of its own: 'Munmap' for a page 'Mmap' answered, 'Free' for a block 'Malloc' answeredThe trap is the checked build’s answer where the compiler cannot tell what a variable holds: one that may also hold a cell the program made.
The last row leaves out more than a trap: a tier without checked drops the
whole Assert, its condition and every side effect in it, so the invariants
a checked build proves cost the shipped program nothing. That is why an
Assert condition owns nothing. A condition that makes a cell with new,
hands one to an owning pointer parameter, or takes one from a call whose
result owns a cell would do so on the checked tiers alone, so it does not
compile on any tier, and the message names the repair: bind the call’s
answer to a variable first, then assert the variable, as in
taken := Take(cell); followed by Assert(taken);. The hand-over then runs
on every tier, and only the check belongs to the checked build.
What the checked tier costs in memory
A checked run answers a stale pointer, and it pays for the answer in memory. Every cell it releases is kept, poisoned, so that a read through a pointer to it is recognized rather than served; and every block it can serve no further cell from is quarantined for the rest of the run. A checked program’s storage therefore grows with the work it does — by design, and for as long as the run lasts.
That is the price of the guarantee, and the guarantee is not weakened to lower it. What it changes is the budget. A program given the fixed arena its release build runs in will exhaust that arena under a checked build, and the exhaustion reads exactly like a leak — which is the opposite of what it is. So the report says so: the allocation failure closes with a note naming the checks.
Mica runtime failure: reason=allocation_failure (8)
Mica runtime context: file=table.mica, line=18, column=14
Mica runtime source: a := new Ints reserve 200;
Mica runtime note: this program is built with memory checks, so every cell it releases is kept poisoned and every block
it can serve no further cell from is quarantined for the rest of the run. Its storage grows with the
rounds by design, and a budget sized for the release build is too small for a checked one.Give a checked run room: the hosted heap, or a fixed arena sized for the whole run rather than for the live set. The release build is the one the budget is measured with.
What a trap is — and what it is not
Mica separates two kinds of going wrong, and the boundary runs straight through this page.
A failure is a value: a file that is not there, a key that might be
missing, input that does not parse. Failures travel the fails channel, are
declared in a function’s signature, and handling them is the program’s
ordinary business — the failure channel article
covers the whole discipline. A map read whose absence you expect belongs
there, and the standard library gives you the failing spelling.
A trap is the runtime’s verdict that the program itself is broken: the trap list above is not a list of conditions to handle but a list of bugs with names. There is no catching a trap, no resuming past one, and no trap handler — the program stops, names the reason, the file, the line, and the column, and exits nonzero. If a trap fires, the fix belongs in the source, not around it.
That is also why the trap report is deliberately spare: reason, place, offending line. Everything a bug report needs, nothing a control flow could consume.
Reading a trap report
Mica runtime failure: reason=index_out_of_range (12)
Mica runtime context: file=/work/Sieve.mica, line=31, column=18
Mica runtime source: flags[candidate] := false;
exit=1Line one names the reason — the spelling in the tables above — and its
number. Line two places it: file, line, column. Line three quotes the
offending source line. The process exits 1 for every trap — including a
stack overflow, which arrives through the signal road but reports and exits
like every other reason. A genuine segmentation fault, by contrast, is
deliberately not dressed up as a trap: it kills the process with the real
signal at the real address, so nothing about a crash is disguised.
A second release met inside a container’s release walk carries one more line. Releasing a container releases every cell its owning slots hold, so lines two and three name the release that started the walk, and the walk line names where the walk met the cell again: the release of the cell’s type, at that type’s declaration.
Mica runtime failure: reason=double_dispose (10)
Mica runtime context: file=Boxes.mica, line=31, column=5
Mica runtime source: dispose box;
Mica runtime walk: this release's walk met it in the drain at file=Boxes.mica, line=7, column=5
exit=1Every reason in the tables above is produced, on both architectures, by the compiler’s own test suite — the tables are generated evidence, not aspiration. No trap is listed that has not been fired.