Summary
In compiled mode, iterating an async generator with for await...of throws at runtime:
System.InvalidCastException: Unable to cast object of type '<ag>d__0' to type 'System.Collections.IEnumerable'.
at <>c__AsyncArrow_0.MoveNext()
The compiled for await lowering casts the async-generator state machine to IEnumerable (the sync-iterable path) instead of driving the async-iterator (next() → Promise) protocol. The interpreter handles the same source correctly.
Repro
async function* ag() { yield 1; yield 2; }
(async () => { for await (const v of ag()) console.log(v); })();
- Interpreter: prints
1 then 2.
- Compiled:
InvalidCastException as above.
Affects async generator declarations (shown) and, now that they parse, async generator expressions (const ag = async function* () { ... }) too. Reproduced inside a compiled async arrow IIFE; likely affects any compiled async context.
Found while implementing #635 (async function expressions). The async-generator-declaration form is pre-existing and independent of that change.
Summary
In compiled mode, iterating an async generator with
for await...ofthrows at runtime:The compiled
for awaitlowering casts the async-generator state machine toIEnumerable(the sync-iterable path) instead of driving the async-iterator (next()→Promise) protocol. The interpreter handles the same source correctly.Repro
1then2.InvalidCastExceptionas above.Affects async generator declarations (shown) and, now that they parse, async generator expressions (
const ag = async function* () { ... }) too. Reproduced inside a compiled async arrow IIFE; likely affects any compiled async context.Found while implementing #635 (async function expressions). The async-generator-declaration form is pre-existing and independent of that change.