Repository navigation
Description
Implement collection literals: map literals [k: v], empty [:], expected-type targets (§4.3c)
Summary
Bracket collection literals are specified as expected-type-driven syntax for arrays, Vec, sets, and maps. The compiler currently parses only array-style element lists and lowers them as fixed arrays, so map literals ([k: v]), empty maps ([:]), and expected-type targets are missing.
Spec
docs/with-specification.md:1572-1614 defines collection literals. [1, 2, 3] defaults to a fixed-size array but can target Vec[T], HashSet[T], or BTreeSet[T] from expected type. [key: value] defaults to HashMap[K, V] and can target BTreeMap[K, V]. [] is an empty sequence literal that requires expected type; [:] is an empty map literal. Duplicate map keys are legal and the later value wins.
Current state
src/Parser.w:6251-6329parses[],[expr; count], list literals, and comprehensions. There is nokey: valueelement form and no[:]special case.src/SemaCheck.w:5848-5882checks array literals only as fixed arrays or expected array/slice values. It does not selectVec,HashSet,BTreeSet,HashMap, orBTreeMapfrom expected type.src/MirLower.w:8320-8350lowersNK_ARRAY_LITas an aggregate array literal.src/ComptimeEval.whas explicitHashMapoperations, but no collection-literal construction path.- Runtime/library surface exists for
Vec,HashMap, andHashSetinlib/std/collections.w:8-34;BTreeSet/BTreeMapare not visible in the current stdlib scan and may need a separate availability decision.
Implementation plan
- Extend the parser to distinguish sequence literals from map literals. Add a representation that preserves key/value pairs and the empty-map
[:]form without overloading the existing array node in a way that loses syntax. - Reject mixed forms such as
[a, b: c]with a parse or sema diagnostic that points at the first inconsistent separator. - In Sema, use expected type to select the target collection:
- no expected type plus sequence elements -> fixed array,
- expected
Vec[T],HashSet[T], orBTreeSet[T]-> construct that collection from elements, - no expected type plus key/value pairs ->
HashMap[K, V], - expected
HashMap[K, V]orBTreeMap[K, V]-> construct that map, - empty
[]and[:]require expected type.
- Lower map and collection targets through ordinary construction plus insert/push operations, or through the
collect[C]multi-target lowering once Implement collect[C] multi-target (HashMap, HashSet, BTreeMap, BTreeSet, String) #388 is available. Preserve duplicate-key "last wins" by inserting in source order. - Add comptime support or emit a loud compile-time diagnostic if a collection literal appears in a comptime context before the target collection can be constructed there.
Test plan
- Parser/sema positive tests for fixed array default, expected
Vec,HashSet,HashMap, empty[], and empty[:]. - Map literal duplicate-key test showing the last value wins.
- Negative tests for mixed sequence/map elements, empty literal without expected type, non-hashable map keys when targeting
HashMap, and unsupportedBTree*targets if those stdlib types are not yet present. - Add at least one comptime test for a supported literal target, or a compile-error test for the loud unsupported path.
- Run
with build,with build :fixpoint, andwith build :test.
Acceptance criteria
-
[k: v]and[:]parse and preserve key/value structure. - Expected type selects the correct collection target for sequence and map literals.
- Empty literals require expected type and produce useful diagnostics.
- Duplicate map keys keep the later source value.
- Unsupported target collections fail loudly rather than silently lowering to the wrong type.
Dependencies
Depends on #388 for collect[C] multi-target support if the implementation chooses to lower literals through collect. If #388 is not ready, this issue can still land by lowering directly to collection constructors and insert/push methods for the currently available stdlib types.
Activity
ehartford commentedon Jun 15, 2026
Implemented in 8bfc358. Added distinct AST/parser support for map literals and empty [:], sema expected-type selection for fixed arrays, Vec, HashSet, and HashMap, native MIR/codegen construction for collection and map literals, duplicate-key last-write behavior through ordered inserts, and loud diagnostics for mixed forms, empty literals without expected type, non-Hash map keys, wrong map targets, and BTree targets pending #414. Verification passed: build, fixpoint, full test suite, and test-green.