Repository navigation
Description
Summary
Implement target-polymorphic collect[C] so iterator pipelines, collection literals, and comprehensions all share one collection-construction path. This removes the current Vec-only special case and is a prerequisite for map/set comprehensions and literal lowering that does not grow compiler intrinsics.
Spec
§13.3 says collection operations funnel through collect[C], which builds the requested collection. §4.3c says expected type selects Vec[T], HashSet[T], BTreeSet[T], HashMap[K, V], or BTreeMap[K, V] for bracket literals. §13.6 desugars comprehensions to iter |> ... |> collect[C]() with C from expected type, defaulting to Vec for element form and HashMap for map form.
Examples:
let xs: HashSet[i32] = source.iter().collect[HashSet[i32]]()
let by_name: HashMap[str, User] = users.iter().map(u => (u.name, u)).collect[HashMap[str, User]]()
let text: str = chars.iter().collect[String]()
Current state
MirIntrinsic has only ITER_COLLECT_VEC at src/Mir.w:139. MirLower.classify_intrinsic maps every iterator .collect call to that Vec intrinsic at src/MirLower.w:5935 and src/MirLower.w:5954. Codegen similarly classifies .collect as ITER_COLLECT_VEC at src/CodegenDispatch.w:4720 and src/CodegenDispatch.w:4739, then mir_emit_iter_collect_vec allocates a Vec and calls with_vec_push only (src/CodegenDispatch.w:8378-8418). docs/completed/spec-impl-unalignment.md:117-126 explicitly sequences this behind the generic-instantiation fix to avoid adding one intrinsic per collection target.
Implementation plan
- Land Fix sema generic type instantiation (TY_GENERIC_INST erasure) — prerequisite for stdlib-as-generics #391 first so generic instantiations keep concrete type identity through sema, MIR, and codegen; do not add more compiler-only collection intrinsics as a workaround.
- Add a generic
CollectInto[C, T]or equivalent stdlib trait/helper inlib/std/collections.wforVec[T],HashSet[T],HashMap[K, V],String, and after Implement BTreeMap and BTreeSet (§11.7, §18.6) #414 forBTreeMap/BTreeSet. - Replace
.collectintrinsic selection insrc/MirLower.wandsrc/CodegenDispatch.wwith normal generic method dispatch once the stdlib helper can type-check. KeepITER_COLLECT_VEConly as a bootstrap compatibility shim during the transition, then remove it. - For map targets, require iterator element type
(K, V)and callinsert(k, v). For set targets, callinsert(value). ForString, acceptchar,u8, orstrelements only if the stdlib exposes explicit append methods; otherwise diagnose unsupported element type. - Update comprehension and collection-literal lowering after Implement collection literals: map literals [k: v], empty [:], expected-type targets (§4.3c) #389/Implement target-polymorphic comprehensions incl. map form [k: v for ...] (§13.6) #390 so expected-type target construction calls the same collect path.
Test plan
Add test/spec/spec_ss13_3_collect_targets.w covering collect[Vec], collect[HashSet], collect[HashMap], and collect[String] from real iterators. Extend it after #414 with collect[BTreeSet] and collect[BTreeMap].
Add compile-error cases:
test/compile_errors/err_collect_hashmap_non_pair.wtest/compile_errors/err_collect_string_bad_elem.wtest/compile_errors/err_collect_unknown_target.w
Add integration coverage when #389/#390 land: collection literals and comprehensions should produce the same runtime results as explicit collect[C].
Acceptance criteria
.collect[C]()selects the expected collection type rather than always returningVec.- HashMap/BTreeMap collect require
(K, V)input elements and preserve last-write-wins insertion semantics. - HashSet/BTreeSet collect insert elements and support membership checks afterward.
- String collect works only for supported text element types and fails loudly otherwise.
- No new long-term per-target compiler intrinsics are introduced.
with build && with build :fixpoint && with build :testpass.
Dependencies
#391 is a hard prerequisite. #414 is required for BTreeMap/BTreeSet targets. #389 and #390 consume this work for literals and comprehensions. #392 should use the same generic iterator substrate.
Activity
ehartford commentedon Jun 15, 2026
Implemented in 2dddc23. collect[C] now uses a single generic collect intrinsic and selects Vec, HashSet, HashMap, and String targets from the explicit collect type. HashMap collect requires (K, V) iterator elements and preserves last-write-wins insertion; HashSet inserts elements; String currently accepts u8 elements and fails loudly for unsupported element types. BTreeMap/BTreeSet now fail with an explicit #414 diagnostic until that Phase 7 issue lands. Added positive spec coverage plus negative tests for non-pair HashMap input, invalid String input, unsupported targets, and BTree pending support. Verification passed: build, fixpoint, full test suite, and test-green.