Compile-time code
Design spec. Status: steps 0–12 done, step 13's @for around items, and step 14's
@for (i, v) in Kind among match arms and @for f in Point among statements, in crates/compiler/8-comptime: @const with
calls, loops, recursion, heap memory and text while compiling; structs, enums, arrays,
optionals and text as values; constants in impl blocks and function bodies; @const as an
expression; @assert and @fail; @const once per generic instance; and @for and @if,
including over a generic function's or impl block's N. The rest is proposed.
Som code can already be worked out while compiling, but only a little of it: a const can be
literals, operators, casts and other constants, and nothing else. This spec lets any som
code run while compiling, written in the same som as the rest of the program. The same
fn works in both places: called at run time it runs then, called from an @const it runs
while compiling, and only its result ends up in the program.
The idea comes from Zig (comptime, inline for) and Jai (#run, #insert). Som's
version is built on what it already has: const, value generics like <N: usize>, and @
for things the compiler does (@require, @unreachable).
1. The syntax
There are four things, and each has a single meaning:
| Written | Means | Runs |
|---|---|---|
@const X = expr; |
A named constant. expr can now call functions |
While compiling |
@const expr / @const { … } |
This value, worked out where it's written | While compiling |
@f(args) / @a::f(args) |
A call of an ordinary fn, the same as @const f(args). On its own at the top level, a check: @assert(…);. A built-in's name (@fail, @embed, …) always means the built-in |
While compiling |
@for x in xs { … } |
One copy of the body for each x |
The loop while compiling, the copies at run time |
@if cond { … } else { … } |
Only the branch cond picks exists |
The choice while compiling, the branch at run time |
The rule: @ marks the parts of the program the compiler settles statically. That
includes named constants, so the plain const keyword goes away: every constant is
@const X = …, from @const CELLS = COLS * ROWS; to @const SHAPES = all_rotations();.
There's one spelling, and reading any line tells you what's settled while compiling.
@const evaluates. @for and @if don't run their bodies while compiling: they decide which
code exists. In Zig these are comptime { } and inline for.
Grammar
item := …
| ("pub" | "mod")? "@const" name (":" type)? "=" expr ";"
| "@" path "(" args ")" ";" // a check run while compiling: `@assert(…);`
expr := …
| "@const" unary_expr // binds like `-` and `!`
| "@const" block
| "@" path "(" args ")" // a call while compiling; `path` isn't a built-in
stmt := …
| "@for" pattern "in" expr block
| "@if" expr block ("else" (block | stmt_if_static))?A declaration and an expression both start with @const. In a function body the parser
tells them apart by what follows: a name and then : or = is a declaration
(@const LIMIT = 10;), and anything else is an expression (@const f(x)).
@const binds like a prefix operator, so @const f(x) + 1 is (@const f(x)) + 1. To cover
more, use a block or parentheses. This keeps it from silently swallowing the rest of a line.
What counts as statically known
A static expression is one the compiler can work out without running the program:
- literals, and arrays and structs of statically known values
- constants, and
usize::MAXand the other limits - a generic value parameter (
Ninimpl<N: usize, T>), in monomorphized code - calls whose arguments are all statically known
@constanything- the built-ins in section 5
@for's iterable, @if's condition, and everything an @const uses must be
static. Naming a local or a parameter there is a ConstNotKnown error that points at the
name: "n is only known when the program runs".
Replacing const
Every const in the std library, the examples and the corpus becomes @const, including
associated constants: pub const PI = … becomes pub @const PI = …. There's no transition:
const is no longer read as a constant at all, and is an ordinary name.
Constants without a type: numbers
A whole number written out is whatever number type it's used as, a float too where that float
holds it exactly (let x: f32 = 640;; 16777217 as an f32 is LiteralNotExact), and an
i32 where nothing decides. In a bit operation (~, &, |, ^, <<, >>) it's an
integer. A float written out is any float type, an f64 where nothing decides.
A constant without a written type whose value is a number written out, or arithmetic of
those and of other such constants, is the same at each use: @const W = 640; is a usize
counting, a u32 hashing and an f32 in float maths, as 640 written there would be. Its
value is worked out once, by its initializer, as one that runs code is (4.3), and made the
type of each use there (ConstKind::as_number_of); each use is checked to fit, as a literal
is. W / H of two used as f32s is float division, as 640.0 / 360.0 is. A constant with a
written type, @const W: usize = 640;, or one whose value runs code, is that one type.
2. Examples
Tables computed while compiling
example/tetris/main.som writes out 28 rotation masks by hand. Instead, it writes the 7
pieces and lets the compiler rotate them:
/// Turns a 4x4 mask a quarter clockwise, inside its top-left `size`×`size` box.
fn rotate(mask: i32, size: i32) -> i32 {
let mut turned = 0;
for y in 0..size {
for x in 0..size {
if filled(mask, x, y) {
// (x, y) moves to (size - 1 - y, x)
turned = turned | (0x8000 >> (x * 4 + (size - 1 - y)));
}
}
}
turned
}
fn all_rotations() -> [i32; 28] {
let bases = [0x0F00, 0x44C0, 0x4460, 0xCC00, 0x06C0, 0x0E40, 0x0C60]; // I J L O S T Z
let boxes = [4, 3, 3, 2, 3, 3, 3];
let mut masks = [0; 28];
for k in 0..7 {
let mut mask = bases[k];
for r in 0..4 {
masks[k * 4 + r] = mask;
mask = rotate(mask, boxes[k]);
}
}
masks
}
/// Worked out while compiling: the program only has the 28 numbers.
@const SHAPES: [i32; 28] = all_rotations();This gives exactly the hand-written table (checked). rotate is an ordinary function, and
the game could call it at run time as well.
A value worked out in place
fn crc32(bytes: &[u8]) -> u32 {
let table = @const crc_table(); // [u32; 256], baked into the program
…
}Unrolled loops
impl<N: usize> Vector<N, f32> {
pub prop magnitude {
let mut sum = 0.0;
@for i in 0..N { sum = sum + .components[i] * .components[i]; }
f32::sqrt(sum)
}
}Each copy has i as a known number, so .components[i] needs nothing from the prover.
Branches only some types have
impl<N: usize, T: Copy> Vector<N, T> {
@if N >= 3 {
pub prop z = .components[2];
}
}For Vector<2, _> there is no z at all. This replaces the separate impl Vector<2, T>,
impl Vector<3, T> and impl Vector<4, T> blocks. Item-level @if like this comes in phase
3, see the plan.
Generating declarations
// std/hash.som: ten impls become one loop.
@for T in [u8, u16, u32, usize, i8, i16, i32, i64, isize] {
impl Hash for T {
fn hash(&self, hasher: &mut Hasher) { hasher.write(*self as u64); }
}
}
// An enum's index and name, with nothing to keep up to date by hand.
impl Kind {
pub fn index(self) -> usize = match self {
@for (i, v) in @variants(Kind) { v => i, }
};
}
// Field-by-field equality for any struct.
pub fn fields_eq<T>(a: &T, b: &T) -> bool {
@for f in @fields(T) {
if a.@field(f) != b.@field(f) { return false; }
}
true
}3. How the compiler does it today
The relevant parts as they are now:
| Pass | What it does with constants |
|---|---|
1-lexer |
@name lexes as TokenKind::Builtin. BUILTINS lists them, for the parser and the editor |
2-parser |
expr.rs turns the Builtin token into @unreachable. item.rs reads @require |
3-resolve |
check_const_expr allows only literals, operators, casts, constants, arrays and structs; anything else is ConstNotKnown. check_consts finds ConstCycle |
4-typeck |
const_number works a constant out from its AST, for array lengths |
5-prove |
const_int does the same again, for facts |
6-lower |
A constant has no value of its own: each use lowers its AST expression again, in place |
9-optimize |
fold.rs folds MIR constants, wrapping like the C backend (-fwrapv). shake.rs drops what nothing uses |
10-codegen |
MIR to C. The C compiler lays out structs |
Two things follow from this:
- A constant's value is never stored anywhere. It's an AST expression copied to each use.
That's fine for
W * Hbut wrong for a call: it would run once per use, at run time. - Types need numbers before MIR exists.
[i32; COLS * ROWS]is worked out in typeck, long before there's any MIR to run. A constant computed by a function can't be known there without running code in the middle of type checking.
4. Design
4.1 Initializers: run code on the MIR that's already there
Each @const that isn't plain arithmetic, and each @const expression, is lowered into an
initializer: a MIR function with no parameters whose body computes the value. Wherever the
value is used, lowering writes a placeholder:
ConstKind::Pending(InitId)From there:
7-borrowcheckchecks initializers like any other function, so code run while compiling follows the same rules as code run later.- A new pass,
som_comptime, runs each initializer once in a MIR interpreter, then replaces everyPending(id)with the value it got. shake.rsdrops the initializers, along with any function only they called.all_rotationsandrotatenever make it into the C.
The pipeline becomes:
lex → parse → resolve → typeck → prove → lower → borrowcheck → comptime → optimize → codegenRunning after the borrow checker means the interpreter only ever sees code that has passed every check. It never has to deal with a broken program.
Lowering is monomorphized, so an @const inside a generic function is run once for each
instance, and @const f(N) uses that instance's N.
Plain constants (@const COLS = 10;) keep working as they do now. const_number and
const_int still handle them, and lowering still inlines them. Only constants that need code
run become initializers.
4.2 The interpreter
som_comptime gets a small interpreter over MIR:
enum Value {
Int(i128, IntTy), // wraps to the type's width, like -fwrapv
Float(f64, FloatTy),
Bool(bool),
Unit,
Str(Rc<str>),
Func(FuncId),
Aggregate { variant: Option<usize>, fields: Vec<Value> }, // struct, enum, array, optional
Pointer { alloc: AllocId, offset: usize }, // into its own heap
Null,
}- Places (
Projections) are paths into a frame's locals or into an allocation. References are places. - Intrinsics.
Resize,Free,Read,Write,Offset,ViewandIsNullwork on an interpreter-owned heap:Vec<Option<Vec<Value>>>, one entry per allocation. This meansList,Mapandstringwork while compiling as long as they don't end up in the result (see 4.3). - Arithmetic matches what the program would do at run time: integers wrap to their width,
floats are IEEE
f64/f32.usizeandisizetake the target's width, which for now is the host's, the same as the C backend.
Things that stop evaluation, each with its own error:
| Error | When |
|---|---|
ConstCallsExtern |
It calls an extern "c" function. Later, an allowlist of pure ones (sqrt, sin, …) done in Rust |
ConstReachedUnreachable |
It reaches @unreachable |
ConstDividedByZero |
It divides by zero, or computes MIN / -1 |
ConstOutOfBounds |
It reads past an allocation, which only unchecked pointer code can do |
ConstTooLong |
It runs past a step limit (default 10 million MIR statements, --const-steps to change) |
Each error shows the call stack while compiling, using the spans MIR statements already carry:
error: this constant reached `@unreachable`
--> tetris/main.som:52:9
|
52 | @unreachable
| ^^^^^^^^^^^^ reached here
= while working out `rotate(...)`, called at tetris/main.som:61:20
= while working out `SHAPES`, at tetris/main.som:67:14.3 Baking the result into the program
What an initializer returns has to make sense in the compiled program, so it has to be free of memory:
- ints, floats, bools,
() strings with the literal count (usize::MAX), already part of the program and never freed- arrays, structs, enums and optionals of the above
A result holding a pointer, a reference, an Alloc or a List is a ConstHoldsMemory error
that names the field: "SHAPES.items points to memory that only exists while compiling".
A string built while compiling is turned into a literal, because its bytes are known.
MIR gets one new constant:
ConstKind::Aggregate { variant: Option<usize>, fields: Vec<Const> }Codegen writes small aggregates as C compound literals. Arrays over a size threshold become a
single static const global that each use reads from, so a 256-entry table isn't repeated at
every use.
4.4 Constants used in types: in rounds
A constant worked out by running code can be used where typeck or the prover need its
number: array lengths (struct Ring { slots: [i32; SIZE] }), [0; SIZE], and an @if's
condition. Done (step 15), in rounds (typecheck_rounds in the driver):
- Typeck checks the program with the constants worked out so far
(
som_typeck::check_with(ast, names, &known)). A computed constant needed where its value has to be known, and not known yet, is needed: what's made of it isTy::Error, with no error said (it unifies with anything, so nothing follows from it). - If nothing's needed, or there are real errors, that's the result.
- Otherwise each needed constant is worked out:
som_lower::lower_initslowers its initializer and only what it runs (lowering's worklist finds methods through traits, drop glue,@on failfunctions), the borrow checker checks that, andsom_comptime::evaluateruns it. If what it runs has a type that isn't known yet (made of another constant still to be worked out), it waits for a later round. - Then typeck checks the program again, with them known. A round that works nothing new
out is a constant needed to work itself out:
ConstCycle(typeck's), at the constant.
The prover and lowering use the values worked out (Typed::const_value). Rounds were chosen
over evaluating on demand inside typeck (how Zig and rustc's queries do it): what a
constant runs is decided by lowering, not typeck, so a typeck-side "what it calls" would miss
dispatch, drop glue and listeners; and a constant's value feeds back into the whole program's
types anyway. A program that needs no computed constant in a type is checked once.
4.5 @for and @if inside function bodies
The body of an @for is checked once per iteration, with the loop variable bound as a
statically known value, the way a generic body is checked per type argument. Lowering unrolls
it. Because each copy knows i:
.components[i]withiknown to be belowNneeds no@requireand no check- each copy can have different types (needed for
@fields(T), phase 3)
@if type-checks only the branch its condition picks. The other branch is parsed and
resolved, but never type-checked or lowered. This is what lets @if N >= 3 { .components[2] }
compile for N = 2.
Phase 2 limit: the iterable and the condition can't depend on a generic parameter of the function they're in. Allowing that means checking generic code once per instance (phase 3).
4.6 The big question: checking generics once vs per instance
Som checks a generic function once, against its bounds, like Rust does. Reflection
(@fields(T), @variants(E)) and @if N >= 3 in generic code don't fit that: what the body
contains depends on T and N.
Two ways to do it:
- Check per instance, only where it's needed. A generic item that uses
@for/@ifover its own parameters, or reflection on them, is checked again for each instance it's used with. Errors name the instance ("inVector<2, f32>"). Every other generic stays checked once. Recommended: the cost is paid only by code that asks for it. - Bound the reflection.
T: structcould make@fields(T)available, with field types only known astype. This stays checked once but can do far less.
Phase 3 depends on this choice.
4.7 Checking generics per instance
Decided: option 1 of 4.6, checking per instance only the generic code that needs it.
Done: @if on a function's generic value parameters (per_instance_*.som). Checked
once, generically, an @if whose condition is worked out from the function's (or its impl
block's) value parameters is a region: not checked, and the function is checked for each
instance. Lowering, as it reaches an instance (describe<2>), checks it
(som_typeck::check_instance): the program's items again, with the instance's parameters
standing for what they are, then only that function's body, where @if N == 2 is known. It
starts from lowering's type table, which only grows, so the ids lowering has stay theirs and
it takes the grown table back. Lowering builds the body from what that check found
(FnBuilder's own typed); errors in it, only those inside its regions, are said as the
instance's, with where it's first used ("in describe<2>: …", Error::InInstance, whose
kind is the error's). An instance nothing uses isn't checked. Two @ifs that between them
always return aren't known to, checked generically (region_missing_return.som).
Each instance is proven too (som_prove::prove_instance), with its value parameters numbers
(N is 3, not Place::Len(N)): .cells[N - 1] under @if N >= 1 is proven in bounds, and
.cells[N] is said to be out of them, as the instance's error (per_instance_index_*.som).
The generic prover skips regions.
Done: @for f in T over a generic type (fields_of_a_generic.som,
fields_eq_generic_args.som, fields_indexed_generic.som). Once every module is loaded, an
@for among statements over a name that's no struct or enum is written out once for each
struct there is (Ast.per_type: the statement standing for it → each struct's copy, a
block), and each copy resolved where it's written. The type checker says whether the name is
one of the function's type parameters (else ReflectNotStruct); if it is, the statement is a
region. Checking an instance picks the copy for T's struct (Typed::chosen_arm), which
lowering and the prover then use; T that isn't a struct (count<i32>) is
ReflectNotStruct, and a field without != is said as fields_eq<Holder>'s Mismatch.
The AST grows by a copy for each struct for each such @for.
Not done yet: pinning (a region can still change the type of a variable outside it, per instance; each instance is checked as a whole, so it's consistent, but the generic check doesn't see it).
@for (i, k) in T among a match's arms, over a generic enum: the whole match is written
out once for every enum (Ast::per_type_matches), with that enum's arms, and is a region:
each instance checks the copy for T's enum (Typed::chosen_match), which lowering and the
prover use. T that isn't an enum is ReflectNotEnum. One such @for per match, for now.
A function whose @ifs on its parameters decide whether it returns (@if N >= 3 { return a; } else @if N < 3 { return b; }, or two @ifs one after the other) is checked for that in
each instance, where they're worked out: pick<1> falling off its end is the error.
The plan, in two passes:
Pass 1: function bodies. @if N >= 3 { … } and @if N == 2 { … } in generic function
and method bodies, including branches that only type-check or prove for some N; @for over
a generic parameter where copies differ; and what step 14's reflection (@fields(T)) needs.
- Typeck finds them. A generic function's body is checked as now. If it meets an
@ifor@for(later, reflection) that depends on the function's own generic parameters, what it found for that body is thrown away, its errors too, and the function is marked per instance. Other generic code stays checked once. - Lowering asks for each instance. Lowering already finds the instances (
describe<3>,Vector<2, f32>), walking frommain. For a per-instance function it asks for that instance to be checked: the body checked again with its parameters bound (NisTy::Value(3),Tisf32). The check starts from lowering's own type table, so itsTypeIds are lowering's (the table only grows, so lowering then takes it over). It gives aTypedfor that body, where@if N >= 3is a knowntrueorfalse. - The prover runs per instance too, with
Na number, notPlace::Len(N):.components[2]under@if N >= 3is proven. - Lowering builds the body from the instance's
Typed.FnBuildergets atypedof its own instead ofself.l.typed(about 100 places), the global one for everything else. - The driver says which instance an error is in: "in
describe<2>: …". Instance errors are collected while lowering and reported before the borrow checker and codegen. - The borrow checker already works on per-instance MIR;
lower_generic's stand-ins skip per-instance functions.
Stages, the corpus passing after each: instance checking in typeck; lowering wired up, for
@if over a value parameter; the prover per instance; errors naming the instance.
@for f in T over a generic T (fields_eq<T>, step 14) needs a step before these: its
copies can't be written out after loading, as a concrete type's are, since T's fields are
only known for an instance. For each instance of such a function, the @fors over its
parameters are written out then (from the body's place in the source, as now), the copies
resolved in the function's scope, and only then checked and proven as above.
Pass 2: members only some instances have. Done:
impl<N: usize> Row<N> {
@if N >= 3 {
pub prop third = .cells[2];
pub fn sum3(&self) -> i32 = .cells[0] + .cells[1] + .cells[2];
} else @if N == 2 {
pub prop third = .cells[1] * 10;
} else {
pub prop third = 0;
}
}
impl<T> Pair<T> {
@if T == i32 { pub fn sum(&self) -> i32 = .a + .b; }
}- Parsing. An
@ifamong an impl's members gives each member in it the condition (Func::when, nested@ifs all of theirs).elseadds!(c)(anExpr::Notover the same condition),else @if dadds!(c)andd. Resolve resolves each condition once, with the block, in terms of the block's own parameters (not a member's copies of them), so members in one@ifshare it. Members of one name in different branches aren't duplicates. - Types in conditions. In an
@if's condition (in a body, too) a type can be compared with another:T == f32,T != Point. A primitive is parsed asExpr::Type; a name that's a type and not a value resolves to the type. Known onceTis: for each instance, or at a member's use. A generic struct named without arguments (Pair) is anyPair. A type compared with a value isTypeNotCompared. - Each member is checked generically, quietly: for its type, which a
propwithout one gets from its body. Its errors are dropped and it's marked per instance; the member, whole, is a region, so it's checked and proven for each instance lowering reaches, with errors said as "inRow::third<2>: …". - At a use (
row.third,row.sum3()), the block's parameters are matched against the receiver's type (Row<2>:Nis 2) and the conditions worked out. False:MemberNotHere(Row<2>has nothird); not known, the receiver's type generic:MemberMaybeNotHere, whose hint is to use it in@if N >= 3 { … }(pass 1), where each instance is checked and it is known. Among properties or methods of one name, those whose condition is false don't count. - The editor completes only the members a value's type has
(
som_typeck::member_holds, which works the conditions out from the finishedTyped).
Value parameters in the prover. N is a place the prover knows facts about
(Place::Len(N), the same as a.len of an [T; N]): if N >= 3 { a[2] } and
@require N >= 1 prove a[0] in a generic body, where the type checker used to reject a
written index into an array of length N outright. At a call, N is the call's argument: a
number (first(&[]) gives "N >= 1 is false"), or the caller's own M, which its own
@require has to say enough about.
4.7b Calling C while compiling
Like Jai's #run, an extern function is called for real (8-comptime/src/foreign.rs):
looked up in its extern "lib" block's library, loaded once and never unloaded, or, for C's
own (extern "c", a plain extern { … }), in the compiler's process. libffi makes the call
from the declaration's signature; a variadic's extras are promoted as C does. The functions
the interpreter does itself (memcpy, strlen, snprintf, …) still are.
The interpreter's memory isn't real memory, so it crosses into C through a bridge:
- An allocation a call's arguments reach (through pointers in it too) is pinned: a real buffer at a fixed address for as long as the constant is being worked out. Before each call every pinned allocation is copied into its buffer; after it, what C changed is copied back (a pointer's bytes C didn't change stay the pointer, the rest becomes data).
- A pointer to a local passes a copy of what it points to, written back after.
- An address C gives (
getenv's text,fopen'sFILE*) is aRoot::Foreignplace, read and written as real memory; one inside a pinned buffer is that allocation's, sostrchrandstrtod'sendcompare equal to the interpreter's own pointers.
Decided: the editor calls them too, on every check. Not supported: callbacks (a function
pointer passed to C), i128, slices by value, enums and optionals by value, static libraries
(.a: only a shared one can be loaded). A constant's value can't hold an address from C
(ConstHoldsMemory), but text it gives, kept as cstr or string, is copied into the
program. What C keeps of a pinned buffer after the constant points to nothing.
4.7b2 Names made by @for around items: (i, a) and @ident
Done. An @for around items can name each element's place too, @for (i, a) in [x, y],
written as its number, and make one name of several, @ident(a, b, "_", i): names, numbers
and text joined. @ident is joined as each @for writes out its copies (fold_idents),
except inside an @for still to be written out, whose names aren't there yet, so nested loops
work. The standard library's swizzles are written so, every one a prop an editor lists:
@for (i0, p0) in [x, y, z, w] {
@for (i1, p1) in [x, y, z, w] {
pub prop @ident(p0, p1): Vector<2, T> = Vector { components: [.components[i0], .components[i1]] };
…
}
}The loops' own names have to be ones the elements aren't: [r, g, b, a] holds a.
4.7c Implementations from fields: @fields and @pub_fields
Done. A trait can say it applies to every struct and enum made of things that have it,
with a body written once, in som, by reflection. The standard library's Eq and Hash are
the first:
impl<T: @fields Eq> Eq for T {
fn eq(&self, other: &T) -> bool = match self {
@for v in T {
v(mine) => match other {
v(theirs) => {
@for f in v { if mine.f != theirs.f { return false; } }
true
},
_ => false,
},
}
};
}- The bound.
T: @fields Traitis onT's fields, notT: the implementation is a struct's or enum's (every variant's fields) when each field's type hasTrait, counting this implementation itself, a field's type parameters read as the type gives them (Pair<i32>'sTisi32). The numbers' built-in operators count (i32isEq), andT?isEqasTis. A raw pointer, a reference or a function never counts: a type holding one says itself what it is (List,Rc). Checked insom_typeck::find_impl, fromImpls::fields, each struct's field types as written, read lazily through the type's arguments, so lowering and the editor use the same rule. - A type's own implementation comes first, as for any blanket implementation; that's how a type opts out, or does something else.
- Privacy.
@fieldsgoes through every field, private ones too, and its methods read them (check_field_visiblelets them): for traits that compare, hash or copy, showing none of them.@pub_fieldscounts only the fields seen from where the implementation is written, and@for f in Tin it writes out only those: for traits that show what they find (Debug), so a type's private fields stay its own. The parser marks such an@for(Reflected::seen_only) and leaves the others out (field_seen). - Reflecting variants and what they hold. Among a
match's arms,v(b)is variantvwith what it holds bound,Circle(b__0),Rect { w: b__w, h: b__h }, and@for f in v { … b.f … }goes through its fields,b.fwritten as the binding. Over a genericT, a struct is written out too, as the one variant it has:v(b)binds all of it, andb.fstays a field. So one body serves both. An arm written by reflection that some copies never reach isn't an error (Ast::reflected_bodies):_ => falseabove, for a struct. - Hash hashes the variant's index, then each field;
impl<T: Hash> Hash for &Thashes what a reference refers to, since a binding of a field that doesn't copy is one.
Anyone can write their own: impl<T: @fields Count> Count for T { … }
(traits/fields_bound_for_a_trait_of_your_own.som).
- Making one. Among a constructor's fields,
T { @for f in T { f: …, } }writesf: …,for each field (a field given by name comes first, the reflection leaving it out). Over a genericTthe constructor is written out for every struct (Ast::per_type_constructs), each copy bound to its struct itself (Ast::constructs_of), not by its name, which where it's written may not see, and each instance picks its own, as amatch's copies are. WithTrait::make(…)'sSelftaken from what's expected, a struct of what's made is made:wgpu::Room,let cells: Cells = gpu.room(count)(const_eval/constructor_fields_by_reflection.som).
A field of a type parameter has what the parameter's bounds give it (Impls::param_bounds),
so in a function where T: Eq, a Pair<T> is Eq too. Arrays are Eq and Hash when their
items are (the standard library's impl<T: Eq, N: usize> Eq for [T; N]), so a struct holding
one is, Vector among them. A @fields bound anywhere but an implementation for every type's
parameter is an error (FieldsBoundMisplaced).
4.8 Other things left for later
Only rerunning what changed. Constants are worked out again on every check, including the C they call. Keeping each one's value with what it depends on (the functions it runs, the constants it reads) and rerunning it only when one of those changed would make the editor faster, and calls into C less surprising.
A query-based type checker. Constants in types are worked out in rounds and instances are checked as lowering reaches them, with no typeck restructuring; pass 2 of 4.7 didn't need one either (a use works its member's conditions out from the receiver's type). Re-checking only what changed, for an editor's speed, could want queries.
What macros do that this doesn't (compile-time code instead of a macro system, like Zig): names made from an element (
@ident, below); calls with any number of arguments (println!'s; tuples or variadics); new syntax (html!,sql!: not planned); embedding files (@embed, step 9); attributes on items beyond@on failand@test.Where a failure is, when the program runs. While compiling, an error in the standard library is said where the program called into it. When the program runs,
assert's@failsaysbuiltins.som:… in assert. Planned: standard-library functions with@failor@unreachablein them take the caller'sLocationas a hidden last parameter, passed on by any that are themselves such functions (Rust's#[track_caller]).Locationwould beCopy, itsfileandfunctioncstrs.Names made from an element:
@for N in 1..4 { fn twice_N() … }writestwice_Nfour times over.@ident("twice_{N}")(step 14), or another spelling.@ifas a value:@if N >= 3 { a } else { b }can't end a function as its value, in generic code or not: it's a statement, so each branchreturns.The editor runs constants on every check: one that runs for the step limit (10 million steps) takes a few seconds in a debug build. The editor could use a lower limit.
Types on functions (from the same discussion, not compile-time code): no
->meaning()rather than inferring the return type, and properties with a: type, like Rust.
5. Built-ins
New entries in som_lexer::BUILTINS, so the editor offers them:
| Built-in | Gives | Phase |
|---|---|---|
@const |
See above | 1 |
@for, @if |
See above | 2 |
@fail("why") |
Stops with a message: when the program runs, it says what failed and where; in a @const, an error while compiling (ConstFailed). Stands for a value of any type, like @unreachable |
1 |
@on fail |
On a line above a fn: called with a &Failure (message, at: Location, unreachable) when the program fails, at @fail or @unreachable. The program's own listeners are called, in order, instead of the standard library's print_failure; it can call that itself. Not called while compiling, where a failure is an error |
1 |
@embed("path") |
A file's bytes ([u8; _]) or text (string), relative to the source file |
1 |
@variants(E) |
An enum's variants, each with .name and .index |
3 |
@fields(T) |
A struct's fields, each with .name and .type |
3 |
value.@field(f) |
That field of value |
3 |
@ident("xy") |
A name, made from text | 3 |
@size_of(T) |
See below | 3 |
assert(ok, "why") isn't a built-in: it's an ordinary function in the standard library,
built on @fail. Called when the program runs, it checks then; called with @
(@assert(SIZE % 8 == 0, "…");, which can stand on its own at the top level), it checks
while compiling. Anyone can write their own checks the same way. While compiling, an
error in the standard library is said where the program called into it, not inside it.
@size_of needs care: the C compiler lays out structs, not som. The interpreter already works
out C's layout for the memory it allocates (8-comptime/src/memory.rs), mirroring codegen's
struct definitions, so @size_of can use that.
6. Errors, all together
| Kind | Pass | Phase |
|---|---|---|
ConstNotKnown (exists, now also for @const, @for, @if) |
resolve | 1 |
ConstCycle (exists) |
resolve | 1 |
ConstCallsExtern |
comptime | 1 |
ConstReachedUnreachable |
comptime | 1 |
ConstDividedByZero |
comptime | 1 |
ConstOutOfBounds |
comptime | 1 |
ConstTooLong |
comptime | 1 |
ConstHoldsMemory |
comptime | 1 |
ConstNeededTooEarly |
typeck | 1, gone in 4 (gone) |
ConstFailed |
comptime | 1 |
7. The plan
Section 8 breaks these phases into small steps, each with its own test.
Phase 1: run code for values.
Parser:
@constdeclarations at item level, inimplblocks and in function bodies.constis still accepted, with a warning and a fix. Migratestd/,example/andtests/corpus/in the same change.Resolve:
check_const_exprallows calls, method calls, blocks,if,matchand loops. It still reports locals of a surrounding function (ConstNotKnown) and still findsConstCycle, now also through calls.Parser/lexer:
@constas a prefix expression, plus@assertand@embed.Typeck:
const_numberreturnsNonefor computed constants. Where a number was required, reportConstNeededTooEarly.Lower: computed constants and
@constsites become initializers andPendingoperands.New crate
crates/compiler/8-comptime(renumberingoptimizeandcodegen): the interpreter, baking, and the errors above.MIR/codegen:
ConstKind::Aggregateandstatic constglobals for big arrays.Done when
tests/corpus/constants/not_known_while_compiling.som, rewritten with@const, changes fromexpect: error ConstNotKnowntoexpect: exit 60, and tetris usesSHAPES.
Phase 2: @for and @if in function bodies, over static values that don't depend on
the function's own generic parameters.
Phase 3: generating items and reflection. Item-level @for/@if, @ident, @fields,
@variants, types as values (@for T in [u8, u16]). Needs the decision in 4.6.
Phase 4: computed constants in types, evaluated on demand during typeck (4.4).
8. Steps
The phases broken into small steps. Each one needs exactly one thing the step before it
didn't, and each becomes a test in tests/corpus/const_eval/ that should pass when that
step is done. Build them in this order.
Phase 1
Step 0. Constant arithmetic, spelled @const. step_00_arithmetic.som, exit 200
@const COLS = 10;
@const ROWS = 20;
@const CELLS = COLS * ROWS;Needs: the parser to accept @const declarations, and std/, example/ and
tests/corpus/ migrated from const. No interpreter yet. This is a good first commit on
its own. Done.
Step 1. Call a simple function. step_01_call.som, exit 4
fn kilobytes(n: i32) -> i32 = n * 1024;
@const BUFFER = kilobytes(4);
fn main() -> i32 = BUFFER / 1024;Needs: calls allowed in check_const_expr, initializers and Pending in lowering, the
som_comptime crate with call frames, integer arithmetic and return, and baking a single
number. This step runs the whole pipeline end to end. Done.
Step 2. Branches and recursion. step_02_recursion.som
fn fib(n: i32) -> i32 = n if n < 2 else fib(n - 1) + fib(n - 2);
@const FIB_20 = fib(20);Needs: the interpreter to follow branches and recursive calls, and the step limit
(ConstTooLong). Done.
Step 3. Loops, changing locals, and an array as the result. step_03_array.som
fn squares() -> [i32; 10] {
let mut table = [0; 10];
for i in 0..10 { table[i] = i * i; }
table
}
@const SQUARES = squares();Needs: places and indexing in the interpreter, range iterators (IterOwned/next
calls), and ConstKind::Aggregate in MIR and codegen. Done.
Step 4. Structs, enums and match. step_04_structs.som
enum Channel { Red, Green, Blue }
struct Rgb { r: u8, g: u8, b: u8 }
fn pure(c: Channel) -> Rgb = match c {
Red => Rgb { r: 255, g: 0, b: 0 },
Green => Rgb { r: 0, g: 255, b: 0 },
Blue => Rgb { r: 0, g: 0, b: 255 },
};
@const RED = pure(Channel::Red);Needs: variants and match (a switch on the variant) in the interpreter, and struct
aggregates in codegen. Done.
Step 5. Heap memory while running, with a result that has none. step_05_heap.som
fn first_primes() -> [i32; 8] {
let mut found: List<i32> = List::empty();
let mut n = 2;
while found.len < 8 {
let mut prime = true;
for p in &found { if p != 0 && n % p == 0 { prime = false; } }
if prime { found.push(n); }
n = n + 1;
}
let mut table = [0; 8];
for i in 0..8 { table[i] = *found.at(i); }
table
}
@const PRIMES = first_primes();Needs: the interpreter heap for the Alloc intrinsics, references, and Drop. Returning
found itself is ConstHoldsMemory (keeps_a_list.som). Done.
Step 6. Text built while compiling. step_06_text.som
@const MAJOR = 0;
@const MINOR = 3;
fn banner() -> string = "som v{MAJOR}.{MINOR}";
@const BANNER = banner();Needs: Display and display_len writing through raw *u8 pointers, and turning the
built string into a literal (count usize::MAX) in the program. Done.
Step 7. Constants inside impl blocks and functions. step_07_scopes.som
impl f32 {
pub @const TAU = f32::PI * 2.0;
}
fn wrap(i: usize) -> usize {
@const SIZE = kilobytes(1) as usize;
i % SIZE if SIZE != 0 else 0
}Needs: @const declarations in impl blocks and function bodies. In a body, the parser
tells a declaration (a name, then : or =) from an expression. Done.
Step 8. @const as an expression. step_08_in_place.som
fn is_prime_small(n: i32) -> bool {
for p in &@const first_primes() { if p == n { return true; } }
false
}Needs: @const as a prefix expression, initializers created at an expression rather than
for a name, and ConstNotKnown when it names a local such as n (local_named.som). Done.
Step 9. Checks and files while compiling. step_09_assert_embed.som
@assert(PRIMES[0] == 2, "the first prime is 2");
@const HELP: string = @embed("help.txt");Needs: @fail and the standard library's assert (done: const_eval/assert*.som,
fail_in_a_function.som), and the @embed built-in, with paths relative to the source
file. Phase 1 is done.
Phase 2
Step 10. @const once per generic instance. step_10_per_instance.som
fn identity<N: usize>() -> [[i32; N]; N] = @const make_identity<N>();Needs: N to count as statically known in monomorphized code, with one initializer per
instance. (This only needs phase 1's machinery, but it's the first step that touches
generics, so it opens phase 2.) Done: lowering keys an initializer by its constant and
the function instance it's in, and builds it with that instance's arguments.
Step 11. @for and @if over fixed values. step_11_unroll.som
fn sum4(v: [i32; 4]) -> i32 {
let mut s = 0;
@for i in 0..4 { s = s + v[i]; }
s
}
@const DEBUG = false;
fn log(msg: string) { @if DEBUG { print(msg); } }Needs: the body checked once per iteration with i known, the loop unrolled in lowering,
and the untaken @if branch never type-checked. Phase 2 is done. Done, with the body checked
once (each copy has the same types until phase 3): @for loops over a range of known numbers
(including a generic function's N, settled per instance in lowering) or an array written
out, and lowering unrolls it; @if and else @if take their condition from literals and
constants, and only the branch picked is checked and lowered.
Phase 3
Step 12. @for over a generic parameter. step_12_generic_for.som
impl<N: usize> Vector<N, f32> {
pub prop magnitude {
let mut sum = 0.0;
@for i in 0..N { sum = sum + .components[i] * .components[i]; }
f32::sqrt(sum)
}
}Needs: nothing beyond step 11. Done: the body is checked once, its types the same in
every copy, and lowering unrolls it for each instance's N. The decision in
4.6 is only needed once copies
can differ in type: @if N >= 3, and reflection (step 13 on). The prover checks the body once
too, for every N: .components[i] and .components[N - 1 - i] are proven, a value being
one term less another and a number (N - 1 - i < N is 0 - i <= 0).
Step 13. Generating items. step_13_items.som
impl<N: usize, T: Copy> Vector<N, T> {
@if N >= 3 { pub prop z = .components[2]; }
}
@for T in [u8, u16, u32, i32, i64] {
impl Hash for T { fn hash(&self, h: &mut Hasher) { h.write(*self as u64); } }
}Needs: @for and @if at item level, types as values, and the generated items registered
with resolve after expansion.
@for around items is done, written out before parsing: each copy of the body's tokens,
with the name written as that element (a type, or a number of a range), is put where the
@for was and parsed like anything else. Its list has to be written out, [u8, u16] or
0..4, since nothing is worked out yet. std/hash.som uses it. Not done: @if N >= 3 in a
generic impl, which needs 4.6;
and names made from the element (fn twice_{N}), which is @ident.
Step 14. Reflection. step_14_reflection.som
impl Kind {
pub fn index(self) -> usize = match self {
@for (i, v) in @variants(Kind) { v => i, }
};
}
pub fn fields_eq<T>(a: &T, b: &T) -> bool {
@for f in @fields(T) { if a.@field(f) != b.@field(f) { return false; } }
true
}Needs: @variants, @fields, .@field(f), and @for producing match arms. @ident
can come here too, for swizzles. Phase 3 is done.
Written @for (i, v) in Kind, not @for (i, v) in @variants(Kind): going through a type
goes through its variants (and, for a struct, will go through its fields). Done for
match arms: the @for is kept, its body's place in the source and where its copies go,
until every module is loaded; then Kind is looked up, preferring the module it's written
in, and each copy's tokens are written with v as the variant's pattern (Red,
Circle(_), Rect { .. }) and i as its index, parsed, and put among the arms.
Resolve and the type checker see the arms written out, so a match is still checked to cover
every variant.
Fields the same way, with no @fields or .@field(f): @for f in Point { if a.f != b.f { return false; } } writes f as each field's name, so a.f is a.hello, then a.bye;
(i, f) gives its index too. Done among statements, for a struct that isn't generic: the
@for stands as a statement until it's written out, then is a block of the copies. An @if
in the body can pick by i. Generic types too, fields_eq<T>, written out for each
instance (4.7). Not done: @for over a type among
items, and properties of the element (v.value, v.name, f.index). The body names the
element everywhere it's written, so a local called the same is written over too.
Phase 4
Step 15. A computed constant as a type. step_15_in_types.som
@const SIZE = next_power_of_two(100);
struct Ring { slots: [i32; SIZE] }Needs: evaluating during typeck, with cycle detection through types. Done, in rounds
(4.4): step_15_in_types.som exits 128, and
array_length.som, which was error ConstNeededTooEarly, now runs; a cycle through a type is
ConstCycle (in_types_cycle.som, in_types_struct_cycle.som).
9. Error cases and other tests
All in tests/corpus/const_eval/, in the corpus's usual format (// expect: exit N /
// expect: error Kind), next to the step tests from section 8. Some overlap with the steps;
they're listed here so every error kind has a test:
| File | Expect |
|---|---|
calls_a_function.som: @const W = width(); |
exit 60 |
table.som: @const SHAPES = all_rotations();, sums them |
exit … |
with_a_list.som: builds a List while compiling, returns an array |
exit … |
string_built.som: concatenates while compiling |
exit … |
in_place.som: @const in a function body |
exit … |
per_instance.som: @const f(N) in a generic |
exit … |
local_named.som: @const f(n) with n a local |
error ConstNotKnown |
calls_c.som: @const X = c::rand(); |
error ConstCallsExtern |
reaches_unreachable.som |
error ConstReachedUnreachable |
divides_by_zero.som |
error ConstDividedByZero |
forever.som: loop {} |
error ConstTooLong |
keeps_a_list.som: returns a List |
error ConstHoldsMemory |
array_length.som: [i32; computed()] |
exit 0 (was error ConstNeededTooEarly) |
calls_itself.som: @const A = f(); fn f() = A; |
error ConstCycle |
assert.som: @assert(SIZE % 8 == 0, "…") with SIZE 12 |
error ConstFailed |
wraps.som: i8 overflow while compiling equals run time |
exit … |
There's also one property worth testing outside the corpus: for any function in the corpus,
@const f(args) and f(args) give the same value. That catches the interpreter and the C
backend drifting apart.
10. Not in scope
- Running code while compiling that has effects: printing, files (apart from
@embed), the network, the clock. Results must depend only on the source. - Generating code from text (Jai's
#insert "…").@identcovers names; everything else is written as som. - Caching results between builds.