Skip to content

codegen: make collection aliases (Map/List/Set/…) type-check and construct - #16

Merged
code-by-sia merged 1 commit into
mainfrom
compiler-robustness
Aug 7, 2026
Merged

codegen: make collection aliases (Map/List/Set/…) type-check and construct#16
code-by-sia merged 1 commit into
mainfrom
compiler-robustness

Conversation

@code-by-sia

Copy link
Copy Markdown
Owner

Two coupled bugs made a collection alias (type M = Map<K, V>, List<T>, Set<T>, Stack<T>, …) unusable, and the second one was a silent runtime crash.

Bugs

  1. Typedef orderinggenRefinedTypedefs emitted typedef xc_Map_string_User_t xc_M_t; before genArrTypedefs defined the underlying xc_Map_string_User_t, so a compound value gave unknown type name.
  2. Silent null-structempty M emitted a zeroed (xc_M_t){0} (a null map/list with no backing storage) that compiled fine and then crashed at first .put/.get. Fixing (1) alone would have turned the compile error into this silent runtime crash, so they had to be fixed together.

Fix

  1. isCompositeAlias now defers any collection-ctype alias (xc_Map_, xc_List_, xc_Set_, …) to genAliasTypedefs, which runs after the monomorphization typedefs exist — exactly how xc_arr_ aliases were already handled.
  2. genPrimary resolves a bare empty <collection-alias> to the real runtime constructor (xstd_map_new / xstd_list_new / xstd_set_new / …), reconstructing the element ctypes from the alias base type (the key suffix is always a single known word, so the split is unambiguous).
type UserMap = Map<String, User>
let m = empty UserMap           // now a real map, was a null struct
m.put("ada", User { name: "Ada", age: 36 })

Verification

  • Regression test examples/language/collection_alias_test.xi (Map with compound + primitive values, List, Set).
  • Probed edge cases: alias as class state, Stack/Set aliases, nested Map<String, List<Integer>> — all work.
  • Self-host fixpoint holds (gen2 == gen3); all 46 existing test files pass.

…nstruct

Two coupled bugs made a collection alias (type M = Map<K, V>, List<T>, Set<T>,
etc.) unusable:

1. Typedef ordering: genRefinedTypedefs emitted the alias typedef before
   genArrTypedefs defined the underlying monomorphization (e.g.
   xc_Map_string_User_t), so a compound value gave an unknown-type error.
   isCompositeAlias now defers any collection-ctype alias to genAliasTypedefs,
   after those typedefs exist, matching how xc_arr_ aliases were already handled.

2. empty M emitted a zeroed (xc_M_t){0} (a null map/list) that crashed at first
   use. genPrimary now resolves a bare empty <collection-alias> to the real
   runtime constructor (xstd_map_new / list_new / set_new / ...), reconstructing
   the element ctypes from the alias base type.

Collection aliases now type-check, construct, and work as locals and class
state, including compound values and nesting. Regression test in
examples/language/collection_alias_test.xi; the self-host fixpoint holds and all
existing tests pass.
@code-by-sia
code-by-sia merged commit adacb66 into main Aug 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant