Skip to content

Reject call-scoped str temporary for retained C-string params (§16.3c) #602

Closed

Description

@ehartford

Summary

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.

Plan

  • Add a retention marker to c_import/extern param contracts (e.g. derived from the curated overlay Curated libc contract overlay for c_import modeled surfaces #379, or an explicit attribute).
  • 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.

Activity

  1. ehartford commented on Jul 4, 2026

    @ehartford
    ContributorAuthor

    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.

  2. ehartford commented on Jul 5, 2026

    @ehartford
    ContributorAuthor

    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:

    1. 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.
    2. 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 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 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)

    • Sema retained_extern_params: HashMap[i32,i32] (name_sym → retained-param bitmask) + mark_param_retained / param_is_retained, curated seed hook.
    • Parser retains: ["fn(idx)"] c_import clause (parallel to owns:/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.
    • 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.

  3. ehartford commented on Jul 5, 2026

    @ehartford
    ContributorAuthor

    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.

  4. added a commit that references this issue on Jul 5, 2026
    92ceea1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions