Repository navigation
Modeled C stage 4b: in-place resources (preinit/init, Drop arming) - #1275
Conversation
|
Audits, driven by the pinned seed (
Not run here by design: |
|
Audits after the D54 rework (94fe7b7), driven by the pinned seed (
Not run here by design: |
|
Rebased onto main's tip 02e618e (after #1274, #1279, #1291, #1301); base is now
Will rebase once more onto main after #1306 merges so the battery tree builds. |
94fe7b7 to
2335fe0
Compare
|
Isolation battery on |
…stage 4b)
A resource whose representation is a by-value C struct and whose facade
names `init` renders as `type R { repr: Repr, live: bool }` with a
constructor `R.<init>`: storage is the representation's ordinary zeroed
construction `Repr {}` (every field of an imported struct carries its zero
default; ruling §67 consumes that rule) or the `preinit` operation's result,
then the C initializer over `&raw mut repr`, then the arming bit. With
`ok CONST` a status other than the constant leaves Drop unarmed and the
storage cleans up as ordinary With (ruling §13.2); without `ok` the status
is uninterpreted (§16.2b.4), so the resource is live and the status is
handed back unread. A status-returning init yields `(status, R)`, the
low-level model a projection sits over; a void init yields `R`.
An operation taking a pointer to the representation — Drop, a destroyer, a
by-value token's address-taking destroyer — is handed `&raw mut self.repr`
(`&raw const` for a const pointer); a typedef'd pointer (`z_streamp`) is
chased through its alias so the renderer agrees with Sema's resolve_alias.
Sema: 4a's "in-place not rendered yet" errors go; `init` must take a
pointer to the storage (by value would initialize a copy), `preinit`
returns the representation, `ok` needs a status to read, `from` and `init`
exclude each other, `preinit` needs an `init`, init/preinit must be safe
once covered, and a C parameter name both preinit and init spell is
refused at the resource (it surfaced as a shadowing error at
`<facade NAME>:1:1`). A resource that passed every check without becoming
a struct is reported as a renderer defect rather than left silent.
Tests: Drop exactly once on every path for an in-place resource; preinit
used when named, a failed init under `ok` never runs the destroyer, a
`destroys` op disarms, an `ok`-less status is handed back live; the
prototype-only phase render (three resources over one z_stream, alias
pointer); six resource-shape errors.
…ss-stability question
… facade's claim (D54)
Spec §16.2b.3 / D54: an in-place resource's representation has one address
from the creation of the resource value until foreign destruction
completes. The renderer keeps the value ordinary and movable and puts the
representation in a `Box[Repr]` cell the value owns: `Box.new(Repr {})`
(or of the preinit result) exists — at its one address — before `init`
runs, every pointer handed to C is `self.repr.as_mut_ptr()` (`.as_ptr()`
for a const parameter), and R's own Drop runs the destruction operation
before the field's Drop frees the cell, so the cell outlives the C state.
A raw pointer taken from a borrow of the representation stays valid across
moves of the value. No immovable type category is introduced.
`movable` is a new resource clause (Ast/Parser/FacadeResource): the
facade's claim, with `ok`'s evidence weight, that no operation keeps the
address; it keeps the by-value rendering. It is an error on a resource
with no `init`, and a pinned resource's drop/destroyer taking the
representation by value is an error (it would act on a copy).
The rendering is spliced after resolution, so Resolve makes a facade
block that declares a pinned resource its module's import of std.box —
the D29 gate refused `Box` in the rendered file otherwise, and a `use`
line in rendered text is not an import edge. `Box.as_mut_ptr` joins
`as_ptr` in std.box.
Tests: address stability across rebinding, a Vec and a function argument
with a header whose init keeps the address (misplaced=0, ends=4); raw
pointer across a move; destroy-then-free under the debug allocator
(`da_c_facade_pinned_cell_destroy_then_free.w`, leak count=0); the arming
test's `Stream` is `movable` (by-value path, preinit) and `Blind` pinned;
the phase render covers both; two new clause errors.
|
Isolation battery on |
2335fe0 to
a562abe
Compare
Rebased on main (83b3f1f+, after #1274 stage 4a and #1279 facade void*/drop rulings); no longer stacked.
Stage 4b of
docs/modeled-c-implementation-plan.md(ruling §13–§13.2, §67; spec §16.2b.3–4; D54 / spec §16.2b.3 pinning, PR #1276), perdocs/handoff-modeled-c-stage4.md. Keeps #1279 behaviors:facade_void_ptr_accepts(avoid *destroyer getsself.repr as *mut c_void), the destroys-without-drop error (a resource renders only withdrop).What 4b renders
An in-place resource — a by-value C struct representation with
init(and optionallypreinit,ok,movable). Pinned by default (D54): the representation lives in aBoxcell the value owns; the value stays an ordinary movable With value; every pointer handed to C points into the cell; R's own Drop runs the destroyer and the Box field's Drop frees the cell after. For the phase test'sover
int inflateInit(z_streamp s); int inflateEnd(z_streamp s); int inflateReset(z_stream* s, int windowBits);the emitted<facade zlib>isA
movableresource — the facade's claim that no operation keeps the address, never inferred — renders over a by-value field. For the same test'sthe emitted text is
With
preinit deflate_storagethe storage expression isdeflate_storage(<preinit args>)(insideBox.new(...)when pinned) and the constructor takes preinit's parameters before init's.Design points, each verified by compiling and running:
Repr {}— the ordinary zeroed construction of an imported struct: c_import gives every field its zero default (ci_default_for_type; probed scalar, pointer, nested-struct, array and function-pointer fields), while a user struct without defaults rejectsT {}.T.zeroed()of the storage-types campaign is not implemented anywhere and is not needed; ruling §67 is consumed, nothing defined.preinitreturns the representation and stands in that position.Box[T]is the existing owned-heap-cell idiom (lib/std/box.w);Box.as_mut_ptris added besideas_ptr. The cell exists, at its one address, fromBox.new— beforeinit, so "allocated, not live" already has its stable address. Drop order: R'smove fn drop()body runs the destroyer, then thereprfield's Box Drop frees the cell (da_c_facade_pinned_cell_destroy_then_free.wreads the state through the kept address insidez_end, under the debug allocator:leak count=0, no read of freed memory). A raw pointer froms.repr.as_ptr()taken beforelet b = move areads correctly after the move. No immovable type category.useline in rendered text is not an import edge and the D29 gate refusedBoxin<facade …>. A facade block declaring a pinned resource is now that module's import of std.box (Resolve.w facade_declares_pinned_resource), the way the prelude's ownuse std.boxis text.ok CONST,liveisstatus == CONST: a failed init leaves Drop unarmed; the cell (or by-value storage) cleans up as ordinary With. Withoutokthe status is uninterpreted — "there is no rule that 0 means success" — so the resource is live and the status is handed back unread. A status-returning init yields(status, R), the low-level model everyResult-shaped projection sits over (stage 5).okon a void init is an error.&raw mut self.repr(movable, or a by-value token whose destroyer takes its address — previously a 4a "in-place not rendered" error).z_streamptypedefs are chased through theirtypealias so the renderer agrees with Sema'sresolve_alias. A pinned resource's op taking the representation by value is an error (it would act on a copy).verify_facade_resourcesends with a net: a resource with a drop or destroyer that passed every check and has no struct under its name is reported as a renderer defect.Sema
initmust take a pointer to the storage;preinitreturns the representation;from/initexclude each other;preinitneedsinit;okneeds a status; init/preinit safe once covered (anint*argument needsfn <init> lend, as the tests do); preinit/init parameter-name clash refused at the resource;movablewithoutinitrefused; pinned by-value op refused. 4a's two "not rendered yet" errors are gone;facade_covers_returncovers preinit's return.Tests
behav_c_facade_resource_in_place_pinned.w— address kept byz_init(s->strm = s), checked after the constructor, rebinding,Vec.push, a function argument; raw pointer across a move;ends=4 misplaced=0.behav_c_facade_resource_in_place_drop_once.w— Drop exactly once on scope exit, early return, moved-in, moved-out, in a Vec (pinned).behav_c_facade_resource_in_place_arming.w—Streamismovablewithpreinit/ok/destroys(tag survives, failed init never runs the destroyer,destroysdisarms);Blindis pinned andok-less (status handed back, live).test/debug_alloc/da_c_facade_pinned_cell_destroy_then_free.w— destroy-then-free ordering, cells freed once per path, failed init frees its cell.test/phase/c_facade_in_place_renders.w— prototype-only; pinned + pinned-with-preinit +movableover onez_stream,z_streampalias.err_c_facade_{init_by_value,preinit_wrong_return,ok_without_status,from_and_init,preinit_without_init,preinit_init_param_clash,movable_not_in_place,pinned_by_value_op}.w;err_c_facade_in_place_not_rendered.wremoved.All
behav_c_facade_*/c_facade_*/err_c_facade_*tests, the three guards, and the five mechanism-A tests pass under the fresh stage1 (#1273 still applies toerr_c_import_raw_call_requires_unsafe.won stage1).Audits
:move-audit/:drop-audit/:debug-alloc-tests: verdicts in a comment below.Open points for the maintainer
(status, R)as the constructor's surface withoutok, and withoka dead-but-ownedRon failure (methods callable, Drop unarmed): the low-level model; stage 5 owns the projection.Stream.z_init(0).1is refused ("a vacate needs avarbase") while the same expression as a call argument is accepted — found writing the fixture, worked around with alet (_, s); not a facade defect.ok(5), mechanism A retirement (4c).