Skip to content

Compiled for await...of over an async generator throws InvalidCastException (cast to IEnumerable) #680

@nickna

Description

@nickna

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions