Adding a feature

The life of an encoding rule, from the question "what should this construct mean?" to a merged change. The steps are the same whether the change is a new accepted construct, a new kind of error or a changed rule. The rules behind them are the invariants in CLAUDE.md; this page puts them in order.

1. Read what exists

Start from the roadmap entry in plan.md, the decision records that the area already has (internal/decisions/), the encoding specification (internal/encoding.md) and the tests of the neighbouring constructs. Find the most similar construct that is already supported and read its record, its encoder code and its tests: a new rule usually extends one of those.

2. Write the decision record first

If the semantics are not obvious, write a decision record before any code (invariant 6). "Not obvious" covers any choice a reader could have made differently: what an operation checks, what a type means in a contract, what is rejected and what is accepted, and any new axiom or trusted contract.

A record is internal/decisions/NNNN-short-name.md, numbered with the next free number. Read two recent ones, such as 0047 and 0048, for the shape:

  • a title line with the number;

  • Status (accepted, by whom and when, and what changed it since), Milestone, and where relevant what the record implements or amends;

  • Context: what is being decided, why, and what was measured;

  • Decision: numbered items, each one a rule, with its reason. Later text refers to "item 7", so keep the numbers;

  • Consequences: what changes elsewhere (tests that move, pages that change, work left for later).

A record states facts it checked. Where a claim depends on how EDG or Silicon behaves, say which probe or run showed it.

3. Write the tests first

Every rule lands with at least one test that must verify and one that must fail for the right reason at the right line (invariant 2). Write them before the encoder, with the expectation you believe the record implies:

  • the positive test under tests/<area>/, with // expect: verified, and // expect: native-ok when it should also compile with ordinary compilers;

  • the twin under that area’s negative/ directory with // expect: error <category> @ L<n> (or rejected), and a // twin: line naming the positive;

  • a rejected test for every new construct you refuse.

If the new check is a new error category, the harness, the driver, the error-messages page and the tests all change together, and the harness self-test pins the set. See Testing.

Then implement. When a test fails, fix the code. Never edit an expect: line to make a test pass (invariant 1): if the expectation looks wrong, stop and discuss it, because a changed expectation is a changed meaning.

4. The encoder change

The code belongs where internal/encoding.md and Architecture put it: what the program says is read in src/backend/encode.cpp, what an operation means is decided in src/vir/lower.cpp, and the emitter prints and decides nothing. Three habits keep a change sound (Soundness):

  • reject what you do not model, with a diagnostic at the source line, rather than approximating (invariant 3);

  • introduce no inhale or assume without a cerastes:inhale-ok reason (invariant 4);

  • report errors at C or C++ source lines (invariant 7); a new check needs its position-map entry.

If you need to see the Viper of a program, build/cerastes verify <file> --keep-vpr; to see the IL, scripts/cerastes-fe.sh --c++20 --cerastes_walk <file>.

5. Audit

Before a change that touches src/backend, src/vir or src/viper is committed, have it reviewed for unsoundness by someone who did not write it, using the checklist in .claude/agents/soundness-auditor.md. A review finding that is blocking is fixed and re-reviewed. Record in the decision record which rounds the change went through and what they found.

6. Document it

User-visible behaviour lands with its documentation in the same change (invariants 8 and 9): the reference page for the feature, the error-messages page for a new category, and the annotation or command-line text for a new option. Every code example is a test, so it passes the same harness (Writing documentation).

When the change completes a concept that the tutorial teaches, or one it does not yet teach, its chapter lands in the same change (decision 0049; the chapters are in pages/tutorial/). Every change also keeps the existing chapters true: a message, a category or an accepted construct a chapter shows is updated with the change.

7. Check and commit

scripts/check.sh

must end with check.sh: all green (the full check). For a quicker loop, run the tests you touched with python3 scripts/run_tests.py <file|dir>; after a green full check, targeted runs are enough for later test edits.

Commit conventions that matter to a contributor:

  • A commit has a message that says what changed and why, and names the milestone or record it belongs to.

  • A golden file or a trusted-contract list that changes is a change of an expectation: give the reason in the commit message and review the diff line by line.

  • EDG source is never edited in third_party/edg; changes to EDG go through the fork (The EDG fork).

  • Pull requests run CI’s PR tier (CI); the nightly full check covers main.

Code style

The tool code (src/ and include/cerastes.h) is written in the maintainer’s Boost libraries' style (decision 0051):

  • Allman braces everywhere, lambda bodies included, and braces around every controlled statement, even one line long; else and catch start their own line;

  • four spaces, no tabs; case labels indented one level; public: at the class’s column; about 120 columns;

  • the pointer and reference marks on the type (T* p, const char* first, T& r), one declarator per declaration;

  • brace initialization (std::size_t n {0U};, T x {e};), except that a lambda keeps = (const auto put = [&](int v) { … };);

  • U on literals that an unsigned type receives (n == 0U for a std::size_t n) and on every shift count (x << 3U; the shifted literal only when its type is unsigned);

  • preprocessor directives inside a function indented to the code’s level.

  • no comment on a line that holds a brace: a comment goes on its own line above the statement (or inside the block), never after { or }. Older code still has such lines; each is moved when its code is next touched.

The layout is clang-format’s, with the configuration in src/.clang-format (and a byte-identical copy in include/.clang-format) and a pinned clang-format that scripts/bootstrap.sh installs:

scripts/format.sh          # format the tool code in place
scripts/format.sh --check  # what check.sh runs: name each file that differs

Brace initialization, U, one declarator per declaration and the brace-line comment rule are review rules: no tool checks them. Tests and documentation examples are never formatted: their lines are expectations, and the root .clang-format disables formatting everywhere but src/ and include/, so an editor’s format-on-save leaves them alone. EDG’s code in third_party/edg keeps EDG’s own style.

A change meant to change nothing (a rename, a restyle, a moved function) can be checked long before a full check: python3 scripts/object_identity.py --before <base> compiles every translation unit from both sides and requires the same LLVM IR and, but for the line numbers EDG’s assertions receive, the same object files, on the platform it runs on (Testing).