Skip to content

Modeled C D64: buffer pairing and fixed arguments; zlib and bzip2 facades; their UAT fixtures at zero unsafe (#1621, #1624) - #1645

Merged
ehartford merged 8 commits into
mainfrom
modeled-c-d64-buffers
Sep 24, 2026
Merged

ehartford merged 8 commits into
mainfrom
modeled-c-d64-buffers

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Implements D64 (spec §16.2b.8 "Buffers", §16.2b.11 "Fixed arguments"; #1621, #1624) on top of the modeled-C stack (base: modeled-c-uat-rewrite).

Compiler

  • Parser: buffer param P len param L, buffer param P capacity param L inout, param N fixed <literal> (capacity without inout and len … inout are parse errors; a fixed literal is an int, null, true/false).
  • Sema (SemaFacade.w): P points at bytes (char/unsigned char/void/typedefs), L is an integer (inout: a mutable pointer to one), distinct, paired once, never also fixed; a fixed literal is a value of the C type (null↔pointer, int↔int, no negative into unsigned, never a resource-receiving param); ok CONST on an fn item is the status contract of a copied-back length (refused without an inout pairing; an inout pairing on a status-returning fn is refused without ok; returns … + inout refused); generated <Fn>Error name clash check. The refusal: 12b's verify_facade_buffer_params (byte/scalar buffers) now skips paired/fixed parameters and its help names the D64 clauses; the D64 pass refuses a record handle no resource wraps under a presentation clause. A paired/fixed parameter never makes the raw C call safe (facade_covers_param).
  • Render (FacadeRender.w): one []u8/[]mut u8 per pairing; the bridge is exactly Eric's: capacity = dest.len; C(dest.ptr, &capacity, src.ptr, src.len); check capacity <= dest.len; capacity as usize — presented as usize (void C fn) or Result[usize, <Fn>Error] (ok), on success only; the caller's slice is untouched; a too-large length is a panic naming the operation and the length. Hosted lends keep their &self method; a free operation renders as __with_facade_<name> (or its rename) and a call to the C name is redirected to it wherever the rendering is visible (SemaCheck.check_call → facade_bridge_redirect, MirLower.lower_call), so the raw declaration stays what C declared (the bridge's own body calls it; a module importing the header without the facade keeps the raw op).
  • WITH_DUMP_FACADE=1 prints the rendered facade text (a <facade NAME>:line:col diagnostic points into it).
  • Two gaps D64 needed, fixed: Vec/array → []T/[]mut T coercion in method calls ([]mut T stages 2–3: local bindings, returns, and split_at_mut #604 had only free calls); s as []u8 validates in MIR (Sema accepted it, the validator refused).

Facades

  • lib/facades/sqlite3.w: sqlite3_prepare_v2 param nByte fixed -1 / param pzTail fixed null; sqlite3_exec param errmsg fixed null. Resulting spellings: db.prepare(sql), db.exec(sql, None, None), db.exec(sql, Some(cb), Some(&ctx)).
  • lib/facades/zlib.w (compress/uncompress as buffer pairs under ok Z_OK, zlibVersion static text) and lib/facades/bzip2.w (BZ2_bzBuffToBuffCompress/Decompress under ok BZ_OK), each clause citing the manual.

UAT (ruling §66)

zlib_main.w 3 unsafe → 0, bzip2_main.w 3 → 0, same output; the release UAT copies the facade into the fresh project as src/facades/<lib>.w (mirrors #1625). :user-programs-safe now lists only libcurl_main.w (8). libcurl needs: curl_easy_init/cleanup as a resource (from/drop), curl_easy_setopt (variadic — not modelable), curl_version_info returning a struct pointer whose fields are read (a borrowed record view — not ruled).

Fixtures

5 behavior (input pair, inout+ok+rename+void, bounds panic, fixed int/null free+hosted, hosted lend pairs), 2 roundtrip twins of the UAT programs, behav_method_slice_coercion, 22 compile_errors. Facade sweep on this stage1: 202 pass; the 4 z_init fixtures (production_local_names, resource_in_place_{arming,drop_once,pinned}) fail identically on the stack head (Stream.z_init now presents as Stream.init) — not this layer.

Verification

with check src/main.w ok; :dev green; :spec-inventory-check green; analyze audit:all on the D64 fixtures: 0 violations. No :fixpoint/:test.

Filed

#1632 (v[..] on a Vec lowers to Unit), #1633 (str.as_bytes() missing; s as []u8 is the route), #1634 (stage1 from main rejects src/main.w:2991), comment on #1137 (== on []u8 compares by address). Pre-existing: #1485 (memchr), the *_warns fixtures need the pre_d_build runner.

Derived decisions

  • A free operation presented under its C name is reached by call-site redirection to __with_facade_<name>, not by shadowing (a facade pub fn compress would hijack a header's inline helpers that call compress, and a translated inline C function has no link symbol to alias).
  • The raw operation stays available under the C name only outside modules where the facade's rendering is visible; rename gives a distinct presented name.
  • ok on an fn item is accepted only with a capacity … inout pairing (nothing else is ruled to present on success).
  • A buffer pairing on a producer/destroyer/init/callback item is refused (their renderings have no slice); a fixed argument on a producer or callback item is the constructor's/method's literal.

@ehartford
ehartford added this pull request to stack #1646 September 24, 2026 17:30
@ehartford
ehartford force-pushed the modeled-c-d64-buffers branch from 0a12cb2 to c682c9e Compare September 24, 2026 17:30
@ehartford
ehartford force-pushed the modeled-c-d64-buffers branch from c682c9e to 0345089 Compare September 24, 2026 17:32
@ehartford
ehartford marked this pull request as ready for review September 24, 2026 18:50
Base automatically changed from modeled-c-uat-rewrite to main September 24, 2026 18:51
Eric Hartford added 8 commits September 24, 2026 14:51
…ut]`) and fixed arguments (`param N fixed <literal>`); the unpaired-pointer refusal; Vec/array coerce to slices in method calls (#1621, #1624)

Spec §16.2b.8 "Buffers" and §16.2b.11 "Fixed arguments" (D64). Parser: the
two clauses, `capacity` without `inout` and `len … inout` refused. Sema: the
pointer must point at bytes, the length be an integer (inout: a mutable
pointer to one), both distinct and paired once; a fixed literal must be a
value of the C parameter's type; `ok CONST` on an fn item is the status
contract a copied-back length is presented under (refused without an inout
pairing; an inout pairing on a status-returning fn is refused without it);
the ruling's refusal — a raw pointer parameter no clause pairs on a
lend/presented item names the parameter and the clauses to add; a paired
or fixed parameter never makes the raw call safe. Render: one `[]u8` /
`[]mut u8` per pairing, the bridge `capacity = dest.len; C(dest.ptr,
&capacity, …); check capacity <= dest.len; capacity as usize`, presented
as `usize` (void) or `Result[usize, <Fn>Error]` (ok), the caller's slice
untouched, a too-large length a panic naming the operation and the length.
A hosted lend renders as before with the bridge; a free operation renders
as `__with_facade_<name>` (or its `rename`) and a call to the C name is
redirected to it where the rendering is visible (SemaCheck/MirLower), so
the raw declaration stays what C declared and the bridge's own body calls
it. Method calls gain the #604 Vec/array → []T/[]mut T coercion the free
call path had (the hosted `n.read(buf)` needed it).
…lice; Sema accepted the cast and the validator refused it (D64: a str is the bytes a buffer pairing takes)
…lite3_exec fixes errmsg null (D64 §16.2b.11); db.prepare(sql) and db.exec(sql, cb, ud) in the fixtures
…/uncompress and BZ2_bzBuffToBuffCompress/Decompress as buffer pairs under ok Z_OK / ok BZ_OK, zlibVersion static text; roundtrip fixtures at zero unsafe
…e 3 -> 0 and 3 -> 0, same output; the UAT copies lib/facades/<lib>.w into the fresh project as src/facades/<lib>.w (ruling §66)
…s a resource's own operations and callback contracts to their renderings; a producer's fixed argument is the constructor's literal (not raw-beyond, not forwarded by the receiver method); WITH_DUMP_FACADE=1 prints the rendered text

err_c_facade_uncovered_return_stays_raw now lends `db_count` on a `Db`
resource: under D64 a `lend` on a bare `db *` no resource wraps is the
refusal (err_c_facade_lend_unpaired_buffer), so the stage-3 fixture keeps
its point (an undescribed pointer return stays raw) through a resource.
…rameters and names the D64 clauses; the D64 refusal covers the handle no resource wraps; `nullable` returns after collecting (the merge had dropped it)

12b's verify_facade_buffer_params names a caller-owned byte or scalar
buffer (its fixture err_c_facade_lend_unpaired_buffer keeps that text);
the D64 pass refuses the record handle no resource wraps under a
presentation clause (err_c_facade_rename_unpaired_buffer). One parameter,
one diagnostic.
…so it applies to c_import translations only — the runtime's own extern seams carry stage 13's domain rows (rt_libc_write, rt_libc_mmap, …) and render nothing
@ehartford
ehartford force-pushed the modeled-c-d64-buffers branch from b3e2e0f to b50b5e4 Compare September 24, 2026 18:51
@ehartford
ehartford merged commit 67f9449 into main Sep 24, 2026
@ehartford
ehartford deleted the modeled-c-d64-buffers branch September 24, 2026 18:51
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