Wado

Research: Assertions and Contracts in Other Languages

Wado checks the contracts of its _unchecked functions at run time, in a build meant to find bugs. Each contract is written twice: as prose in the function's doc comment, and as a check in its body. The open question is whether a contract gets syntax on the function's signature. There it would be an executable comment: documentation that a reader, a tool and a checking build all read from one place.

This survey records where Wado stood on 2026-10-08 (037dba480), how other languages write contracts, and what their users and researchers say about the choices. It feeds no WEP yet.

Wado on 2026-10-08

assert

Assertions holds the rule. No build removes an assertion: at every optimization level the condition is evaluated, and a false one traps. A program can therefore rely on assert to protect its state.

The only part a build may drop is the power-assert message. -f bare-asserts (on at -Os) makes a failed assertion trap silently.

panic(message) and unreachable() trap on purpose. Nothing recovers from a trap.

Behavior classes and contract violations

Behavior Classes holds the rule, and WEP: Behavior Classes says why. Where the specification leaves behavior open, it names one of three classes:

A contract violation is a program reaching an operation outside the contract that operation states. It is always a bug, and each operation says which class its violation has. Unconstrained behavior arises from a contract violation and from nothing else.

Contract checks

Contract Checks holds the rule, and WEP: Contract Checks says why.

A checked function looks like this today. The contract appears once as prose and once as code:

/// # Contract
/// - `0 <= index < self.len()`.
///
/// Any other `index` is a contract violation, and the result is
/// unspecified: a byte of the backing array past `len()`, or a trap.
pub fn get_byte_unchecked(&self, index: i32) -> u8 {
    if builtin::contract_checks() {
        assert 0 <= index < self.used, "index out of bounds";
    }
    return self.repr[index];
}

The WEP lists three known gaps that bear on syntax:

_unchecked functions

WEP: String API — checked / unchecked / internal Discipline sets the pattern. A checked method is assert on its inputs followed by a call to its _unchecked twin.

Function Contract Class of a violation
String::get_byte_unchecked 0 <= index < len() unspecified
Slice::get_unchecked 0 <= index < len() unspecified
String::set_byte_unchecked 0 <= index < len(), and the bytes stay UTF-8 unconstrained
char::from_u32_unchecked the value is a Unicode scalar value unconstrained
StrSlice::slice_unchecked 0 <= start <= end <= len(), both on character boundaries unconstrained

Wado has no unsafe block, and the behavior-classes WEP plans none.

Arithmetic

Overflow and Division by Zero holds the rule. Integer +, - and * wrap in two's complement at every width, so overflow is not a contract violation. Division by zero and a signed MIN / -1 trap, as the Wasm instructions do.

Effects

WEP: A Function Without with Performs No Effects makes a function without a with clause effect-free. The exception is an #[ambient] function such as log_stderr. A predicate written in Wado can therefore be checked for effects, though it can still trap: an index out of range or a failed assert inside a called function does.

with already follows a function's return type, so any clause added to the signature has to sit beside it.

Assertions elsewhere

An assertion is a statement in the body, unlike the contracts in the next section.

Language Check that always runs Check a build removes What a removed check means
Rust assert! debug_assert!, by debug_assertions nothing
Swift precondition, kept at -O assert at -O; both at -Ounchecked nothing at -O; assumed true at -Ounchecked
Kotlin require, check assert, by the JVM's -ea nothing
Java none assert, off unless -ea nothing
Python none assert, removed by -O nothing
C++26 hardened standard library pre, post, contract_assert under ignore nothing
D none assert, in, out, removed by -release a violation is undefined behavior
Zig none std.debug.assert unreachable, so undefined behavior in ReleaseFast
Go none: the language has no assert none none

One spelling with two meanings gets misused

Where assert disappears in a release build, people still write it for checks that must hold.

A removed check that becomes an assumption does more damage

Zig implements std.debug.assert as if (!ok) unreachable. In ReleaseFast, reaching unreachable is undefined behavior, and the optimizer uses it. Zig's users point out that this does more harm, and is harder to debug, than removing the check would.

Swift does the same at -Ounchecked. On swift-evolution, Joseph Lord and Dave Abrahams argued that an assertion should never create undefined behavior. Chris Lattner answered that -Ounchecked deliberately removes the guard rails, and that nobody has to use it.

Rust keeps the assumption separate and explicit. core::hint::assert_unchecked is an unsafe fn, and calling it with a false condition is immediate undefined behavior.

Keeping checks on costs little where it was measured

Google applied libc++'s hardening mode to hundreds of millions of lines of C++. It found over 1,000 bugs, and the average slowdown was 0.30%. That experience is the case for C++26's standard library hardening (P3471), whose checks terminate the program in a hardened implementation.

Removal done well gives the two kinds different names

SQLite's assert(X) states that the developers have a proof that X holds, and it is a no-op in a release build: enabled, the asserts make SQLite about three times slower. A check the developers believe but have not proven is written ALWAYS(X) or NEVER(X) instead. Those stay in the release build, and code after them handles the case where the belief was wrong.

Some refuse assertions altogether

Go has no assertions. Its FAQ says programmers "use them as a crutch to avoid thinking about proper error handling and reporting". SQLite's assert page argues against that view.

Contract syntax elsewhere

A contract can live in one of five places: on the signature, in an attribute, in the body as a call, in a comment or a separate block, or at a module boundary.

Language Where Precondition Postcondition Result and old value Checking
Eiffel signature require x > 0 ensure Result > x Result, old a monitoring level per class; off in release
Ada 2012 signature with Pre => X > 0 Post => F'Result > X'Old 'Result, 'Old Assertion_Policy per kind; SPARK proves
D signature in (x > 0) out (r; r > 0) named in out -release removes; checked in the callee
C++26 signature pre (x > 0) post (r: r > 0) named in post one of four semantics, per translation unit
Vala signature requires (x > 0) ensures (result > 0) result at run time
Spec#, Midori signature requires x > 0 ensures return > x return, old(..) Midori: always
Dafny, Verus signature requires x > 0 ensures r > x the result is named proved statically
Rust nightly attribute #[requires(x > 0)] #[ensures(\|r\| *r > x)] a closure parameter only under -Zcontract-checks
.NET Code Contracts body, as a call Contract.Requires(x > 0) Contract.Ensures(...) Contract.Result<T>() a binary rewriter; discontinued
Scala body, as a call require(x > 0) ensuring(r => r > x) a lambda parameter always; Stainless verifies
Clojure signature {:pre [(pos? x)]} {:post [(pos? %)]} % the *assert* flag
JML comment //@ requires x > 0; //@ ensures \result > x; \result, \old a separate tool (OpenJML)
ACSL (Frama-C) comment /*@ requires x > 0; */ /*@ ensures \result > x; */ \result, \old a separate tool (E-ACSL checks, WP proves)
Python PEP 316 docstring pre: x > 0 post: __return__ > x __return__, __old__ deferred, never adopted
Move separate block spec f { requires x > 0; } ensures result > x; result, old the Move Prover only; never at run time
Racket module boundary (contract-out [f (-> positive? …)]) in the same -> or ->i form named in ->i always, with blame

Swift, Go and Zig have no contract syntax. Kotlin's contract { … } shares the name only: a function tells the compiler what its result implies, for smart casts, and nothing is checked at run time.

On the signature

Eiffel introduced Design by Contract. A failed precondition is the caller's fault, and a failed postcondition is the function's own. The split is why a precondition sits on the signature, where the caller reads it. Eiffel's interface view of a class shows the signatures with their contracts and hides the bodies, so the contract is the documentation.

Ada 2012 added the Pre and Post aspects. pragma Assertion_Policy decides per kind of assertion whether a run checks it (Check) or skips it (Ignore). GNATprove, SPARK's prover, uses every contract whatever the policy says, so one contract serves both testing and proof. Tucker Taft told Rust's language team that contracts were the one Ada 2012 feature that got people to move off older versions.

D's in, out and invariant are removed by -release, as assert is. DIP 1009 (2018) gave them the expression form in the table.

Vala places requires and ensures between the parameter list and the body, after any throws clause. A method may repeat either clause.

Spec# put requires and ensures on the signature of C#. Midori, an operating system written in a Spec#-derived C#, kept that placement after trying the alternatives (see A contract API in the body failed).

Dafny and Verus write the same clauses, and prove them instead of running them. Verus names the result in the return type: fn f(x: u32) -> (r: u32).

As attributes

Rust's nightly contracts feature (rust-lang/rust#128044) writes #[core::contracts::requires(...)] and #[core::contracts::ensures(...)]. A requires may hold a sequence of statements ending in a bool (rust-lang/rust#144444). A run checks contracts only under -Zcontract-checks, which is off by default. The project goal behind it is to specify the safety conditions of every unsafe function in the standard library and verify them with Kani, without changing the library's behavior or speed for anyone who has not opted in.

The tracking issue lists questions it has not settled:

In the body, as calls

.NET Code Contracts wrote contracts as calls to a library: Contract.Requires and Contract.Ensures at the top of the body. A separate tool rewrote the compiled assembly, moving each postcondition to every exit. Microsoft discontinued it and archived the repository.

Scala's require is an ordinary function, and ensuring is a method on the result expression. Stainless reads both as a specification to verify.

In comments and separate blocks

JML and ACSL are contracts written in comments, so the host language's compiler never reads them. A separate tool checks them at run time (OpenJML, E-ACSL) or proves them (Frama-C's WP). They are literally executable comments, and run only when that tool runs.

PEP 316 (2003) proposed inv:, pre: and post: lines in docstrings. Its stated reasons were better documentation and easier testing. It was deferred after python-dev objected that docstrings would grow even longer. Its style lives on in CrossHair, which reads contracts from docstrings, and in decorator libraries such as icontract.

Move puts a contract in a spec block beside the function. The Move Prover reads it, and no build runs it.

At module boundaries

Racket attaches a contract where a module exports a function, so the check runs when a value crosses the boundary, not on every internal call. Every check records which party broke it: the caller for an argument, the module for a result. This is what "blame" means in the research below.

What the record shows

Most contracts are null and range checks

Study Corpus Finding
Chalin (2006) 85 Eiffel projects the earlier study of contract use that Estler et al. extend
Estler, Furia, Nordio, Piccioni, Meyer 21 projects in Eiffel, C# and Java; 260 million lines over 33% of program elements carry contracts in most projects; contracts change less often than the code; inheritance changes little
Schiller, Donohue, Coward, Ernst 90 C# projects using Code Contracts 68% preconditions, 26% postconditions; 75% check that a value is present, mostly non-null; suggested contracts did not broaden what people wrote
Dietrich, Pearce, Jezek, Brada the 200 most popular Maven Central projects many mechanisms mixed in one program; fewer contracts than expected; projects that adopt contracts add more; some contracts break substitutability

Midori reached the same figure from the inside. Joe Duffy reports that about 90% of its contracts were nullability and numeric ranges, and that about 90% of the argument-validating exceptions in typical .NET code became preconditions. He sums it up as "contracts begin where the type system leaves off". Midori explored non-null and range types to absorb those checks, and Duffy calls the failure to deploy non-null types system-wide one of his biggest regrets.

A contract API in the body failed

In "Embedded Contract Languages" (SAC 2010), Fähndrich, Barnett and Logozzo argued for writing contracts as ordinary code rather than in a new syntax. They observed that the number of specification languages roughly equals the number of tools that consume them. Code Contracts was that design.

Two reasons are given for its discontinuation. Contracts took effect only after the separate rewriter ran, so a build pipeline that skipped it lost them silently. And users found some constraints they wanted could not be written, while others they wrote turned out too strict and had to be removed.

Duffy's account of Midori, written by a team that had lived through Code Contracts, gives the design reasons:

One kind of contract, always checked

Midori first offered weak and strong contracts, and contracts compiled only in debug builds. Developers misused the variants and could not tell when a contract would be checked. Midori dropped them all and kept a single kind that is always checked. A violation ends the process ("abandonment"). Duffy reports that forcing the one choice produced healthier code.

Midori's assertions went the other way. They stayed library calls (Debug.Assert, Release.Assert), not part of any signature, and the team wrote more release assertions than debug ones.

C++26 adopted contracts over sustained objection

P2900 adds pre, post and contract_assert. The committee added it to the working draft in Hagenberg in February 2025 (100 for, 14 against, 12 abstaining). In March 2026 the committee finished C++26 and sent it to ISO's international approval ballot (114 for, 12 against, 3 abstaining). The objections did not go away. They come from compiler, library and language designers: P3173 and P3506 (Gabriel Dos Reis, Microsoft), P3573 "Contract concerns" (nine authors), and P4334 (Stroustrup, Garcia, Falco, Spicer, Voutilainen; 2026-08-09).

The debate is about what a build may do with a contract, not where it is written:

The alternative those papers name is the hardened standard library: checks that stay on in production, measured at about 0.3% overhead across Google's servers. The objections to the predicate and to inheritance follow below.

A disabled contract drifts from the code

A contract that no build reads stops being checked against the code it describes, and can name a parameter that no longer exists with nothing to report it. JML, ACSL and Code Contracts fail this way whenever their separate tool does not run. Rust's nightly contracts avoid it: a disabled contract stays in the function body under a condition that is false, so it is still type-checked (rust-lang/rust#144438). A proposal that skipped disabled contracts altogether was set aside, its author warning that such contracts "are susceptible to becoming out of sync with the rest of the codebase" (rust-lang/rust#145229).

Checking in the callee loses the caller's build

D runs an in contract inside the called function. A library vendor who ships a -release build therefore ships it with every precondition removed, and the library's users cannot turn them back on. The DIP 1009 discussion asked for the check to move to the call site, which would make the contract part of the signature. No DIP followed. P3267, on C++ implementation strategies, weighs the same choice between checking at the call and checking in the callee.

Rust's standard library takes the caller's side for its unsafe preconditions. assert_unsafe_precondition! is decided by the caller's debug_assertions when the caller is monomorphized, so a release-built standard library still checks when it is called from a debug build. Each check says what it guards: check_language_ub for undefined behavior of the language itself, and check_library_ub for a documented library precondition whose violation is not immediately undefined behavior. The two correspond roughly to Wado's unconstrained and unspecified violations.

The predicate has its own failures

A contract predicate is code, and every language has to say what happens when that code misbehaves.

Inheritance needs a rule

A method that overrides another may only weaken its precondition and only strengthen its postcondition, or callers written against the parent break. Eiffel writes this as require else and ensure then, and Ada as Pre'Class and Post'Class. C++26 adopted an inheritance model into P2900R13 and removed it before R14 for lack of consensus, so virtual functions cannot carry contracts yet. Dietrich et al. found Java contracts that break the rule in practice.

The cost of a check can exceed the function

Some preconditions cost far more than the function they guard. In a July 2025 thread on Rust's internals forum, checking that bytes are UTF-8, an O(n) scan, was called too slow even for a debug build, since from_utf8_unchecked is O(1). Others answered that a checked twin, String::from_utf8, already exists for whoever wants the check.

D's forum returns to the same dilemma from the other side: keep expensive contracts and miss the performance requirement, or remove them and miss the safety requirement.

Rust's RFC 560 makes integer overflow a program error that is not undefined behavior. A build with debug_assertions must detect it and panic. A release build may skip the check, and then the result is defined to wrap. In Wado's terms, that is a contract violation whose class is unspecified, with exactly one listed outcome. A 2021 internals thread proposed checking overflow in every build. It stalled on cost: one cited study measured slowdowns from 0.4% to 95%, with a mean of 30%. Wado took neither side: its arithmetic wraps by definition.

Higher-order contracts cost more again. A contract on a function argument has to wrap the function and check each later call. Takikawa et al. ("Is Sound Gradual Typing Dead?", POPL 2016) measured slowdowns above 100x in Typed Racket, where such contracts guard every boundary between typed and untyped code.

One contract for checking and proof

Kani, Prusti and Creusot each define their own contract language for Rust. A 2022 design meeting of Rust's language team discussed a contracts RFC draft whose motivation was a shared one. The draft proposed contract and debug_contract, mirroring assert! and debug_assert!, so that expensive checks could be turned off. The team agreed to start with contracts checked at run time before any static verification. The standard library verification effort now reports 989 contract-verified proofs.

Ada's Pre and Post already serve both, as described above.

The theory: blame, and contracts as types

Findler and Felleisen ("Contracts for Higher-Order Functions", ICFP 2002) extended contracts to functions passed as values, and introduced blame: each failed check names the party that broke the agreement. Dimoulas, Findler, Flanagan and Felleisen ("Correct Blame for Contracts", POPL 2011) showed that the obvious semantics sometimes blame the wrong module, and gave one that does not.

Greenberg, Pierce and Weirich ("Contracts Made Manifest", POPL 2010) compared latent contracts, which are checks the type system does not see, with manifest contracts, which are refinement types recording the checks a value has passed. The two had been assumed interchangeable. They are not quite: translating one into the other can make a program blame more.

Contracts as documentation and as test oracles

Sean Parent and Dave Abrahams ("Better Code: Contracts", CppCon 2023) teach contracts as a design method, and argue between them whether the code or the documentation matters more.

Khlebnikov and Lakos (P1743, "Contracts, Undefined Behavior, and Defensive Programming") distinguish a wide contract, which has no precondition, from a narrow one. They argue that giving every input a defined behavior, as a null check in strlen would, hurts performance and correctness alike.

Andrzej Krzemieński's 2013 "Preconditions" series treats a precondition as an idea rather than a language feature, and concludes that assertions and comments were the best C++ then offered.

John Regehr ("Use of Assertions") counts executable documentation of pre- and postconditions among the benefits of assertions, and calls them a gateway to formal methods.

Hillel Wayne ("Property Tests + Contracts = Integration Tests") uses contracts as the oracle of a property test. The test asserts nothing itself, and fails when any contract does.

Questions this leaves for Wado

The survey bears on these questions. It answers none of them.

Sources

Wado

Languages

Rust

C++

Papers and essays