Repository navigation
Description
Summary
Implement the full §13.6 comprehension surface: target-polymorphic element comprehensions, map comprehensions, nested for, optional filters, and expected-type based collection selection. Current parser/lowering covers only a Vec-shaped array-comprehension path.
Spec
docs/with-specification.md:5990-5998 says comprehensions are one polymorphic family over collection targets: element comprehensions default to Vec, map comprehensions default to HashMap, and expected types select targets such as HashSet, BTreeSet, or BTreeMap. docs/with-specification.md:6000-6017 gives required examples, including [x*x for x in xs], HashSet = [...], HashMap = [name: age for ...], and BTreeMap = [...]. docs/with-specification.md:6019-6031 defines desugaring through collect[C]() or collect[M](). docs/with-specification.md:6044-6045 requires duplicate map keys to follow the target map's normal insertion semantics, last win for standard maps. The grammar includes both element and map comprehension heads at docs/with-specification.md:11387-11390.
Current state
src/Parser.w:6279-6313 parses only [expr for pattern in iter ... if filter] into NK_ARRAY_COMPREHENSION. Because it starts by parsing a single expression and then looks for for, [key: value for ...] has no map-head path. src/MirLower.w:7978-7979 lowers only NK_ARRAY_COMPREHENSION, so there is no map-comprehension lowering node or target-directed lowering path.
Existing coverage is element-only. test/spec/spec_ss13_6_comprehensions.w covers simple, nested, filtered, membership, and tuple-pattern element comprehensions, but there is no HashSet/BTreeSet target test and no HashMap/BTreeMap map comprehension test. test/behavior/behav_list_comp.w:1-4 is check-only and still contains an old TODO comment, so it is not useful as the source of truth.
Implementation plan
- Extend AST/parser support with a representation that distinguishes element comprehensions from map comprehensions while preserving the existing element behavior.
- Reuse
parse_pattern()for eachfor pattern in iterableclause so tuple, struct, enum, and slice patterns share the normal pattern implementation. - Teach sema to infer the comprehension target from expected type. With no expected type, default element comprehensions to
Vec[T]and map comprehensions toHashMap[K, V]. - Lower element comprehensions through the same
map/filter/collect[C]machinery required by §13.6, not a Vec-only special case. - Lower map comprehensions by inserting key/value pairs into the selected map target and preserving normal duplicate-key semantics.
- Keep
indisambiguation local to the comprehension parser as required bydocs/with-specification.md:6047-6055.
Test plan
Add behavior/spec tests for default Vec, expected HashSet, expected BTreeSet, default HashMap, expected BTreeMap, nested generators, filters, tuple-pattern bindings, and duplicate-key last-win behavior. Add compile-error tests for malformed map heads and incompatible expected collection targets.
Acceptance criteria
- All §13.6 examples compile and behave as specified.
[k: v for ...]works for default and expected map targets.[expr for ...]is target-polymorphic across at leastVec,HashSet, andBTreeSet.- Comprehension desugaring shares the standard collection target machinery rather than hardcoding Vec.
- Existing element-comprehension tests continue to pass.
Dependencies
Depends on #388 for target-polymorphic collect[C]() and overlaps #389 for collection literal target inference and map syntax.
Activity
ehartford commentedon Jun 13, 2026
Close-out from the
docs/finish.mdaudit:This is the right tracker for the remaining list-comprehension work. The old checklist entry was too broad/outdated:
test/spec/spec_ss13_6_comprehensions.w.test/behavior/behav_list_comp.wis stale: it is//! check-onlyand still says[expr for x in iter if cond]is not implemented.[k: v for ...].Acceptance addition from this audit: either delete/replace
test/behavior/behav_list_comp.wor turn it into real executable coverage. It should not continue to claim ordinary element list comprehensions are unimplemented.ehartford commentedon Jun 15, 2026
Implemented in 68f9eeb. Added target-polymorphic element comprehensions for Vec/HashSet, map comprehensions with default and expected HashMap targets, duplicate-key last-write semantics, parser/AST/walker support for [key: value for ...], and positive/negative tests. BTree targets remain the explicit #414 collection work and continue to fail loudly until that issue lands. Verification passed: WITH=$PWD/out/bin/with ./out/release/bin/with build; build :fixpoint; build :test; build :test-green.