Skip to content

Modeled C stage 12: the SQLite facade against the real header (ruling §66) - #1619

Merged
ehartford merged 2 commits into
modeled-c-stage13from
modeled-c-stage12
Sep 24, 2026
Merged

ehartford merged 2 commits into
modeled-c-stage13from
modeled-c-stage12

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

D51 stage 12 — the gate for every example rewrite (§66: "The SQLite facade must compile and typecheck before … are rewritten against the new surface"). lib/facades/sqlite3.w is written against the real sqlite3.h and behav_sqlite_facade_end_to_end.w runs every §66 item against the real library with no unsafe. Nothing under build/release_uat_fixtures/, examples/ or docs is touched; those rewrites are the next PR.

§66 item by item

Item Modeled as
sqlite3 owned pointer resource resource Database wraps *mut sqlite3
sqlite3_open out-parameter production from sqlite3_open(out param 1)
SQLITE_OK ok SQLITE_OK → Result[Database, DatabaseError]
failed-open resource production DatabaseError.FailedWithResource(status, FailedDatabase); closed when the error drops (doc sentence quoted in the facade)
sqlite3_close / sqlite3_close_v2 drop / destroys (db.close_v2() consuming)
sqlite3_stmt dependent child resource Statement … borrows param 0
sqlite3_prepare_v2 / sqlite3_finalize from …(out param 3) / drop
borrowed text from sqlite3_errmsg returns borrow CStr from param 0 → Option[CStr] view of the connection
nullable borrowed text from sqlite3_column_text same; SQL NULL is None (fixture asserts both)
view invalidation across statement mutation step/reset/column_int state nothing (invalidate); preserves param 0 on column_count, column_type, column_bytes with the sqlite3.h sentences quoted; pinned by err_sqlite_facade_column_text_after_step.w
callback API with userdata sqlite3_exec: callback param 2 userdata param 3
consume with destroy callback sqlite3_create_function_v2: consumes param 4 destroyed_by param 8 (da_sqlite_facade_destroy_once.w: xDestroy runs exactly once on every path)
retained callback lifetime retains param 5/6/7 by param 0; capturing closures refused (err_sqlite_facade_*_captures.w)
thread capabilities thread creator on both, with the reason send/share are not granted (the mode is a build/open-time choice the header does not prove)
method presentation Database.open, db.exec, db.errmsg(), db.prepare, stmt.step(), stmt.column_text() …
explicit presentation override rename prepare (plus rename on the statement ops, see #1610)

Left raw (findings, filed)

What the real header exposed, fixed here

*const u8 C strings; a facade's rendered surface is pub (so the facade is its own module) and a c_import type in a pub signature is not private; null as a method / generic-fn argument takes its parameter's type; a statement produced through db.prepare(…) escaped its database's scope (the receiver method's signature was not found when the dependency summary was applied — err_c_facade_child_outlives_parent_via_receiver.w); N as a C parameter name. Also filed: #1609 (bare fn → extern "C" fn through a generic parameter; the fixture uses a typed local), #1610 (library-prefix presentation), #1617 (pre-existing static-fn-through-instance codegen failure, found while probing).

Derived decision

Facade location: lib/facades/sqlite3.w — project source space per §16.2b.1/§18.5 (toolchain publishes no facade beyond libc's; c.sqlite3 is Conan and ships none), reachable from every fixture as use facades.sqlite3 through the lib/ module root; not lib/std, which the compiler embeds.

Verification

with build :dev green; with-stage1 check on the facade and every fixture; the new behavior fixture runs against the system sqlite3 and matches; compile_errors sweep over all err_c_facade_* + new: 121/121 + 5/5; run --debug-alloc on the new lane fixture: leak count=0. No :fixpoint/:test/audits (battery by the coordinator). Under this facade build/release_uat_fixtures/sqlite3_main.w needs 0 of its current 11 unsafe (every op it uses is modeled; its exec(…, null, null, null) becomes a no-op callback until #1618).

Eric Hartford added 2 commits September 24, 2026 09:35
… §66)

lib/facades/sqlite3.w: the first complete facade written under the
ruling, against the sqlite3.h c_import resolves on the host, covering
every §66 item: Database over *mut sqlite3 (sqlite3_open out param 1,
ok SQLITE_OK, drop sqlite3_close, destroys sqlite3_close_v2, thread
creator), Statement as a dependent child (sqlite3_prepare_v2 out param
3, borrows param 0, drop sqlite3_finalize), errmsg and column_text as
borrowed text (Option[CStr]), step/reset invalidating column views with
`preserves` where sqlite3.h guarantees it (each sentence quoted),
sqlite3_exec's callback with typed userdata, sqlite3_create_function_v2
consuming pApp destroyed_by xDestroy and retaining xFunc/xStep/xFinal,
presentation (Database.open, db.exec, stmt.column_text) with `rename
prepare` as the explicit override. Location derived from spec §16.2b.1
and §18.5: project source space under lib/facades, not lib/std (the
toolchain publishes no facade beyond libc's); it moves into c.sqlite3
when the package can carry it.

What the real header exposed in the facade language, fixed here:
- `returns borrow CStr` refused `const unsigned char *` (*const u8),
  sqlite3_column_text's spelling of the same bytes (SemaFacade).
- A facade's rendered surface is now `pub` (FacadeRender), so a facade
  module exports its resources; a c_import type in a pub signature is
  not the module's private type (SemaDecl module_private_type).
- A `null` argument takes its pointer type from the parameter through
  methods and generic fns as through plain calls (SemaCheck
  null_arg_expected_type): `db.prepare(sql, -1, null)`.
- A statement produced through the parent's presented method
  (`db.prepare`) escaped its database's scope: the receiver method's
  signature is found by qualified text (SemaFacade facade_producer_sigs).
- A C parameter named `N` rendered as a pattern; facade parameter names
  are escaped as the c_import wrappers escape them (p_N).
…stroy-once under the debug allocator

behav_sqlite_facade_end_to_end.w runs every §66 item against the real
library with no unsafe; err_sqlite_facade_* pin the column view used
after step, the statement outliving its database, and capturing
closures as exec's and create_function_v2's callbacks;
err_c_facade_child_outlives_parent_via_receiver.w pins the receiver
form the dependency summary missed; da_sqlite_facade_destroy_once.w
witnesses xDestroy running exactly once on every path.
@ehartford
ehartford changed the base branch from main to modeled-c-stage13 September 24, 2026 13:35
@ehartford
ehartford added this pull request to stack #1616 September 24, 2026 13:35
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery green — stack #1616 (main → #1614 stage 10 → #1615 stage 13 → #1619 stage 12), top 5295cc0 on main 2c7ed44, pinned seed, cold c_import cache, audits included:

build        rc=0
:fixpoint    rc=0
:drop-audit  rc=0
:move-audit  rc=0
:test        rc=0   (no red targets; contract-view-tests, runtime-domain-audit, the SQLite fixtures against the system libsqlite3 included)
:test-green  rc=0
:last-green  rc=0

Also in this stack (stage 13 layer): the getenv facade fix (C11 7.22.4.6p4) and the analyze fix — a semantic-snapshot request over a failed compilation is never ok.

Ready to merge: gh stack merge 1619 --squash.

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