Skip to content

Codegen: an async fn's value spawns its task, as a direct call does (§24.1) - #2748

Merged
ehartford merged 1 commit into
fix-2279from
fix-async-value
Oct 26, 2026
Merged

ehartford merged 1 commit into
fix-2279from
fix-async-value

Conversation

@ehartford

@ehartford ehartford commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

async fn quick(i: i32) -> i32 is fn quick(i: i32) -> Task[i32]
(§24.1), and Sema types its value so. Codegen made the value from the
symbol quick, which declare_async_function binds to the body (returning
the unwrapped i32; a direct call spawns it). So apply(quick, 3) and
ids |> map(quick) ran the body synchronously and handed its i32 where a
Task was expected: .await read through it and segfaulted (EXC_BAD_ACCESS
at address 0x3 in main, lldb). §14.11.1's own example,
ids |> map(fetch_user) |> await_all, crashed this way; found while
fixing #981.

Root cause: CodegenDispatch's CK_FN constant (a function named as a value)
always built gen_fn_abi_thunk's call to the function, never the spawn a
direct call emits (emit_async_fn_spawn).

The fix: the CK_FN branch reads Sema's fact (task_fns, the same one a
direct call reads) and, for an async fn, gen_fn_abi_thunk returns the Task
from emit_async_fn_spawn_task_value instead of calling the body. A
function value whose async-ness disagrees with its FnAbi convention
(FN_ABI_ASYNC) is now a loud BUG; audit:resolution passed over this
program, as its MIR types were right and codegen chose the callee.

Test: behav_async_fn_value_spawns (through a fn parameter and a map
over a list; segfaulted, now 30 | 60). The 69 other behavior fixtures
on async, await, tasks, fibers, spawn and function values pass on stage1.

…§24.1)

`async fn quick(i: i32) -> i32` is `fn quick(i: i32) -> Task[i32]`
(§24.1), and Sema types its value so. Codegen made the value from the
symbol `quick`, which declare_async_function binds to the body (returning
the unwrapped i32; a direct call spawns it). So `apply(quick, 3)` and
`ids |> map(quick)` ran the body synchronously and handed its i32 where a
Task was expected: `.await` read through it and segfaulted (EXC_BAD_ACCESS
at address 0x3 in main, lldb). §14.11.1's own example,
`ids |> map(fetch_user) |> await_all`, crashed this way; found while
fixing #981.

Root cause: CodegenDispatch's CK_FN constant (a function named as a value)
always built gen_fn_abi_thunk's call to the function, never the spawn a
direct call emits (emit_async_fn_spawn).

The fix: the CK_FN branch reads Sema's fact (`task_fns`, the same one a
direct call reads) and, for an async fn, gen_fn_abi_thunk returns the Task
from emit_async_fn_spawn_task_value instead of calling the body. A
function value whose async-ness disagrees with its FnAbi convention
(FN_ABI_ASYNC) is now a loud BUG; audit:resolution passed over this
program, as its MIR types were right and codegen chose the callee.

Test: behav_async_fn_value_spawns (through a fn parameter and a `map`
over a list; segfaulted, now `30 | 60`). The 69 other behavior fixtures
on async, await, tasks, fibers, spawn and function values pass on stage1.
@ehartford
ehartford added this pull request to stack #2749 October 26, 2026 07:19
@ehartford ehartford changed the title fix async value Codegen: an async fn's value spawns its task, as a direct call does (§24.1) Oct 26, 2026
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on the stack top 41f0547 (with run tools/battery.w in fix-s2b, pinned seed nightly-20261025-local-1-c88f3eeff0bc, seed stamp dc299893… verified): battery rc=0 wall=292s; the gate, uat (13 scenarios: 12 pass, 1 skip) and battery-checks are all green in the one survey; behavior-tests 1953 files ok. Each layer also ran src/main build :gate green under the pinned seed (WITH=$PWD/src/main). The first battery run was red only on last-green-record: stage1 had been seeded by ~/.local/bin/with from gate runs without WITH set, and a build-store entry then served an installed-generation LLVM bridge into the seed bootstrap-lib; the redo ran seed-driven with WITH_BUILD_CACHE_DIR=none after clearing out/lib, out/bootstrap-lib and out/tmp/with_runtime.

@ehartford
ehartford marked this pull request as ready for review October 26, 2026 08:24
@ehartford
ehartford merged commit dee141b into main Oct 26, 2026
@ehartford
ehartford deleted the fix-async-value branch October 26, 2026 08:24
ehartford added a commit that referenced this pull request Oct 27, 2026
… D164) (#2751)

`with_concurrency(tasks, n)` documented "at most n tasks at a time" and
its body was `let _ = n; tasks` (lib/std/task.w:227-230); the call did
not even compile ("return type mismatch" inside task.w). No
task-taking signature can mean what the doc said: a Task is already
running (§24.1). Measured on the task-taking draft, `ids |> map(f)`
started all 6 tasks under limits 2 and 1, and the lazy adapter spelling
does not type-check into `impl IntoIter[Task[T]]`.

`with_concurrency[I, T](inputs: impl IntoIter[I], n: isize, f: fn(I) -> Task[T]) -> List[T]`
runs `f` over the inputs, calling `f(input)` — which spawns (#2748) —
only when a slot frees, and returns results in input order. `n < 1` is
the same loud refusal `await_first` gives empty input. Docs follow:
lib/std/async/with_concurrency.md, lib/std/async/README.md, and the
migration guide's `ids |> map(fetch_user) |> with_concurrency(10) |> await_all`
becomes `ids |> with_concurrency(10, fetch_user)`.

Test behav_981_with_concurrency_bounds_running_tasks (fails pre-fix:
"expects 2 argument(s), found 3"): squares of [6..1] in input order,
peak 2 running under limit 2, peak 1 under limit 1, empty input empty;
debug allocator leak count=0. Delegated under D98 as D164 (80%).

Ceremony found, not fixed here: an unannotated list literal through an
ordinary generic call binds its isize default before a later fn
argument's demand (`map` takes the demand; `with_concurrency` does
not), so the fixture annotates `List[i32]`.

Co-authored-by: Eric Hartford <[email protected]>
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.

1 participant