Skip to content

Make ephemerality provenance-tracked and modular #362

Closed

Description

@ehartford

Make ephemerality provenance-tracked and modular

Summary

The spec now states that ephemerality is not purely structural. Type-level ephemerality is structural, but value-level ephemerality is provenance-tracked: returned-origin sets, binding-level task ephemerality, closure/callable capture provenance, assignment/call propagation, returned-view checking, and escape checks require deterministic compiler analysis. This issue tracks completing that implementation substrate. The goal is not Rust-style user lifetime annotations — the compiler carries the facts so the user does not have to.

Spec

  • §5 Ephemeral Types (docs/with-specification.md:2223+): type-level ephemerality (references, views, declared-ephemeral types) and §5.2 propagation through aggregates and generic containers.
  • §14.22 (docs/with-specification.md:7390-7397): "Ephemerality is a per-binding property, not a per-type property. The type Task[i32] is the same whether ephemeral or storable. The compiler determines ephemerality at the creation site by analyzing the arguments... This marking propagates through assignments and function calls."
  • §21.1 Rule 6 / §22.x (returned views must originate from parameters/receiver/static, not locals).
  • §22.1 Rule 8 (returned ephemeral values: caller's binding inherits ephemerality).
  • Diagnostics contract (docs/completed/spec-feedback.md:380-390): every ephemeral/origin rejection must name (a) where the value was borrowed/created, (b) where it escapes, (c) the idiomatic fix.
  • docs/requirements.md 14.22.1.9 ("no effect-summary propagation" — verdict: partial) and 8.3.2.10 ("arena raw ptrs untracked") are filed against this issue.

Current state

Root cause / 5 Whys (from triage)

  1. Why was the old requirement wrong? It said no dataflow was required and that ephemerality was determined structurally by types.
  2. Why is that unsound? Returned references/views, task handles, closures, and callable values need to know which origin a value came from, not merely what shape its type has.
  3. Why can't type shape answer that? Task[T] has one spelling whether ephemeral or non-ephemeral, and a returned &T may originate from a parameter, receiver, capture, static storage, or local temporary.
  4. Why do we need summaries? The checker must remain modular across function, closure, function-pointer, trait-object, wrapper, and call boundaries without user-written lifetime syntax.
  5. Root cause: With needs a unified deterministic value-provenance summary/dataflow substrate. The implementation has partial machinery, but the spec-level model is broader than the current ad hoc checks.

Partial machinery (all references re-verified)

  • src/Sema.w: signature returned-view origin masks sig_param_view_origins (:268), declared-ephemeral type registry ephemeral_types (:346), task-fn registry task_fns (:358), binding-level task ephemerality bind_is_ephemeral_task (:403, with scope_lookup_is_ephemeral_task/scope_set_is_ephemeral_task :2624-2633), closure capture summaries closure_capture_summary_starts/counts/data (:515-517, writer set_closure_capture_summary :2774).
  • src/SemaCheck.w: returned/yielded view origin checks check_returned_view_origins (:4864), record_call_view_origins (:4931), check_return (:4985); ephemeral task tracking expr_creates_ephemeral_task (:2309), expr_is_ephemeral_task (:2514), check_ephemeral_task_arg_escape (:2537), check_ephemeral_task_storage (:2549); structural type-level check type_is_ephemeral_value (:12830); escaping-closure-captures-ephemeral rejection + capture summary recording.
  • Existing compile-error fixtures: err_return_local_ref_direct.w, err_return_local_ref_via_call.w, err_return_local_ref_binding.w, err_escaping_bound_closure_captures_ref.w, err_thread_spawn_closure_captures_ref.w, and the err_ephemeral_task_*storage/escape family.

Verified gaps

  • By-value ephemeral-Task passes to ordinary functions only warn (src/SemaCheck.w:2547) — Make unproven ephemeral task escape a hard error #355.
  • §5.2 generic-container propagation is not enforced: let xs: Vec[E] = Vec.new() with E declared ephemeral passes check today (verified against out/bin/with-stage2).
  • No callee effect summaries exist beyond sig_param_view_origins and may_suspend; calls erase provenance (14.22.1.9 verdict: "no effect-summary propagation").
  • Returned-view origin tracking per §21.1 Rule 6 is incomplete — tracked separately as Implement returned-view origin tracking (§21.1 Rule 6) with the §22.3 diagnostic contract #378; NLL is last-use AST analysis with documented uncovered cases (src/BorrowCfg.w:22-29).
  • Arena/raw-pointer provenance untracked (8.3.2.10).

Required design shape (normative for the implementation)

  • Preserve structural type-level ephemerality for references, declared-ephemeral types, and aggregates/generic containers with ephemeral components.
  • Add or complete value-level provenance facts for bindings, returned-origin sets, task captures, closure captures, assignment propagation, call propagation, returned views, and escape checks.
  • Task[T] keeps one spelling. A task value is ephemeral when its captures/result depend on an ephemeral origin; this is inferred and propagated.
  • Closures and callable values carry summaries: captures, origin sets, ephemerality, and may_suspend facts across closure, function pointer, trait object, generated-wrapper, and indirect-call boundaries.
  • The analysis is modular: intra-procedural dataflow plus inferred summaries at interfaces.
  • The analysis is deterministic and conservative. No unordered map iteration or pointer-address ordering may affect verdicts (fixpoint invariant). If safety cannot be proven, reject.
  • False rejection of actually-safe code is compiler precision debt, not a reason to add user lifetime/ephemerality annotations.

Implementation plan

Phased; each phase independently passes the full gate.

  1. Unify the fact store. Introduce a single per-binding provenance record in Sema (origin set: ParamN / Receiver / Static / Local(i) / CaptureOf(closure) / Heap-owned; ephemerality bit; task bit) replacing the parallel ad hoc vectors (bind_is_ephemeral_task, binding view deps, expr view deps) with one keyed structure. Keep existing query names as thin wrappers so call sites don't churn in the same commit.
  2. Assignment/binding propagation. On NK_LET_DECL/NK_LET_BINDING/NK_ASSIGN, propagate the RHS provenance record to the binding (today only the task bit propagates). Covers "origin propagation through local bindings and assignments".
  3. Call-boundary summaries. Per-function inferred summary: for each param, does it escape (store/return/send/escaping-capture)? for the return value, which param/static origins can it carry? Extend sig_param_view_origins into this shape; compute memoized + cycle-conservative like fn_symbol_may_suspend (src/SemaCheck.w:2058). Make unproven ephemeral task escape a hard error #355 lands the Task-param-consumed slice of this; Implement returned-view origin tracking (§21.1 Rule 6) with the §22.3 diagnostic contract #378 lands the returned-view slice.
  4. Closure/callable summaries. Extend closure_capture_summary_* to carry origin sets and ephemerality per capture, and make the summary survive coercion to fn(...) values, trait objects, and generated wrappers (the indirect-call check then consults the summary attached to the callable's type/value instead of giving up). Includes implicit-context captures (Implement implicit parameters and with name(expr): contexts (§7.3a, §9.1a) #398 step 3).
  5. §5.2 container propagation. Enforce ephemerality of F[T] when T is ephemeral at instantiation sites (annotation, construction, field decl). Today only direct struct-field storage is rejected ("ephemeral type 'X' cannot be stored in non-ephemeral struct"); Vec[E] must be rejected the same way. May require the TY_GENERIC_INST work in Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391 to know T reliably.
  6. Diagnostics. Adopt the three-location contract for every rejection in this family: value creation site ("borrows X here" / "ephemeral task created here"), escape site, idiomatic fix (clone/collect/await-in-scope/keep-in-scope). The §15.6 view-liveness diagnostic (src/SemaCheck.w:11797-11809) is the in-tree precedent to copy.
  7. Determinism audit. All new maps iterated for verdicts must be index-keyed Vecs or sorted; add no pointer-address ordering. with build :fixpoint is the gate.

Test plan

Mirror the acceptance criteria; files under test/compile_errors/ and test/behavior/:

  • Returned local ref/view escapes (direct, via binding, via call) — extend the existing err_return_local_ref_* family with: origin through two assignments, origin through a helper call that returns its param (summary must propagate, not reject blindly when origin is a caller param).
  • Safe returns: param-origin and static-origin views returned and used — //! expect-stdout behavior tests (must NOT be rejected; precision tests).
  • Ephemeral task propagation: through binding, reassignment, storage (existing), send/detach, callback argument, and return-with-inherited-ephemerality (Rule 8) — error fixtures for each escape, behavior tests for in-scope use.
  • Closure/callable summaries: function-pointer coercion of a capturing closure, trait-object callable, wrapper — escaping ones rejected with the capture named; non-escaping accepted.
  • §5.2 container propagation: Vec[E], Option[E] field in non-ephemeral struct, generic Wrap[E] — rejected; same types used as locals/params — accepted.
  • Diagnostic-shape assertions: at least one fixture per category asserting the three-location output via //! expect-stderr fragments.

Acceptance criteria

  • Tests cover direct returned local reference/view escapes and safe parameter/static-origin returns.
  • Tests cover origin propagation through local bindings, assignments, and function calls.
  • Tests cover ephemeral task capture propagation through binding, assignment, storage, send/detach, callback, and return paths.
  • Tests cover closure/callable summary propagation, including function pointers and trait-object/dyn callables.
  • Diagnostics name the escaping value and the origin/provenance fact that could not be proven safe (three-location contract).
  • Verdicts are deterministic; with build :fixpoint passes after every phase.
  • with build, with build :fixpoint, with build :test, and with build :test-green pass.

Dependencies

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Progress landed in be40a23: introduced a single per-binding BindingProvenance record for value/task ephemerality and view origin slices, propagated value-level ephemerality through let/var bindings and assignment, and added focused coverage for local Vec[ephemeral], nested generic field rejection, and ephemeral Task assignment propagation. Verification passed: with build, with build :fixpoint, with build :test, and with build :test-green. Keeping this umbrella issue open until the named Phase 4 consumers #378 and #355 land on top of this substrate.

  2. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Completed across the Phase 4 substrate and consumers. Substrate landed in be40a23 (unified binding provenance and propagation); returned-view origin tracking landed in 90171ad (#378); unproven ephemeral Task escape hard errors landed in 6a2881f (#355). Full verification passed for the final consumer: with build, with build :fixpoint, with build :test, and with build :test-green.

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