Skip to content

§13.5 implicit .iter() is not implemented for types other than Vec/HashMap/BTreeMap (SortedVec, Trie, user types) #1837

Closed

Description

@ehartford

Spec

§13.5 For-In Loops, Implicit iteration: "When it does not implement Iter[T] but has an .iter() method that returns an Iter[T], the compiler inserts .iter() automatically."

Repro

use std.collections.sorted_vec
fn main:
    var s: SortedVec[i32] = SortedVec.new()
    s.insert(3)
    for x in s:
        print(f"{x}")

with check (main 355c20b): ok. with run:

error: MIR lowering failed for function 'main' in sv.w; AST codegen was removed, so this function cannot be compiled

for x in s.iter(): prints 3.

Cause

Only Vec, HashMap, BTreeMap (and & of them) get the implicit form, each special-cased in SemaCheck.infer_for_element_type and MirLower.lower_for. For any other type Sema fell through to an i32 element type (the #1828 fallback) and MIR tried to call next() on the collection itself (lower_for, callee_sym == next_sym → mark_unsupported).

Expected

Sema resolves the .iter() call for the loop (receiver mode, generic specialization, the @[iter_of_self] borrow of the collection) and records it under the for node; MIR lowers that call and steps the returned iterator with the existing next() protocol.

Interim

The #1828 fix makes Sema refuse this loop with a named diagnostic pointing at .iter() instead of failing in MIR. Workaround: spell .iter().

Activity

  1. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by 7b56d3f (stack #1979, battery green on 029574d). for x in c inserts .iter() for any type whose iter() returns an Iter[T] (§13.5); Sema records it, MIR lowers it; SortedVec, user types, &T and generic fixtures.

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