std.assert
Assertion module. The most commonly used tool in tests and examples.
use std.assertFunction Overview
| Function | Signature |
|---|---|
assert | (cond: Bool, ?msg: String) -> Never |
Functions
assert
assert: (cond: Bool, ?msg: String) -> NeverAssert that cond is true.
cond—— the boolean expression to be evaluatedmsg—— optional message,?indicates it can be omitted; printed with the diagnostic when the condition is not met
Return: returns Void when the condition holds, without interrupting execution (the implementation at src/std/assert.rs:82-83 returns RuntimeValue::Void in the passing branch). The declared return type, however, is Never — the signature (cond: Bool, ?msg: String) -> Never comes from src/std/assert.rs:24-29; the two do not contradict: use sites of Void (such as x = assert(true)) already accept Never <: T (principle of explosion, see Type System §2.2).
Error: throws E6005 (assertion failed) when the condition is false; the program exits with a non-zero code.
use std.assert
main: () -> Void = {
assert(1 > 0)
assert(1 > 0, "这个字面断言必然成立")
}Assertions are the primary means of evaluation for test corpora — src/std/tests/*.yx and tests/yaoxiang/** both work by having assert failures cause the process to error out:
use std.assert
use std.list
main: () -> Void = {
list.len([1, 2, 3]) == 3
assert(list.len([1, 2, 3]) == 3, "len == 3")
}Type Families (Undocumented)
In addition to one native export, std.assert also registers two type families (TypeFamilyExport), which belong to the type universe rather than the value universe, and are therefore not counted in the "Function Overview" table above:
| Name | Definition | Source |
|---|---|---|
IsTrue | (b: Bool) -> Type, true => Void / false => Never | src/std/assert.rs:34-44 |
Assert | (cond: Bool) -> Type, i.e. IsTrue(cond) | src/std/assert.rs:45-49 |
There is also EffectSpec::new("assert", [GammaAssume { predicate_arg: 0 }], true) (src/std/assert.rs:53-59), used for injecting into the flow-sensitive assumption set Γ.
Currently you cannot write these two names in type position:
Assert(true)reportsE0010 Expected a type, found BoolLiteral(true), andassert.Assert(true)reportsE0012 Invalid syntax: declaration requires an initializer. In other words, only the runtimeassertis available; compile-time refinement primitives are not yet wired up. For the spec-level notation, see Standard Library Specification §1.5.
Related
- Test Specification — corpus organization and evaluation conventions
- Error Code Reference —
E6005assertion failed
