14 KiB
Contract AIR bindings candidate
Non-normative completion of the binding grammar in RFC 0039, following source elaboration and accounting/diagnostics. This document allocates no feature, instruction or diagnostic and authorizes no implementation. Compatibility, trap/dispatch schemas, owner adoption and full vertical delivery remain gates.
Text and identity
Reuse AIR format 0's tokens, comments, significant instruction newlines, global/local namespaces and list conventions (air-text.ebnf). Proposed additions are:
contract_pre = ( "contract.pre" | "contract.pre.assume" | "contract.pre.document" )
global bindings ;
contract_old = local "=" "contract.old" global bindings ;
contract_post = ( "contract.post" | "contract.post.assume" | "contract.post.document" )
global bindings ;
bindings = "(" [ binding { "," binding } ] ")" ;
binding = [ "ref" ] ( local | "result" ) ;
checked_type = "checked" "<" type "," global ">" ;
checked_op = "checked.make" checked_type "," local
| ( "checked.view" | "checked.take" ) local
| "checked.modify" local "," global "(" [ local_list ] ")" ;
The generic instruction production still governs checked operation result bindings: make/view/take bind exactly one result; modify follows ordinary call rules (one for a value return, none for void, optional for never). A never callback cannot publish an update or produce an observable result; an optional never-typed SSA binding does not change this. Old binds exactly one snapshot, while pre/post bind none. There is no old.assume/document or optional old result. Lists have no trailing comma, matching ordinary AIR call lists. Empty bindings are valid for parameter-free helpers. There are no type annotations, constants, projections, nested calls, mut/move modifiers or inline expressions in a binding. Compute those inside the helper. Source spelling remains type.checked and explicit static function application; this grammar does not reserve a source module named checked.
Checked operands and modify extras remain ordinary body SSA operands, not restricted clause roots. Make's explicit checked type determines R/p without operand inference; its operand has exactly R. View accepts ref/mut<checked<R,p>> and produces a shared dependent ref; take accepts the whole checked value and produces R. Modify's first operand is mut<checked<R,p>>, and its callback signature starts with mut, followed by exactly the extra argument types. The implicit first argument counts toward the 64-argument bound, leaving at most 63 extras. Callback return/effects/loan rules remain those of the invariant candidate; no runtime function value or optional inline callback is added.
The word result is a contextual placeholder only in these binding lists. %result is always an ordinary SSA identity and may name a header parameter; @result is an ordinary global. Names such as %ref remain locals. Parsing does not infer a placeholder from an SSA spelling or reverse a generated name. Contract operations occupy the uninterrupted entry prefix pre*, at most one old, then post*; entry has no predecessors. Interleaved modes retain textual order within each class. Ordinary instructions, including constants, start the body and end the prefix. These are CFG rules, not a new function header or lexer keyword policy.
A resolved binding is (root,form,span), where root is a header parameter ordinal, this function's single snapshot identity, or result, and form is value or shared. Span identifies the binding token (ref when present) separately from the installing operation. The parser retains ordinary ValueIds for local name resolution; verifier preparation converts legal roots to the typed descriptor without inventing a body SSA type. Bindings are descriptors, not immediate ordinary SSA reads: the SSA stage classifies their root/context before applying ordinary instruction-use dominance checks. Thus a pre naming the later snapshot reports snapshot_not_post, rather than pretending it reads that snapshot early. Ordinary body uses retain their existing SSA rules. All binding fields needed for pre-SSA budgets come from the header or old-helper signature. A malformed/unknown root is not dereferenced for size calculations.
Local resolution is ordinary function-wide AIR resolution, including forward definitions. A known body/block-parameter identity is not silently reinterpreted as a header parameter. Function lookup accepts forward global declarations; helpers need not appear before their uses. Repeated bindings are legal and preserve their order and count. Handwritten AIR is not deduplicated; repeated Copy ref bindings materialize separate places, while repeated in-place shared roots create ordinary overlapping shared children. Source capture deduplication remains a frontend rule, with each selected binding counted once. Canonical serialization preserves the binding list and inserts ref only for shared form; it cannot erase a duplicate or replace result with a ret-specific SSA name.
Observation type and lifetime
A binding's produced type must exactly equal the corresponding helper parameter. Arity must match and retain the ordinary 64-argument bound. There is no numeric conversion, array-to-slice coercion, dereference or owner/checked representation fallback based on the expected helper type. Post result type is the enclosing declared return type, independent of which valid SSA operand a particular ret uses. The checked predicate signature is exactly ref -> bool; clause/old root adaptation is a different operation from make/modify's whole-R observation.
| Root type T | Value form | Shared form |
|---|---|---|
| Data T, including snapshot Data | T, ordinary Copy observation | ref, through a complete temporary Copy place |
| complete Copy non-Data T, e.g. cap | rejected | ref, temporary Copy place; predicate rules still forbid extracting non-Data content |
| ref or mut | rejected | ref, shared child/reborrow |
| slice or slice_mut | rejected | slice, shared child/reborrow |
| own | rejected | ref, ordinary owner-content view |
| own_array | rejected | slice, ordinary owner-array view |
| addressable linear aggregate/resource/graph/buffer root | rejected | ordinary borrow.ref result type, dependent view without ownership transfer |
| checked<R,p> | value only when R is Data | ref<checked<R,p>>; Copy uses a temporary place, linear uses a direct dependent SSA-root view; never implicitly ref |
Apply the ordinary type validity and root-position rules before accepting a row. Storage/function-bound/void/never values do not become complete Copy places merely because a shallow Copy query returns true. Shared observation of a borrowed checked value uses the borrow row, preserving checked content. Predicate P entry signatures contain no mutable parameters; S additionally applies to persistent checked predicates. The table does not admit new operations to P/S or widen ordinary body addressability. On Data checked values, shared materialization preserves the existing claim without reevaluating the predicate.
Linear checked roots require an explicit checked case in ordinary borrow.ref typing and runtime root access, analogous to linear named/array roots; a frame local cannot substitute because it requires Copy content. Before CFG/SSA, the accounting candidate charges every Copy ref occurrence to E(clause/old), including duplicates and shared snapshots/results, using header/helper descriptors and its total size/Copy queries. The activation bound is L + S + max E; each materialized complete value also retains the ordinary maximum-value bound. For example, 64 ref bindings of array<u8,16385> with L=S=0 require 1,048,640 bytes and reject E5101, whereas 64 bindings of array<u8,16384> exactly fit. Sequential clauses use max, not sum; views within one evaluation coexist. Documented bindings create no runtime places and contribute no E, while their argument/W limits and virtual access checks remain.
Pre/old bindings may name only original header parameters. Post bindings may additionally name the earlier snapshot and result. A bare borrow or linear root cannot bind by value; shared roots require explicit ref. Result in pre/old is rejected even if no evaluation would occur. A void function has no result binding; a never function cannot install any post, including document. No contract fact revives a consumed root or bypasses a frozen parent.
Pre evaluates in entry state; old evaluates after all executable pre helpers and before the body. Post installation creates no view/place/read. Every normal ret re-resolves its bindings against the final pre-transfer state, including result's actual operand and the current loan ancestry. All arguments of one helper are prepared in list order and coexist for that evaluation. Their temporary views/places and descendants end before the next predicate or normal result transfer. Helpers may not return their views through bool/Data. Post observation through an exclusive result may be valid while a simultaneous observation through its frozen input parent rejects; selecting a different form does not bypass ordinary access checks. Preserve the isolated return/post collection rule in the accounting candidate.
The snapshot is a named contract-only Data value, not a general body definition. Its only legal uses are post bindings, including shared form. It cannot be passed into old/pre, copied/projected in the body, used in an ordinary helper call/branch or returned. At least one executable post (runtime or assume) must use it. A documented post may reuse that snapshot but cannot be its only consumer. Projections occur inside post helpers after receiving the Data record; direct prefix field projection is not a binding. No snapshot is created for a function with no executable old expressions.
Stages and factual errors
Ordinary parsing/name resolution retains E0001 for malformed syntax, E0004 for unknown globals/opcodes and E5001 for undefined/duplicate SSA values. Known new operations require their registered feature before verification (E0006 if missing); assumed variants require their separate extension. No registry spelling/status is allocated here. Unknown-opcode research probes do not establish full format-0 compatibility, particularly when an unknown snapshot also has a dependent body use (Q-055).
Limits use total queries/descriptors and retain E5101; CFG checks prefix order and entry predecessors with E5002. SSA then checks root identity/context and snapshot escape/use. Proposed E8001 has stable reason values:
| Reason | Fact |
|---|---|
| invalid_root | a resolved binding names a body definition or block parameter rather than an allowed root |
| result_not_post | result is used by pre/old |
| snapshot_not_post | a snapshot binding is used by pre/old |
| snapshot_escape | an ordinary body instruction uses the snapshot |
| snapshot_unused | no post binding uses the snapshot |
| snapshot_document_only | post uses exist, all in document mode |
Invalid root/context facts belong to each binding use; escape belongs to the body use. Unused/document-only belongs to the old declaration, and is classified only after that snapshot's root/context/escape checks pass, preventing a secondary unused diagnosis for the same misuse. Any correctly resolved snapshot binding in a post counts as a use, even if another binding in that post is invalid. An old operation referencing its own snapshot is snapshot_not_post and suppresses its dependent unused check. Ordinary SSA duplicate/undefined checks remain earlier evidence; descriptor operands do not undergo ordinary dominance checks and cannot add a competing E5001 for an invalid root/context. Prefix structural failure stops before SSA, and malformed root metadata cannot cause an earlier budget crash. This table defines proposal reasons, not allocated public codes.
Candidate E8001 code-specific fields are function,block,inst,reason,binding,root, in that order; none is a new common-envelope field. Function/block are the enclosing AIR names without sigils and inst is the zero-based primary instruction index. Binding is the zero-based argument index for a binding misuse, null for a body escape or old-declaration failure; root is %name for the actual resolved local or result for the placeholder. A body escape identifies the actual body instruction; unused/document-only identifies old; invalid bindings identify their prefix instruction. No synthetic ValueId or invented name is serialized. Types/access errors retain their existing payload rather than adding this schema.
Types checks Data/value admissibility, shared adaptation, exact arity/signatures, Data old result and bool pre/post result with E1001. It also rejects result on void and every post on never. Complete ordinary type validation precedes P/S and their proposed E8002/E8003 witnesses. Effects and unsafe follow existing stage order. At the evaluation boundary, use existing initialization, availability, borrow-conflict and lifetime diagnostics; a post's invalid result transfer preserves the ordinary return diagnostic list and suppresses dependent post checks. An alias-conflict message is not relabeled as invalid_binding. Runtime false/helper traps and their exact invocation metadata remain a separate gate.
The binding vectors retain 18 current-language probes at 5ed0d86: eleven ordinary-view/call cases verify owner/owner-array adaptation, mut/slice_mut reborrows, simultaneous shared children, frozen-parent rejection, exclusive-result observation, scalar/authority Copy non-addressability, and both never-call binding forms. Seven complete prefix spellings reject E0004 alone without falsely declaring a future feature. These constrain the design; they do not test implemented contract bindings or settle Q-055.
Future vertical fixtures must cover every grammar form/mode, empty/64/65 lists, duplicate binding storage, scalar Copy places versus owner unwrapping, result/%result, snapshot shared uses/escapes, unrelated prefix/SSA/type errors and return legality. Current-language probes and candidate vectors are evidence for these requirements, not implemented-contract conformance.