Repository navigation
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
Conversation
…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).
ccd7b1a to
1afcf49
Compare
|
Amended (rebased onto 02301f8, head 1afcf49): the stack battery's emit-c-smoke red — |
Draft: layer 6 of 7 in the ownership/leaks stack (batch battery pending; iterate tier +
:drop-audit/:move-auditon 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 Tbare. An annotated async fn's signature is wrapped at declaration
(SemaDecl:
sig_ret_type = self.fn_signature_return_type(flags, …)), soonly 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 wasi32, and
copy _7.f0read 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-mirof main_1: ty3->_1: ty1772(Task[i32]);run --debug-allocleak 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.await10+leak -> 10;f(21).await42;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 aTask value" -> 5; nested async awaits 20; select await ok (base leaked 1);
let x: i32 = f(5): compiled and printed the fiber id (1024) -> "typemismatch in binding"; annotated controls unchanged.
--validate-all: okon 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 + 1for five targets) passes withthe 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
.f0of an i32 — and (b) aFIBER_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).