Skip to content

Modeled C: retained variadic callback pairs — abandon, setter status contract, pair setters, MIR pair-state flow (D66, #1652) - #1679

Merged
ehartford merged 8 commits into
mainfrom
modeled-c-retained-variadic
Sep 26, 2026
Merged

ehartford merged 8 commits into
mainfrom
modeled-c-retained-variadic

Conversation

@ehartford

@ehartford ehartford commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Implements Eric's 2026-09-25 ruling on retained variadic callback pairs (D66 amendment, spec §16.2b.5 and §16.2b.9, landed in #1688) — #1652.

What lands

  • abandon <op> on a resource: the safe abandonment path. Sema verifies it is the resource's own operation, takes only the representation, and is itself callbacks none; the rendered Drop runs it before the destroyer on every drop path (a spurious reset costs one call, a missed one is unsafe).
  • ok CONST on a variadic operation: the status contract of its listed cases, presentation unchanged; MIR reads a comparison of the returned code against it (through a binding or a copy) as the setter's success/failure edge.
  • A callback case case CONST: callback param N as T userdata param CONST2 retains by param 0 renders mut fn <base>__<CONST>[U](…, value: <T with its void * as &U>), and the paired userdata setter mut fn <base>__<CONST2>[U](…, value: &U) is implied by the pairing (no second case line). Retention is stated, never inferred: a callback case without retains by is refused. A callback-only setter binds U from the callback's own signature; a named fn coerces to the extern "C" type (§12.4).
  • Sema registers every operation of a resource with a pair under the concrete signature MIR records on the call (FacadePairOp): setters per specialization with their canonical U (a closure literal's per-literal fn type is interned structurally), the abandon op as the reset, destroyers, and every other operation as callback-capable unless callbacks none — including the other cases of the same setter, so two-call setup requires callbacks none on the setter (§16.2b.9).
  • MirForeignPairs.w: after lowering, before codegen, the ForeignPairState.w place flow runs over each body's real CFG — setters and their status edges, borrows (including projections through a reference local), moves, resource births at call results, drops, returns/stores as escapes, and unmodeled helper calls (checked as callback-capable, then unknown until a reset). Violations are diagnostics in the body's own file: an operation that may invoke an unproven pair; a retained userdata that dies or moves while held (reported at the setter that retained it); a retaining resource returned or stored. WITH_DUMP_PAIR_FLOW=1 prints every fact the flow ran on.
  • lib/facades/libcurl.w: abandon curl_easy_reset, callbacks none and ok CURLE_OK on curl_easy_setopt, callbacks none on curl_easy_reset (bounded to the modeled option set; the MIME freefunc path is documented), and the CURLOPT_WRITEFUNCTION/CURLOPT_WRITEDATA case. The release UAT program transfers a local file through the modeled pair (no network, no unsafe), under the D66 amendment (§66: the facade first, then the validation artifact).

Evidence (branch stage1, this worktree)

  • test/behavior/behav_libcurl_facade_callback_pair.w against the real library: written: true / fetched: true / ok, --debug-alloc leak count 0.
  • test/behavior/behav_libcurl_facade_callback_pair_failure.w — the ruling's required acceptance case with the real variadic ABI: first setter succeeds, second fails (test facade pairs the callback with CURLOPT_LASTENTRY), the function returns an error, Drop resets then cleans up: first: true / second: false / configured: false / ok, leak count 0.
  • test/behavior/behav_c_facade_variadic_callback_pair_check.w (check-only): either setup order, a reset between runs, a helper handed a proven pair, a sink read between the setter and the run, early returns after a first successful setter — accepted.
  • Compile-error fixtures: incomplete pair, unhandled setter failure, userdata dying while held, a retaining resource returned, a helper leaving the pair unknown, abandon naming a callback-capable op, a callback case without retention, the unlisted-option refusal moved to CURLOPT_READDATA (the pairing now implies CURLOPT_WRITEDATA).
  • Existing facade fixtures (behav_libcurl_facade_setopt, behav_c_facade_retained_callback, _callback_scope, _free_callback, _variadic_free, _callbacks_none) unchanged and passing.

Not modeled (refused conservatively, documented in docs/modeled-c-pair-state-plan.md): interprocedural pair summaries for helpers; closure literals as the callback (pass a named fn); the retained pointer case (CURLOPT_POSTFIELDS); reading a chunk's bytes inside a write callback (its char * stays raw until a callback buffer clause is ruled — counts are safe today).

Status: draft until the pinned-seed battery is green and posted here.

@ehartford
ehartford added this pull request to stack #1676 September 25, 2026 15:42
@ehartford ehartford changed the title modeled c retained variadic Modeled C: retained variadic callbacks and resource state (#1652, in progress) Sep 25, 2026
@ehartford
ehartford force-pushed the modeled-c-retained-variadic branch from a57c64f to 278e0da Compare September 25, 2026 17:34
@ehartford
ehartford removed this pull request from stack #1676 September 25, 2026 17:38
@ehartford
ehartford force-pushed the modeled-c-retained-variadic branch from 278e0da to aa7eba4 Compare September 25, 2026 17:38
@ehartford
ehartford added this pull request to stack #1682 September 25, 2026 17:39
@ehartford
ehartford removed this pull request from stack #1682 September 25, 2026 17:41
@ehartford
ehartford changed the base branch from modeled-c-element-buffers to public-return-foundation September 25, 2026 17:41
@ehartford
ehartford added this pull request to stack #1684 September 25, 2026 17:41
@ehartford
ehartford removed this pull request from stack #1684 September 25, 2026 18:06
@ehartford
ehartford changed the base branch from public-return-foundation to josh/float-round-trip September 25, 2026 18:06
@ehartford
ehartford changed the base branch from josh/float-round-trip to public-return-foundation September 25, 2026 18:06
@ehartford
ehartford added this pull request to stack #1685 September 25, 2026 18:06
@ehartford
ehartford force-pushed the modeled-c-retained-variadic branch 2 times, most recently from abd3ff5 to 4821959 Compare September 25, 2026 19:07
Base automatically changed from public-return-foundation to main September 25, 2026 19:08
@ehartford
ehartford force-pushed the modeled-c-retained-variadic branch from 4821959 to 0dc4b32 Compare September 25, 2026 19:08
Eric Hartford added 4 commits September 25, 2026 17:49
Specification 9.1 and D43 do not exempt public functions from inference.
Remove the unsupported visibility-based parser rejection and replace its
negative test with public value/Unit/method/generic/import regressions.
Keep a mixed-tail rejection test and correct the formatter plan's claim.
The callback-state helpers do not need public method visibility; remove
the redundant Unit annotations added to accommodate the parser error.

Root-cause proof: out/public-return-parser-proof.log, LLDB stopped in
Parser.parse_fn_decl at Parser.w:1318 with is_pub=1 and ret_type=0.
The missing annotation took the unconditional public-only diagnostic.

Validation: pinned-seed source check and :dev pass. The four targeted
public-return tests pass with rebuilt stage1; foreign-pair place tests
also pass with inferred helper returns. Full stack battery is pending.
Eric Hartford added 3 commits September 25, 2026 18:36
…atus contract, pair setters, and the MIR pair-state flow (D66, #1652)

Implements the 2026-09-25 ruling (spec §16.2b.5, §16.2b.9; #1688).

- `abandon <fn>` on a resource: the safe abandonment of partial callback
  setup. Sema verifies it is the resource's own operation, takes only the
  representation and is itself `callbacks none`; the rendered Drop runs it
  before the destroyer on every drop path.
- `ok CONST` on a variadic operation is the status contract of its listed
  cases (presentation unchanged); MIR reads `status == OK` / `!= OK`,
  through a binding or a copy, as the setter's success or failure edge.
- A callback case (`callback param N as T userdata param CONST retains by
  param 0`) renders `mut fn <base>__<CB>[U](…, value: <T with &U>)` and the
  implied userdata setter `mut fn <base>__<UD>[U](…, value: &U)`; retention
  is stated, never inferred (a case without it is refused). A callback-only
  setter binds `U` from the callback's own signature, and a named fn is
  checked against its own signature as a C function pointer (§12.4).
- Sema registers every operation of a resource with a pair under the
  concrete signature MIR records on the call (FacadePairOp): setters per
  specialization with a canonical `U` (a closure literal's per-literal fn
  type is interned structurally), the abandon op as the reset, destroyers,
  and every other operation as callback-capable unless `callbacks none`.
- MirForeignPairs.w runs the ForeignPairState.w place flow over each
  lowered body's CFG: setters and their status edges, borrows (a projection
  through a reference local is that local), moves, resource births at call
  results, drops, escapes into the return place or an aggregate, and
  unmodeled helper calls (checked, then unknown until a reset). Findings
  are reported in the body's own file: an operation that may invoke an
  unproven pair, a retained userdata that dies or moves while held (at the
  setter that retained it), a retaining resource that escapes.
  WITH_DUMP_PAIR_FLOW=1 prints every fact the flow ran on.
- ForeignPairState.w gains the unknown-operation transfer and the escape
  check the adapter needs.

The pass collects findings through a report record rather than moving
them between Vecs with into_iter: #1689 (a returned Vec of a three-str
record yields its last element at every position under into_iter).
…the UAT transfers a file through it (D66, #1652)

`curl_easy_setopt` states `callbacks none` and `ok CURLE_OK`, and lists
`case CURLOPT_WRITEFUNCTION: callback param 2 as curl_write_callback
userdata param CURLOPT_WRITEDATA retains by param 0`; `curl_easy_reset`
is the handle's `abandon` path, `callbacks none` bounded to the modeled
option set (the MIME freefunc path is documented); `curl_easy_cleanup`
stays callback-capable. Every clause quotes its manual page.

The release UAT program follows the spec change (§66; the D66 amendment
of 2026-09-25): it transfers a local file through the modeled pair —
`file://`, no network, no `unsafe` — counting the bytes the callback
receives, with the sink declared before the handle so it outlives it.
…on notes (#1652)

Runtime, against the real library: the pair over a file:// transfer, and
the ruling's required acceptance case (first setter succeeds, second
fails with the real variadic ABI, the function returns an error, Drop
resets then cleans up). Check-only: the accepted shapes. Compile errors:
an incomplete pair, an unhandled setter failure, a userdata dying while
held, a retaining resource returned, a helper leaving the pair unknown,
`abandon` naming a callback-capable operation, a callback case without
retention. The unlisted-option fixture moves to CURLOPT_READDATA, since
the pairing now implies CURLOPT_WRITEDATA.
@ehartford
ehartford force-pushed the modeled-c-retained-variadic branch from 0dc4b32 to 93ddd18 Compare September 25, 2026 22:36
@ehartford ehartford changed the title Modeled C: retained variadic callbacks and resource state (#1652, in progress) Modeled C: retained variadic callback pairs — abandon, setter status contract, pair setters, MIR pair-state flow (D66, #1652) Sep 25, 2026
…#1652)

The contract audit now states each operation's callback capability with
its provenance (`callbacks none`, or the conservative default); the three
contract-view expectations gain that row.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery verdict — green. Driven by the pinned seed nightly-20260925-local-1-607a52f543e7 (src/main digest = seed.lock), cold c_import epoch, on 9a9ebd8 (rebased on main 1e83944):

gate result
build ok
:fixpoint ok
:drop-audit ok
:move-audit ok
:test ok — 1294 behavior, 1142 compile-error, 210 spec, 63 phase, 17 codegen, internals/contract/lexer/parser lanes
:test-green / :last-green ok, published
:user-programs-safe ok

The first run was red on contract-view-tests only (three expectations lacking the new per-operation callbacks: provenance row the contract audit now states, §16.2b.9); the expectations were updated in 9a9ebd8 and the battery re-run once. Logs: ~/.local/with-staging/modeled-c-retained-integration/out/logs/. Ready for merge (alone in its batch: it changes the generated Drop).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant