Repository navigation
Description
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 escapeCode (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)
- Why does unsafe code compile?
Sema.check_ephemeral_task_arg_escapeemits a warning for by-value ephemeralTaskarguments to non-extern functions. - Why a warning? The old spec said ambiguous ephemeral escapes should warn rather than block.
- 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.
- 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.
- 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:
- 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 pathcheck_ephemeral_task_storagecovers (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: newSema.task_param_consumed_summary(fn_sym, param_idx)in src/SemaCheck.w next to the existingfn_symbol_may_suspendmemoized walker (:2058), which is the structural template for this kind of cross-function AST summary. - 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); otherwiseemit_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". - Keep the extern path unchanged (already an error).
unsafeescape hatch: per §14.22/expected behavior, an explicitunsafeblock around the call suppresses the error (matching how other unprovable-safety assertions work). Verify how existingunsafe-context suppression is modeled in Sema and gate the new error the same way.- Sweep for in-repo violations:
grep -rnfor 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). - Warning policy: after this change no ephemeral-escape path may emit a warning. Audit
emit_warningcalls 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 takesTask[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.awaitcalled 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_referenceearly-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 :testpass (andwith build :test-greenrecorded).
Dependencies
- Make ephemerality provenance-tracked and modular #362 (ephemerality provenance substrate) — generalizes the summary machinery; this issue lands the narrow Task-by-value disposition first. 14.22.1.9 is tracked under Make ephemerality provenance-tracked and modular #362.
- Implement position-based Task disposition semantics #350 (position-based Task disposition semantics) and Enforce Task sendability conditions (§14.7, §14.22) #480 (Task sendability conditions) touch adjacent §14.22 rules; keep diagnostics and summaries shared.
Activity
ehartford commentedon Jun 12, 2026
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.