You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reject call-scoped str temporary for retained C-string params (§16.3c) #602
A modeled C-string parameter that the C callee retains (stores the pointer past the call) must require caller-managed storage (CString/&CStr) and reject a hidden call-scoped str temporary (§16.3c, docs/with-specification.md:8388-8391). Today the str→*const c_char coercion (CodegenDispatch copy_str_to_cstr_temp) always materializes a call-scoped temporary, with no notion of "retained".
Status
Split out of #427, whose conversion-API surface (String/StrView/CStr/CString, as_view/to_owned/to_cstring/as_cstr) is now implemented. This criterion is deferred because there is no param-retention contract yet: c_import bindings don't carry a "retains pointer" annotation, so the compiler can't tell a retained param from a borrowed one.
At the call site, when a retained *const c_char param is given a str (a call-scoped temporary rather than a CString/&CStr), emit an error directing the caller to to_cstring().
Traceability
16.3.4.x retention rules; depends on #379 (curated libc contract overlay) for libc retention metadata.
BDFL ruling (2026-07-04): default-borrowed + curated retains: + debug-alloc teeth. (1) Default stays borrowed — call-scoped temporaries remain legal, zero c_import/migrate ceremony. (2) The curated overlay (#379/#357 owns:/borrows: family) and a user attribute gain a retains: marker; a retained *const c_char param given a call-scoped str temp is a compile error directing to to_cstring(). (3) Under --debug-alloc, freed call-scoped temps are scribbled so UNMARKED retainers fail loudly in tests (Go-cgocheck analog). Zero release-mode cost.
Architectural finding (2026-07-05): enforcement is currently unreachable
Attempted the full retention contract this cycle (curated overlay + retains: c_import clause + codegen/sema enforcement). The mechanism is straightforward to build, but it has no reachable trigger with the current c_import surface, so it can't be landed with a passing test (and shipping untestable enforcement violates our "done requires evidence" bar). Reverted; recording the finding so the next attempt starts here.
Why unreachable
The str → *const c_char coercion — the thing #602 must reject for a retained param — only happens when the callee's const char* parameter is modeled as cstr_in. That modeling comes exclusively from the hardcoded curated overlay ci_overlay_cstr_in_param_count (SemaDecl.w:502): atoi/atof/getenv/strchr/strcmp/strlen/strstr/.... Two consequences:
Every function in that overlay is genuinely borrowed (transient read of the string); none retains. So there is no existing cstr_in-modeled function to mark retained and test against.
cstr_in modeling is not user-extensible. A user's own use c_import("int f(const char* s)") imports f with a raw surface (callable only under unsafe, and a str does not coerce — verified: "wrong argument type" / "raw c_import function call requires unsafe context"). So a user cannot even construct the Reject call-scoped str temporary for retained C-string params (§16.3c) #602 scenario (a cstr_in-coercible param that retains).
Net: the #602 scenario ("a modeled C-string param that the callee retains") requires a function that is bothcstr_in-modeled and retaining — which does not exist in the overlay and cannot be user-created today.
What a complete fix needs (larger than a bug fix)
retains: must ALSO make the annotated param cstr_in-modeled (so the coercion path is entered and then rejected), i.e. make cstr_in modeling user-extensible via the c_import contract, keyed by the retention annotation. That is a c_import-overlay feature, in the sensitive "never degrade c_import/migrate" area — it deserves a dedicated, focused effort, not the tail of a large session.
Infrastructure that was prototyped (reverted, but the shape is known-good)
Enforcement: at the str→cstr coercion — check-time (SemaCheck.w:12309, where the coercion is decided) is preferred over the codegen fallback (CodegenDispatch mir_eval_call_operand_info).
Debug teeth (WITH_DEBUG_ALLOC_SCRIBBLE / dbg_scribble) are already built and would pair with this once the enforcement is reachable.
Fixed in 92ceea1. retains: ["fn(idx)"] on a c_import param makes it a modeled cstr_in input that rejects a call-scoped str temporary (the caller must pass a pointer into owned storage, e.g. to_cstring()?.as_cstr().ptr()). The earlier 'unreachable' finding became the design: retains: is itself the cstr_in evidence (decisions.md D4), so it's testable with pure user code — no dependency on a real retaining libc function. Fixpoint + full suite green; 3 fixtures. Surfaced a separate Result-of-Drop unwrap double-free → #648.
Activity
ehartford commentedon Jul 4, 2026
BDFL ruling (2026-07-04): default-borrowed + curated
retains:+ debug-alloc teeth. (1) Default stays borrowed — call-scoped temporaries remain legal, zero c_import/migrate ceremony. (2) The curated overlay (#379/#357 owns:/borrows: family) and a user attribute gain aretains:marker; a retained*const c_charparam given a call-scopedstrtemp is a compile error directing toto_cstring(). (3) Under --debug-alloc, freed call-scoped temps are scribbled so UNMARKED retainers fail loudly in tests (Go-cgocheck analog). Zero release-mode cost.docs: record BDFL soundness-tier rulings (2026-07-04)
ehartford commentedon Jul 5, 2026
Architectural finding (2026-07-05): enforcement is currently unreachable
Attempted the full retention contract this cycle (curated overlay +
retains:c_import clause + codegen/sema enforcement). The mechanism is straightforward to build, but it has no reachable trigger with the current c_import surface, so it can't be landed with a passing test (and shipping untestable enforcement violates our "done requires evidence" bar). Reverted; recording the finding so the next attempt starts here.Why unreachable
The
str → *const c_charcoercion — the thing #602 must reject for a retained param — only happens when the callee'sconst char*parameter is modeled ascstr_in. That modeling comes exclusively from the hardcoded curated overlayci_overlay_cstr_in_param_count(SemaDecl.w:502):atoi/atof/getenv/strchr/strcmp/strlen/strstr/.... Two consequences:cstr_in-modeled function to mark retained and test against.cstr_inmodeling is not user-extensible. A user's ownuse c_import("int f(const char* s)")importsfwith a raw surface (callable only underunsafe, and astrdoes not coerce — verified: "wrong argument type" / "raw c_import function call requires unsafe context"). So a user cannot even construct the Reject call-scoped str temporary for retained C-string params (§16.3c) #602 scenario (acstr_in-coercible param that retains).Net: the #602 scenario ("a modeled C-string param that the callee retains") requires a function that is both
cstr_in-modeled and retaining — which does not exist in the overlay and cannot be user-created today.What a complete fix needs (larger than a bug fix)
retains:must ALSO make the annotated paramcstr_in-modeled (so the coercion path is entered and then rejected), i.e. makecstr_inmodeling user-extensible via the c_import contract, keyed by the retention annotation. That is a c_import-overlay feature, in the sensitive "never degradec_import/migrate" area — it deserves a dedicated, focused effort, not the tail of a large session.Infrastructure that was prototyped (reverted, but the shape is known-good)
retained_extern_params: HashMap[i32,i32](name_sym → retained-param bitmask) +mark_param_retained/param_is_retained, curated seed hook.retains: ["fn(idx)"]c_import clause (parallel toowns:/borrows:), serialized after the CoreFoundation's reference-counted type family: model it as a D51 convention profile #357 ownership record; a Sema reader (read_c_import_retentions) parsing the entries into the store.str→cstrcoercion — check-time (SemaCheck.w:12309, where the coercion is decided) is preferred over the codegen fallback (CodegenDispatchmir_eval_call_operand_info).Debug teeth (
WITH_DEBUG_ALLOC_SCRIBBLE/dbg_scribble) are already built and would pair with this once the enforcement is reachable.ehartford commentedon Jul 5, 2026
Fixed in 92ceea1.
retains: ["fn(idx)"]on a c_import param makes it a modeled cstr_in input that rejects a call-scopedstrtemporary (the caller must pass a pointer into owned storage, e.g.to_cstring()?.as_cstr().ptr()). The earlier 'unreachable' finding became the design:retains:is itself the cstr_in evidence (decisions.md D4), so it's testable with pure user code — no dependency on a real retaining libc function. Fixpoint + full suite green; 3 fixtures. Surfaced a separate Result-of-Drop unwrap double-free → #648.Fix #602: retained C-string params (retains:) reject a call-scoped st…