Repository navigation
Codegen: an async fn's value spawns its task, as a direct call does (§24.1) - #2748
Conversation
…§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.
|
Battery GREEN on the stack top 41f0547 ( |
… 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]>
async fn quick(i: i32) -> i32isfn 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 (returningthe unwrapped i32; a direct call spawns it). So
apply(quick, 3)andids |> map(quick)ran the body synchronously and handed its i32 where aTask was expected:
.awaitread through it and segfaulted (EXC_BAD_ACCESSat address 0x3 in main, lldb). §14.11.1's own example,
ids |> map(fetch_user) |> await_all, crashed this way; found whilefixing #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 adirect 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
mapover a list; segfaulted, now
30 | 60). The 69 other behavior fixtureson async, await, tasks, fibers, spawn and function values pass on stage1.