Skip to content

Sema: an unannotated async fn's calls are Task[T]; typed MIR rejects a scalar field projection and a non-Task fiber operand (#1464) - #1554

Merged
ehartford merged 1 commit into
fix-1409-1408-field-join-viewfrom
fix-1464-task-handle-type
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-1409-1408-field-join-viewfrom
fix-1464-task-handle-type

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Draft: layer 6 of 7 in the ownership/leaks stack (batch battery pending; iterate tier + :drop-audit/:move-audit on the layer's stage1 done locally). Closes #1464.

Audits (tools/drop_audit.w + tools/move_audit.w, candidate = this layer's stage1, baseline = the pinned seed): drop-audit 197 cells, 0 non-PASS, 0 regressions; move-audit 105 cells, 0 FAIL. The 130 async fixtures give the base's verdicts file for file.

Sema: an unannotated async fn's calls are Task[T]; typed MIR rejects a scalar field projection and a non-Task fiber operand (#1464)

§14.2/§14.4: "Calling it spawns a lightweight thread and returns a
Task[T] handle immediately."

Root cause (exact line): SemaCheck.check_fn_body_with_sig_at, the D43
branch for a body without a return annotation,
self.set_sig_return_type(sig_idx, inferred_ret), stored the inferred T
bare. An annotated async fn's signature is wrapped at declaration
(SemaDecl: sig_ret_type = self.fn_signature_return_type(flags, …)), so
only unannotated async fns had calls typed as the awaited value: the #1464
program's _1: ty3 (i32) held the task, lower_single_await's task_ty was
i32, and copy _7.f0 read the fiber id out of an i32 local. The trait-
contract branch beside it stored a contract's bare T the same way. Both now
go through fn_signature_return_type(flags, …).

Evidence (origin/main ba2d5d1 stage1 -> fixed): --dump-mir of main
_1: ty3 -> _1: ty1772 (Task[i32]); run --debug-alloc leak count=1 ->
0 (await_task_owns_result asked for the local's value drop, and an i32
local has none, so the result buffer was never freed).

Matrix (unannotated async fn unless noted; base -> fixed, all leak 0
fixed): let t = f(5); t.await 10+leak -> 10; f(21).await 42;
tuple await with a str task 2 n2; struct result 7 xy; str result; a
Task[i32] parameter: "wrong argument type" -> 16; Vec[Task[i32]]
push + await: "wrong argument type" -> 6; .cancel() + was_cancelled:
compiler abort "BUG: user generic call lacks a concrete contract" -> false;
detached call statement ok; async method s.get().await: "await requires a
Task value" -> 5; nested async awaits 20; select await ok (base leaked 1);
let x: i32 = f(5): compiled and printed the fiber id (1024) -> "type
mismatch in binding"; annotated controls unchanged. --validate-all: ok
on every compiling cell. The 130 async fixtures (behavior async/await/
task, spec §14, debug_alloc async, compile_errors) give the same verdict
as the base, file for file; behav_fn_abi_async.w (analyze audit:all on an
inferred async fn number(n: i64): n + 1 for five targets) passes with
the fixed compiler as its stage2.

The validators passed the mistyped MIR (a tool that stayed silent). Typed
MIR validation now rejects (a) a field or tuple projection whose base is a
scalar — the place's declared type hid .f0 of an i32 — and (b) a
FIBER_AWAIT/FIBER_CANCEL operand that is not a Task or ScopedTask handle
(MirModule gains the two symbols). Unit tests build both shapes by hand
(src/MirValidationTests.w, test/internals/mir_scalar_field_projection_
test.w).

Fixtures: behav_async_inferred_task_type.w (exact stdout; base fails to
build), err_async_inferred_call_is_task.w, da_async_inferred_task_await_
owns.w (base leak count=1). Drop audit gains async_await_{inferred,
annotated} (an async fn returning R; the inferred cell dies with no output
on the base).

@ehartford
ehartford added this pull request to stack #1556 September 23, 2026 19:42
…a scalar field projection and a non-Task fiber operand (#1464)

§14.2/§14.4: "Calling it spawns a lightweight thread and returns a
`Task[T]` handle immediately."

Root cause (exact line): SemaCheck.check_fn_body_with_sig_at, the D43
branch for a body without a return annotation,
`self.set_sig_return_type(sig_idx, inferred_ret)`, stored the inferred T
bare. An annotated async fn's signature is wrapped at declaration
(SemaDecl: `sig_ret_type = self.fn_signature_return_type(flags, …)`), so
only unannotated async fns had calls typed as the awaited value: the #1464
program's `_1: ty3` (i32) held the task, lower_single_await's task_ty was
i32, and `copy _7.f0` read the fiber id out of an i32 local. The trait-
contract branch beside it stored a contract's bare T the same way. Both now
go through fn_signature_return_type(flags, …).

Evidence (origin/main ba2d5d1 stage1 -> fixed): `--dump-mir` of main
`_1: ty3` -> `_1: ty1772` (Task[i32]); `run --debug-alloc` leak count=1 ->
0 (await_task_owns_result asked for the local's value drop, and an i32
local has none, so the result buffer was never freed).

Matrix (unannotated async fn unless noted; base -> fixed, all leak 0
fixed): `let t = f(5); t.await` 10+leak -> 10; `f(21).await` 42;
tuple await with a str task 2 n2; struct result 7 xy; str result; a
`Task[i32]` parameter: "wrong argument type" -> 16; `Vec[Task[i32]]`
push + await: "wrong argument type" -> 6; `.cancel()` + was_cancelled:
compiler abort "BUG: user generic call lacks a concrete contract" -> false;
detached call statement ok; async method `s.get().await`: "await requires a
Task value" -> 5; nested async awaits 20; select await ok (base leaked 1);
`let x: i32 = f(5)`: compiled and printed the fiber id (1024) -> "type
mismatch in binding"; annotated controls unchanged. `--validate-all: ok`
on every compiling cell. The 130 async fixtures (behavior async/await/
task, spec §14, debug_alloc async, compile_errors) give the same verdict
as the base, file for file; behav_fn_abi_async.w (analyze audit:all on an
inferred `async fn number(n: i64): n + 1` for five targets) passes with
the fixed compiler as its stage2.

The validators passed the mistyped MIR (a tool that stayed silent). Typed
MIR validation now rejects (a) a field or tuple projection whose base is a
scalar — the place's declared type hid `.f0` of an i32 — and (b) a
FIBER_AWAIT/FIBER_CANCEL operand that is not a Task or ScopedTask handle
(MirModule gains the two symbols). Unit tests build both shapes by hand
(src/MirValidationTests.w, test/internals/mir_scalar_field_projection_
test.w).

Fixtures: behav_async_inferred_task_type.w (exact stdout; base fails to
build), err_async_inferred_call_is_task.w, da_async_inferred_task_await_
owns.w (base leak count=1). Drop audit gains async_await_{inferred,
annotated} (an async fn returning R; the inferred cell dies with no output
on the base).

CCodegen has no fiber runtime: an async fn is emitted as a synchronous C function returning the awaited T, and a call of it yields that T. Every signature-return read in src/CCodegen.w now goes through c_sig_return_type (unwrap_task_type over sig_return_type); before, only annotated async fns carried Task[T] and none reached the C backend, so the prelude's unannotated `async fn task_cancel_point(): ()` typed Task[Unit] surfaced as `Task_Unit_ task_cancel_point__727()` — a C type the backend never defines (:test's emit-c-smoke red on the stack battery).
@ehartford
ehartford force-pushed the fix-1464-task-handle-type branch from ccd7b1a to 1afcf49 Compare September 23, 2026 21:39
@ehartford

Copy link
Copy Markdown
Contributor Author

Amended (rebased onto 02301f8, head 1afcf49): the stack battery's emit-c-smoke red — Task_Unit_ task_cancel_point__727() — was this layer's. Root cause in src/CCodegen.w: every signature-return read (self.sema.sig_return_type, 25 sites: decl/def emitters, call-return typing, the return terminator) took the Sema signature type verbatim; the C backend has no fiber runtime and emits an async fn as a synchronous C function, so a Task[T] there names a C type it never defines. Before #1464 only annotated async fns carried Task[T] and none reached the C backend; the prelude's unannotated async fn task_cancel_point(): () did. Fix: c_sig_return_type (unwrap_task_type over sig_return_type) at every site. Verified on the release build of the amended stack: :emit-c-smoke EMIT-C SMOKE OK, :cli-selfhost-edge-tests rc=0; the 130 async fixtures on the LLVM backend give the base's verdicts file for file (behav_fn_abi_async passes with a stage2 present); under --emit-c the async fixtures fail on the base and here alike (C backend cannot resolve call callee — the C backend has never supported async), none regresses from clean to failing.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 22:32
@ehartford
ehartford merged commit c15fdda into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1464-task-handle-type branch September 24, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MIR types a Task handle local as the awaited T; lower_single_await reads .f0 of an i32/i64-typed local (validator silent)

1 participant