Files
aji/docs/design/contract-source-errors-and-compatibility.md
T

62 lines
8.7 KiB
Markdown

# Contract source errors and AIR compatibility audit
Non-normative completion of the source rejection map for [RFC 0039](../rfcs/0039-verifier-visible-contracts.md), following [source elaboration](contract-source-and-module-state.md), [AIR bindings](contract-air-bindings.md) and [runtime context](contract-runtime-and-context.md). No reason, code, feature or language rule is allocated here. This candidate does not authorize implementation or resolve Q-055.
## Source rejection map
Keep spec 15 §10's frontend/verifier division. Syntax and source-resolvable type/name errors stop before AIR; independently verified access, ownership, purity, stability and status obligations retain their AIR codes and independently mapped positions. A source type mismatch must not become E1001 from a generated helper. New E7006 reasons below are proposed additions to the existing stable registry; existing reasons keep their meaning. No message parsing or parallel `contract_reason` vocabulary is needed.
| Source fact | Candidate code / reason | Primary position |
|---|---|---|
| Malformed/empty clause group, missing terminator, misplaced group or unsupported mode spelling | E7001 with ordinary expected/found | offending token |
| Unknown value/function/type, including a body local absent from the clause environment | E7002 | unresolved name |
| Header parameter or clause binder named result when ensures exists | E7006 / contract_result_conflict | conflicting binder |
| Snapshot-shaped unqualified old call also resolves to a visible function/parameter/binder | E7006 / contract_old_conflict | call target old |
| Inline return in a clause or old expression | E7006 / contract_return | return keyword |
| Nested old inside executable/documented old expression | E7006 / contract_old_nested | inner old call target |
| result referenced inside old expression | E7006 / contract_old_result | result occurrence |
| old expression refers to a known enclosing clause-local binder rather than an original header root or its own scratch local | E7006 / contract_old_capture | captured name |
| Documented old has no structurally equal executable capture | E7006 / contract_document_old | documented old call target |
| Checked alias-resolution cycle | E7006 / invariant_cycle | reference that closes the active resolution chain |
| Non-bool clause; non-Data old value; result on void; any ensures group on never | E7005 with expected/found | clause expression; old argument; result occurrence; ensures keyword, respectively |
| Wrong checked predicate/callback signature or static argument kind | E7005 with existing static-signature fields where applicable | supplied fn/type argument |
| Wrong runtime operand type/mode, including mutable projection through a checked representation | E7005 with expected/found | operand/place |
| State { ... } attempts to construct a checked alias as a struct | E7005 | literal's type name |
| zero State or zero of a containing non-zeroable type | E7006 / zero_not_zeroable | zero expression |
| break/continue outside a loop within the inline expression | E7006 / loop_control_outside_loop | control keyword |
| Existing explicit transfer, borrow-binding, duplicate declaration or static-function restriction | existing source code/reason | existing position |
| Source alias/specialization depth, instance count or key-byte bound | E7006 / implementation_limit | existing expansion/application position |
| Generated helper exceeds AIR argument, frame, value or virtual-boundary bounds | E5101, mapped through generation provenance | offending root/clause/declaration mapping |
There are eight proposed reason additions, all for newly introduced constructs. Ordinary unknown names remain E7002 even inside old. The special capture reason requires a resolved enclosing clause-local identity; it must not be guessed from spelling. Unqualified old recognition remains exactly the source candidate's one-argument ensures form. Qualified old calls, old outside ensures and body bindings named result retain ordinary rules.
Check conflicts before binding the special result/old forms; classify nested old and result within old before checking snapshot value type or document matching. Type-check a documented expression before searching for an equal executable capture. A matching capture is still eager work, not proof that its expression terminates. Alias-cycle detection uses the active resolved alias identities; an ordinary nominal representation reference stops alias expansion, while finite-layout validation remains a separate obligation.
Existing global/signature phase stops, first body error per source declaration, generic-template skipping, limit 32 and code/location dedup remain in force. These rules do not make malformed or type-invalid clauses reach the AIR verifier to collect extra purity errors. Once typed lowering succeeds, P membership precedes S membership, and ordinary access errors remain factual access errors. Generated headers, deduplicated roots, snapshot projections and actual source helper instructions use the already specified provenance map; no generated-name parsing or fabricated source binder is introduced.
The table is a delivery checklist, not compiler output. Vertical tests must pair every new rejection with a valid neighbor: ordinary body result; qualified old; inner scratch rather than outer capture; own loop rather than outer break; Data old rather than non-Data; executable/documented matching versus mismatch; nominal recursion versus alias cycles. Existing E7006 reasons and contract-free CLI records must remain unchanged.
## Executed format-0 rejection baselines
[Ten current-language vectors](contract-compatibility-vectors.json) at c633f23 retain complete source and emitted diagnostic fields except the physical file label. All are `aji check` calls, exit 1; no runtime/model experiment is involved. Relevant helper declarations are included so their absence does not explain a future rejection. The old parser has not been executed.
| Case | Current codes | Audit obligation |
|---|---|---|
| Unknown pre; unknown checked signature | E0004 each | named opcode/type exception candidate |
| Unknown old/post | E0004, E0004 | independently classify both registered names |
| Old snapshot used in ordinary body | E0004, E5001(%old) | still invalid under the proposal; a transition to snapshot_escape changes the code list |
| Pre plus unrelated undefined operand | E0004, E5001(%missing) | recognizing pre cannot erase the independent SSA defect |
| Old body escape plus unrelated undefined operand | E0004, E5001(%old), E5001(%missing) | identical codes conceal different causes; retain value identities and positions |
| Checked make/take, returned take result | E0004, E0004, E5001(%r) | ordinary use of a newly defined result is not the forbidden snapshot escape |
| New requires header | E0001 | outside the named-opcode/type exception |
| Ordinary undefined operand | E5001 | no extension-related cause |
| Unknown contracts feature | E0006 / unknown_feature | current header stop, not evidence for future construct validation |
The checked make/take example is intended to become valid with the proposed feature and pure predicate; the old body escape is intended to remain invalid. Neither result is implemented or claimed by these baselines. Comparing only acceptance, the first code or aggregate code frequencies misses the rejection-compatibility question.
Spec 07 §8.7 requires preservation of accepted behavior/canonical form and rejected codes, with an exception for names formerly rejected E0004/E0006. It does not explicitly decide whether the exception includes dependent undefined-result diagnostics in a mixed list. A possible **owner policy**, not adopted here, would restrict that exception to the newly recognized construct and facts causally dependent on it, preserving unrelated invalid operands. It would need an explicit normative rule and a complete parser/stage audit, not a blanket waiver whenever E0004 occurs. Alternatively, an RFC can introduce AIR1 while retaining AIR0 unchanged. Header E0001 is a distinct issue; these opcode probes cannot legitimize it.
## Remaining adoption and delivery gates
The source rejection map and runtime schemas are now concrete candidates. Owner decisions remain: rooted caller-owned state versus another module-invariant abstraction; Q-055's version/compatibility policy; and adoption of the RFC 0039/0040 representations. They are not resolved by continued probe collection. Once chosen, integrate complete normative/reference rules, register features/codes/reasons, and deliver verifier/interpreter/frontend/formatter/report/Quick Reference changes with positive and negative AIR/source fixtures, GC and schedule coverage, contract-free compatibility checks and measured bundle growth. Full P6-6 still includes module/type claims and all four statuses. RFC 0039 remains Draft.