Skip to content

Make unproven ephemeral task escape a hard error #355

Closed

Description

@ehartford

Make unproven ephemeral task escape a hard error

Summary

The spec now says possible ephemeral escape is not warning-worthy safe code. Ephemerality is part of With's safety contract: if the compiler cannot prove an ephemeral value's origin outlives every use, accepting the program risks dangling references, cross-thread borrowed values, detached ephemeral tasks, or erased origin information. Today a by-value pass of an ephemeral Task to an ordinary function compiles with a warning; it must become a compile error unless the compiler proves the callee consumes the task in scope (per the §14.22 effect-summary model).

Spec

docs/with-specification.md §14.22 Task Ephemerality and Send (lines 7368-7460); docs/requirements.md 14.22.1.9–14.22.1.13.

Normative sentences:

  • "Clear ephemeral escapes are compile errors. Ambiguous or unproven ephemeral escapes are also compile errors, because ambiguity means the compiler cannot prove safety." (14.22.1.11/.12)
  • "Ephemeral values can be passed to functions — by reference or by value — only when the compiler can prove the value remains within its valid scope, or when the callee's effect summary propagates the ephemerality/origin information to its result." (14.22.1.9)
  • "Warnings are appropriate for weird-but-safe code, performance guidance, style, or suspicious but semantically valid patterns. They are not sufficient when accepting the program could produce a dangling reference, cross-thread borrowed value, detached ephemeral task, or erased origin." (14.22.1.13)

Spec example fixing the required dispositions (docs/with-specification.md:7415-7427):

fn process_task(t: Task[i32]):
    t.await                          // OK: consumes the task
fn store_globally(t: Task[i32]):
    GLOBAL_TASKS.push(t)             // escapes

let task = process(&v)               // ephemeral task
process_task(task)                   // OK: compiler sees task is consumed
store_globally(task)                 // ERROR: cannot prove it stays in scope

Current state

Repro (current output is a warning):

./out/bin/with -e 'async fn work(x: &i32) -> i32: 1
fn take(t: Task[i32]): 0
let x = 1
let t = work(&x)
take(t)'
# warning: ephemeral Task passed by value may escape

Code (verified): Sema.check_ephemeral_task_arg_escape (src/SemaCheck.w:2537-2547):

fn Sema.check_ephemeral_task_arg_escape(self: Sema, arg_node: i32, expected_ty: i32, is_extern_call: i32):
    ...
    if self.param_is_by_reference(expected_ty) != 0:
        return                       // by-ref pass is fine
    if is_extern_call != 0:
        self.emit_error("ephemeral Task cannot be passed by value to extern function", arg_node)
        return
    self.emit_warning("ephemeral Task passed by value may escape", arg_node)

Call sites: src/SemaCheck.w:7726 and :8169 (the latter passes is_extern_call from extern_fn_names). Supporting machinery: expr_is_ephemeral_task (:2514), expr_creates_ephemeral_task (:2309), check_ephemeral_task_storage (:2549, already a hard error), bind_is_ephemeral_task binding state (src/Sema.w:403). Existing hard-error fixtures: err_ephemeral_task_struct_storage.w, err_ephemeral_task_vec_storage.w, err_ephemeral_task_enum_storage.w, err_ephemeral_task_extern_by_value.w, err_ephemeral_task_in_spawn_os_worker.w, err_ephemeral_task_in_extern_callback.w. The by-value-to-ordinary-function case is the one remaining warning.

Root cause / five whys (from triage)

  1. Why does unsafe code compile? Sema.check_ephemeral_task_arg_escape emits a warning for by-value ephemeral Task arguments to non-extern functions.
  2. Why a warning? The old spec said ambiguous ephemeral escapes should warn rather than block.
  3. Why is that model wrong? Ephemerality carries lifetime/origin safety without user-written lifetimes; ambiguity means the compiler cannot prove the origin outlives every use.
  4. Why can't the compiler trust the programmer here? Accepting an unproven escape can create dangling references, detached borrowed tasks, cross-thread borrowed values, or erased origin information in safe code.
  5. Deepest cause: the compiler lacks a proof/effect-based disposition for by-value ephemeral task arguments and treats the unproven case as a warning instead of a safety failure.

Implementation plan

The naive fix (warning → error) would reject the spec's own process_task(task) OK-example, so the change is an error plus a minimal callee proof:

  1. Add a per-function "task param consumed in scope" summary. For each non-extern With function with a by-value Task[...] parameter, compute (on demand, memoized, deterministic) whether the parameter provably does not escape: it is not (a) stored via any path check_ephemeral_task_storage covers (struct/enum/container/global), (b) returned, (c) captured by an escaping closure, (d) sent/detached cross-thread, or (e) passed by value to another function whose own summary is not proven-consuming. Recursion/cycles resolve conservatively to "not proven" (deterministic fixed verdict, no unordered-map iteration — fixpoint hazard rule). Implementation home: new Sema.task_param_consumed_summary(fn_sym, param_idx) in src/SemaCheck.w next to the existing fn_symbol_may_suspend memoized walker (:2058), which is the structural template for this kind of cross-function AST summary.
  2. Flip the disposition in check_ephemeral_task_arg_escape (:2547): if the callee summary proves consumption → accept (no diagnostic at all; do not warn on proven-safe code); otherwise emit_error. Error text per the spec-feedback diagnostics contract (docs/completed/spec-feedback.md:386-389 — name the value, the escape point, and the fix):
    "ephemeral Task may escape: callee '<name>' is not proven to consume it; await it in scope, pass by reference, or make the callee consume it".
  3. Keep the extern path unchanged (already an error).
  4. unsafe escape hatch: per §14.22/expected behavior, an explicit unsafe block around the call suppresses the error (matching how other unprovable-safety assertions work). Verify how existing unsafe-context suppression is modeled in Sema and gate the new error the same way.
  5. Sweep for in-repo violations: grep -rn for by-value Task passes in src/, lib/std/, test/ that currently ride on the warning; fix or annotate them before flipping (the compiler must build itself under the new rule — fixpoint chain will enforce this).
  6. Warning policy: after this change no ephemeral-escape path may emit a warning. Audit emit_warning calls in src/SemaCheck.w for other ephemeral/task cases and confirm none remain in the escape category.

Phase split if needed: (a) land the summary + error for the direct case with the consuming-callee proof; (b) extend proof coverage (transitive by-value passes, closure-callback paths) under #362's substrate.

Test plan

  • test/compile_errors/err_ephemeral_task_by_value_escape.w — //! expect-check-fail: ephemeral Task may escape — exactly the repro above: callee takes Task[i32] and ignores/stores it; the pass must be an error, not a warning.
  • test/compile_errors/err_ephemeral_task_by_value_stored_by_callee.w — callee pushes the task into a Vec[Task[i32]] → error (proves the summary looks inside the callee).
  • test/compile_errors/err_ephemeral_task_by_value_returned_by_callee.w — callee returns its parameter; result stored by the caller → error (or accepted-with-propagated-ephemerality once Make ephemerality provenance-tracked and modular #362's return propagation lands — write as error now, note the upgrade path in the test comment).
  • test/behavior/behav_ephemeral_task_consuming_callee.w — //! expect-stdout: ok — the spec's positive case: fn process_task(t: Task[i32]): t.await called with an ephemeral task → compiles, runs, correct value.
  • test/behavior/behav_ephemeral_task_by_ref.w — by-reference pass of an ephemeral task remains accepted (existing param_is_by_reference early-return), awaited via the reference where supported.
  • Unsafe escape hatch test: same shape as the first error fixture inside unsafe: → compiles (file under test/behavior/ or test/spec/ per how other unsafe-suppression tests are organized).
  • Keep all existing err_ephemeral_task_*.w fixtures green.

Acceptance criteria

  • By-value ephemeral Task to a non-consuming or unproven callee is a compile error; the warning string "ephemeral Task passed by value may escape" no longer exists in the codebase.
  • The spec's consuming-callee example compiles and runs (no diagnostic).
  • Diagnostic names the value, the unprovable fact, and the idiomatic fixes (keep in scope / pass by ref / copy to owned / unsafe).
  • Summary computation is deterministic (no unordered-map iteration; memoized; cycle-conservative).
  • with build && with build :fixpoint && with build :test pass (and with build :test-green recorded).

Dependencies

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Implemented in 6a2881f. By-value ephemeral Task arguments now require proof that the callee consumes the task in scope; unproven ordinary callees are hard errors, extern callees remain hard errors, and explicit unsafe is treated as the ordinary-call escape assertion. Added positive and negative coverage for consuming/forwarding callees, by-ref pass, unsafe assertion, ignored/returned/unproven-forwarded escapes, plus existing extern/container regressions. Verification passed: with build, with build :fixpoint, with build :test, and with build :test-green. Filed follow-up #569 for the separate Task.is_done() parameter lowering bug found during testing.

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