Skip to content

for-loop over a generic iterator value has never worked: element type binds the unsubstituted param (invalid LLVM) or falls back to i32 #912

Closed

Description

@ehartford

Repro (plain user file, no stdlib involvement)

type Holder2[T] { vec: Vec[T] }
impl[T] Holder2[T]:
    pub mut fn next() -> Option[T]: self.vec.pop()

fn main:
    var xs: Vec[i64] = Vec.new()
    xs.push(41 as i64)
    var h = Holder2 { vec: xs }
    for v in h:
        print(f"got {v}")

→ LLVM function verification failed after MIR cleanup for main (invalid LLVM dumped). The identical NON-generic iterator (Option[i64] return) for-drives correctly, and driving the generic one manually with match h.next(): works — only the for-desugar is broken.

Root cause

infer_for_element_type (src/SemaCheck.w:22904): the generic Iter-protocol tail looks up the next sig and extracts arg0 of its Option[...] return — but for a generic iterable that stored sig returns Option[<type-param>], and the code extracts the unsubstituted placeholder (user-file case → loop var typed as the param → invalid LLVM at codegen) or, when the sig lookup misses across module-interned base syms (embedded-std case), hits the silent ty_i32 fallback (loop var silently typed i32 → 'wrong argument type' errors downstream).

substitute_method_return_for_generic_inst (src/SemaCheck.w:19447) already does exactly the needed substitution (inst args over the base's params, with an AST-node fallback) — the for-infer path just never calls it.

Also worth a look while here: the self.ty_i32 as i32 terminal fallback is a silent wrong-type default — when no element type can be inferred the loop should get a diagnostic, not i32.

Found while implementing D33 (#724): for task in tasks.into_iter(): hit the i32-fallback arm.

Activity

  1. ehartford commented on Aug 31, 2026

    @ehartford
    ContributorAuthor

    Scope note: lower_comprehension_generic_iter (src/MirLower.w:6885) is a second copy of the same desugar with the same defect — a comprehension over a generic iterator value resolves the template sym, codegen finds no function, invalid LLVM. The for-loop fix (specialization demand registered in check_for keyed by the for node + dispatch of the recorded mono sym in the desugar) covers only the for path; the comprehension path needs the equivalent demand in comprehension checking. Left on the old behavior for now.

  2. ehartford commented on Aug 31, 2026

    @ehartford
    ContributorAuthor

    Fixed across a239713 (for-loops) and 9880aa5 (comprehensions).

    For-loops: infer_for_element_type substitutes next()'s return under the inst (substitute_method_return_for_generic_inst, whose AST fallback now also consults generic_fn_nodes); check_for registers the specialization via check_generic_method_call; the desugar dispatches the recorded mono through the GENERIC_CALL contract. Two new-ground drop bugs fixed with existing idioms: Option payload taken by MOVE (the ? idiom) and the loop binding in a per-iteration scope (#614b) — pre-D33 no by-value Drop loop var could exist.

    Comprehensions: shared demand_generic_iter_next with a dedicated iter_next_sigs/mono_syms channel (keying resolved_call_sigs by an expression node clobbered the iterable's own contract when it was itself a call — xs.into_iter() crashed; the typed_expr_types stamp also retyped the iterable). lower_comprehension_generic_iter dispatches the recorded mono, moves the payload, and inlines the clause body with a single-join per-iteration drop scope covering both filter edges; the leaf push registers the element move so the back-edge drop can't double-free what the output owns.

    Pinned: behav_for_generic_iterator (i64/str/comprehension), da_consuming_iter_full/break/moved/comprehension — counted drops, leak 0. Full :test green, evidence recorded.

    The silent ty_i32 terminal fallback in infer_for_element_type noted in the report still exists as a diagnostic-quality item; it is now unreachable for the generic-iterator shapes fixed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions