Skip to content

CoreFoundation's reference-counted type family: model it as a D51 convention profile #357

Done

Description

@ehartford

Restated (2026-10-25 triage)

The per-constructor owns:/borrows: annotations this issue was built on are superseded by D51's facade language (resources with drop F, retains, convention profiles; §16.2b) and D121 (facades drafted from evidence). What remains is still real: CoreFoundation's reference-counted type family — every +1 object from any Create/Copy function must reach CFRelease, and CFRetain adds a reference across many concrete types. D51 names the mechanism ("A trusted convention profile may make that interpretation for an API family", ruling §39/§59; spec §16.2b.12): a CoreFoundation convention profile. None exists: no profile, facade or registry fact in src/, lib/ or build/ names CFRetain or CFRelease; CoreFoundation appears only in the link-flag tests (link_framework_args_test.w, the :no-host-toolchain stub).

Measured against §16.2b.12 (convention profiles) and §16.2b.15 (facade provenance).


Original report

Summary

The unsafe half of this work is complete: C resource names never cause
automatic cleanup, and generated cleanup is expressed through proven-ownership
wrappers with Drop. The annotation and curated-ownership paths have also
shipped. The one remaining increment is reference-counted type-family
modeling
.

Current state (revalidated 2026-07-15)

Completed:

  • 1373e45 removed heuristic scope-local auto-defer.
  • ed5f0b3 added regressions proving a create/destroy name pair neither creates
    an owning wrapper nor runs cleanup at scope exit.
  • Curated owning wrappers, safe borrowing wrappers, and explicit
    owns:/borrows: import annotations are implemented and tested.
  • Cleanup is Drop on the owning wrapper; raw C functions remain available and
    no MirLower defer is synthesized.
  • 16a25bc added link: "framework:CoreFoundation", so the intended
    CoreFoundation pilot is now testable end to end.

The remaining gap is broader than per-constructor owns: metadata.
CoreFoundation release functions apply to a type family: CFRetain accepts a
CFTypeRef from many concrete constructors and every +1 reference must later
call CFRelease. The current per-constructor wrapper tables do not model that
shared family contract.

There is also a traceability mismatch: docs/requirements.md marks
16.2.2.23 complete even though family-level retain/release modeling is still
open. That checkbox must either be re-scoped explicitly to the shipped
per-constructor path or reopened until this issue lands.

Remaining implementation

  1. Add a curated ownership-family table mapping compatible C reference types to
    their release operation.
  2. Generate one shared owning reference wrapper per family (for example a
    CRef-style CoreFoundation wrapper), rather than one incompatible wrapper per
    constructor.
  3. Make +1 constructors, copy/create functions, and CFRetain produce an owned
    wrapper whose Drop invokes the family release function exactly once.
  4. Keep borrowing accessors non-owning; they must not acquire Drop merely
    because their C type belongs to the family.
  5. Preserve the raw surface and the rule that names alone never establish
    ownership.

Acceptance criteria

  • A real CoreFoundation constructor returns an owned family wrapper and drops
    through CFRelease exactly once.
  • CFRetain creates an independently owned +1 reference; moving or dropping
    either wrapper cannot double-release the other.
  • A borrowing accessor produces no-Drop access.
  • Name-only retain/release candidates still produce no owning wrapper.
  • The requirements entry for 16.2.2.23 accurately describes the implemented
    scope.
  • Full build, fixpoint, and test gates pass.

Traceability

  • Specification §16.2a, Proven ownership cleanup
  • docs/requirements.md 16.2.2.11 through 16.2.2.27

Activity

  1. ehartford commented on Jun 9, 2026

    @ehartford
    ContributorAuthor

    Partial implementation landed in 1373e45. The unsafe name-heuristic auto-defer path is removed, and docs/completed/c-import-auto-methods.md now treats destructor-name matches as suggestions only. This closes the unsound cleanup behavior, but the proven-ownership generated Drop wrapper surface is not implemented yet, so this issue should stay open for that remaining positive feature work. Verification for the landed safety slice: with build, with build :fixpoint, with build :test, and with build :test-green.

  2. ehartford commented on Jun 17, 2026

    @ehartford
    ContributorAuthor

    Added regression tests pinning the removal half (commit ed5f0b3):

    • test/compile_errors/err_c_import_destroy_heuristic_no_owning_wrapper.w — a widget_create/widget_destroy pair imports with a raw surface; the constructor call requires unsafe (no auto-generated safe owning constructor from the *_destroy name heuristic).
    • test/behavior/behav_c_import_destroy_heuristic_no_auto_defer.w — a created handle leaving scope does not auto-call widget_destroy (observed via caller-owned storage); explicit cleanup still works.

    This closes the "no regression tests pinning the removal" gap noted in the issue body.

    Still open (the positive path): ownership-evidence surface (explicit annotation; author/imported metadata; proving header analysis; curated convention) generating owning Drop wrapper types, plus refcount-aware modeling (+1 constructor vs borrowing accessor). That is a substantial new subsystem and remains the bulk of this issue.

  3. added 3 commits that reference this issue on Jun 18, 2026
  4. ehartford commented on Jul 2, 2026

    @ehartford
    ContributorAuthor

    Progress: three of the four planned increments are shipped.

    • Borrow-params (4a77a123): a curated function that borrows an owned C resource gets a safe wrapper taking &COwned_<ctor>, forwarding .handle() internally — pilot: readdir/rewinddir over opendir's DIR. Owned handles now cross back into C with zero raw-pointer exposure.
    • Annotation evidence surface (f2b13386): c_import(..., owns: ["ctor -> dtor"], borrows: ["fn(pi) -> ctor"]) — the §16.3c explicit-annotation source. Outranks the curated tables, participates in the translation cache key. Fixtures annotate un-curated libc pairs (getcwd→free, telldir(0)→opendir).
    • Schema note: the planned record-schema was replaced by extending the codebase's existing accessor-fn table pattern, which the annotation source now feeds too.

    Remaining: refcount modeling. Blocked on a real test vehicle: CoreFoundation needs -framework linking (verified: CF symbols don't link from c_import today) and GLib isn't on the host. The +1-constructor half is already expressible via owns: (e.g. CFStringCreateWithCString -> CFRelease); what's genuinely new is retain functions over type FAMILIES (CFRetain takes any CFTypeRef — per-constructor tables don't fit). Design sketch: a curated family table (family → release fn) with a shared CRef_<family> wrapper type; retain returns a second owned reference. Deferred until framework/library linking gives it a testable pilot.

  5. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Framework-linking prerequisite shipped (16a25bc0): c_import(..., link: "framework:CoreFoundation") now emits -framework CoreFoundation (Link.w link_stage_lib_args; loud error on non-macOS; plain libs unchanged). Verified end-to-end — a modeled c_import calling CFStringGetTypeID() links and runs. This unblocks the CoreFoundation refcount-modeling pilot: CFRetain/CFRelease/CFStringCreate* now LINK (previously the CF symbols didn't resolve at all).

    Remaining #357 work (refcount modeling, the last increment): with CF now linkable, the pilot can proceed. Current raw-path state: CFRetain/CFRelease are RAW (require unsafe) because CFTypeRef isn't modeled. The design (from earlier scoping): a curated type-FAMILY table (any CFTypeRef → CFRelease) with a shared CRef_<family> owning wrapper where +1 constructors (CFStringCreate*, CFRetain) return an owned ref whose Drop calls CFRelease, and borrowing accessors (CFGetRetainCount etc.) don't. This differs from the per-constructor Box/Rc wrappers already shipped because the release fn is shared across a whole type family. Greenfield; its own cycle.

  6. changed the title [-]Replace heuristic c_import auto-defer with proven-ownership Drop wrappers[/-] [+]CoreFoundation's reference-counted type family: model it as a D51 convention profile[/+] on Oct 25, 2026
  7. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): still open. No convention profile, facade clause or registry fact models CoreFoundation's retain/release family:

    $ grep -rn "CFRelease\|CFRetain" src lib build
    (no matches)
    

    post-release: a modeled-C campaign item (a profile for an API family, D51 §59), and nothing in the standard library or the release UATs imports CoreFoundation objects.

  8. added this to the post-v0.16.0 milestone on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions