Skip to content

Facade: void * destroyers accept object pointers; destroys without drop is an error - #1279

Merged
ehartford merged 2 commits into
mainfrom
facade-void-destroyer-and-drop-required
Sep 22, 2026
Merged

ehartford merged 2 commits into
mainfrom
facade-void-destroyer-and-drop-required

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Two modeled-C facade rulings (Eric, 2026-09-22), one commit each.

Ruling 1 — void * destroyer accepts every object pointer representation

Ruling §61 "the destroyer accepts the representation" is C's own conversion rule: a destroyer parameter of type void * (*mut c_void / *const c_void after c_import) accepts *mut T, *const T and typedef'd pointers chased through their aliases; it does not accept a function-pointer representation (C does not convert one to void *) or a by-value representation. Exact type otherwise.

  • One helper, facade_void_ptr_accepts (src/SemaFacade.w), read by facade_accepts_repr (drop, destroys, init, preinit) and by the in-place-shape check in verify_facade_destroyer (a void * destroyer is no longer mistaken for inflateEnd(z_stream *)).
  • destroyed_by now verifies the same rule: the callable's first parameter takes the consumed parameter. New diagnostic:
    fn 'db_register': destroyed_by param 2: extern "C" fn(i32) -> Unit destroy does not take the consumed param 1: *mut c_void app as its first parameter (§16.2b.9, §16.2b.13)
  • FacadeRender casts the representation to a void-pointer destroyer's spelling (self.repr as *mut c_void): With converts *const T to *mut c_void only explicitly.
  • Rejections keep the existing wording: resource 'Hook': 'release' does not take the representation as its first parameter (§16.2b.13).

Tests: behav_c_facade_void_destroyer.w (counting void* / const void* destroyers over *mut db, *const db, typedef db* dbp, plus a destroys with a void* first parameter — each object counts its destruction: exactly one), err_c_facade_void_destroyer_fn_pointer.w, err_c_facade_void_destroyer_by_value.w, err_c_facade_destroyed_by_wrong_param.w; err_c_facade_destroyer_wrong_repr.w still fails as before.

Ruling 2 — destroys operations with no drop is a compile error

A value dropped while live would leak silently (never half-model unsafely). Reported at the resource:

  • exactly one unary destroyer:
    resource 'Database' has 'destroys' operations but no 'drop' — never half-model unsafely: a value dropped while live would leak silently (§16.2b.3)
    = help: name the unary destroyer as the drop operation: 'drop db_close'
  • several unary destroyers: same error, help name one unary destroyer as the drop operation: 'drop a', 'drop b'
  • none unary:
    resource 'Database' has 'destroys' operations but no 'drop'; every destroyer takes further arguments, so name a 'drop' operation or model the representation differently — never half-model unsafely: a value dropped while live would leak silently (§16.2b.3)

The renderer emits nothing for a resource without drop (Sema names it). Higher RAII / must-consume linear resources are not implemented (future ruling).

Tests: err_c_facade_destroys_without_drop.w (asserts the help line), err_c_facade_destroys_without_drop_no_unary.w, test/phase/c_facade_destroys_with_drop.w (the drop-named form compiles).

Verification

with check src/main.w per commit; build :dev; on out/bootstrap/bin/with-stage1: all behav_c_facade_*, err_c_facade_*, c_facade_* tests (49 files) green plus the guards err_c_import_destroy_heuristic_no_owning_wrapper.w and err_c_import_uncurated_ptr_return_raw.w. err_c_import_raw_call_requires_unsafe.w is red on stage1 only through #1273 (<stdlib.h> mistranslation, div_t by value); the installed compiler passes it and a spelled-prototype twin (void free(void*); free(null)) is rejected by stage1 with the expected error. Battery not run.

Eric Hartford added 2 commits September 22, 2026 08:40
…n (ruling §61 under C's conversion rule)

A destroyer parameter of type void * (*mut c_void / *const c_void after
c_import) accepts *mut T, *const T and typedef'd pointers chased through
their aliases; never a function pointer (C does not convert one to
void *) and never a by-value representation. Exact type otherwise. The
rule is one helper, facade_void_ptr_accepts, read by facade_accepts_repr
(drop, destroys, init, preinit) and by the in-place-shape check in
verify_facade_destroyer, and destroyed_by now verifies the same rule
for the callable's first parameter against the consumed parameter. The
renderer casts the representation to a void-pointer destroyer's
parameter spelling, since With converts *const T to *mut c_void only
explicitly.

Tests: behav_c_facade_void_destroyer (counting void*/const void*
destroyers over *mut, *const and typedef'd reprs, plus a destroys),
err_c_facade_void_destroyer_fn_pointer, err_c_facade_void_destroyer_by_value,
err_c_facade_destroyed_by_wrong_param; err_c_facade_destroyer_wrong_repr
still fails.
…never half-model unsafely)

A value of such a resource dropped while live would leak silently, so
Sema reports it at the resource (ruling, Eric 2026-09-22). When exactly
one destroys operation is unary the diagnostic adds a help line naming
it as 'drop <op>'; when several are, it lists them; when none is, it says
every destroyer takes further arguments and a drop must be named or the
representation modeled differently. The renderer emits nothing for a
resource without a drop, as for one without a destruction contract.
Must-consume linear resources are a future ruling and are not modeled.

Tests: err_c_facade_destroys_without_drop (the help line),
err_c_facade_destroys_without_drop_no_unary, and
c_facade_destroys_with_drop (the drop-named form compiles).
@ehartford
ehartford merged commit 07f4f5f into main Sep 22, 2026
5 checks passed
@ehartford
ehartford deleted the facade-void-destroyer-and-drop-required branch September 22, 2026 13:05
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery on 7fa31d0c (seed-driven): build, :fixpoint, :test, :test-green, :last-green — all rc=0.

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