Skip to content

Implement collection literals: map literals [k: v], empty [:], expected-type targets (§4.3c) #389

Closed

Description

@ehartford

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-6329 parses [], [expr; count], list literals, and comprehensions. There is no key: value element form and no [:] special case.
  • src/SemaCheck.w:5848-5882 checks array literals only as fixed arrays or expected array/slice values. It does not select Vec, HashSet, BTreeSet, HashMap, or BTreeMap from expected type.
  • src/MirLower.w:8320-8350 lowers NK_ARRAY_LIT as an aggregate array literal.
  • src/ComptimeEval.w has explicit HashMap operations, but no collection-literal construction path.
  • Runtime/library surface exists for Vec, HashMap, and HashSet in lib/std/collections.w:8-34; BTreeSet/BTreeMap are not visible in the current stdlib scan and may need a separate availability decision.

Implementation plan

  1. 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.
  2. Reject mixed forms such as [a, b: c] with a parse or sema diagnostic that points at the first inconsistent separator.
  3. In Sema, use expected type to select the target collection:
    • no expected type plus sequence elements -> fixed array,
    • expected Vec[T], HashSet[T], or BTreeSet[T] -> construct that collection from elements,
    • no expected type plus key/value pairs -> HashMap[K, V],
    • expected HashMap[K, V] or BTreeMap[K, V] -> construct that map,
    • empty [] and [:] require expected type.
  4. 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.
  5. 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 unsupported BTree* 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, and with 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

  1. ehartford commented on Jun 15, 2026

    @ehartford
    ContributorAuthor

    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.

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