Skip to Content
Language in depth

Checked operations and build profiles

What the compiler proves, what cpl run traps on, and what the release build on the judge does instead.

Overflow, a value outside its declared range, an index past the end, / 0: in C++ each is a wrong answer or undefined behaviour, and you find out from the verdict. CP Lang answers three questions for every such operation: what it means, what the compiler can prove, and what each build does. The normative list is §9.3 of contexts and checks, with the overflow cases in numbers and layout §4.

Two builds

BuildUsed byAn unproven check
checkedcpl run, cpl stress, cpl test, the playground, cpl build --checked, C++ with -DLOCALtraps, naming the values
releasecpl build and the file you submitis not checked

A valid program, one that never traps, behaves the same in both builds and in the interpreter. A program that would trap is a bug, and the checked build is where you find it: every test, stress run and playground run uses it. (core/09-contexts-checks §9.2)

Proved, so never checked

Ranges in the declarations you write anyway are the proofs. Here both factors are at most 1e9, so the product fits in 64 bits, and the compiler emits a plain multiplication with no check in either build:

Output
Press Run (Ctrl+Enter).

Indexing a[i] with i: D on a [D]T is the same: the type is the proof, so it is never checked. Proofs are interval arithmetic within one function, so compile time stays linear.

Unproven, so checked

With Int inputs nothing bounds the product. cpl run traps with the operands instead of printing a wrapped number:

error on purposeOpen in playground
Output
Press Run (Ctrl+Enter).

A store into a range type is checked the same way. k is declared 0..=10, so the fourth += 3 traps with the value it tried to store:

error on purposeOpen in playground
Output
Press Run (Ctrl+Enter).

Reads are checked too: let n: 1..=5 = read() on input 9 stops at the read, naming the range, so a misread format shows up on the first sample.

Release on the judge

The release build optimizes for valid programs: it runs no checks unless you opt in. Unproven Int arithmetic wraps, a range store truncates to its width, an assert is skipped. Where avoiding C++ undefined behaviour is free, the lowering still does: arithmetic wraps through unsigned casts, which compile to the same instructions, so -O2 can't miscompile the valid code around a bug. To keep a check on the judge, opt in for a block or file, or for a whole submission in the judge profile:

with checks(ranges: on, overflow: on, bounds: on):   // kept in release too
    ...

The opposite also exists. with checks(overflow: off) removes a check in the checked build too, for a hot loop whose stress runs are too slow, and unchecked: turns every check off in every build. Both are greppable. (core/09-contexts-checks §9.4)

Output
Press Run (Ctrl+Enter).

Why it's designed this way

Release runs no checks. decision 148: on the judge only valid programs matter, and a wrong program fails whether it gives a wrong answer or a crash. So release pays nothing for checks, and the checked build, which every local run uses, catches the bug before submission. The rejected alternative, keeping every check in release, costs hot-loop time on every accepted solution to turn one kind of failing verdict into another. The reversal is one line: default release_checks to the checked set.

Proofs, not annotations. The alternatives were C++'s, where you pick int or long long by hand and an overflow is silent, and Rust's, where every operation is checked in debug builds and the width is still yours to choose. Both make you restate a fact the statement already gave. Here the ranges that drive the proofs are the statement's constraints, written once as types; they also pick storage widths and drive input validation. A proven operation costs nothing in either build, which is why the zero-cost forms exist for every check.

Checks never call user code. decision 180: a checked build makes exactly the calls to user functions, closures and input that release makes, in the same order. A check only inspects values the program already computed, so an interactive query, a counter in a closure or a cached function sees the same history in both builds, and "passes locally" means the same program.

Still open. Whether a bad index, / 0 and a failed assert should trap on the judge too, rather than run unchecked, is one of the calls with Ryan. This chapter describes the current rule.

On this page