codegen: make collection aliases (Map/List/Set/…) type-check and construct - #16
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
genRefinedTypedefsemittedtypedef xc_Map_string_User_t xc_M_t;beforegenArrTypedefsdefined the underlyingxc_Map_string_User_t, so a compound value gaveunknown type name.empty Memitted 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
isCompositeAliasnow defers any collection-ctype alias (xc_Map_,xc_List_,xc_Set_, …) togenAliasTypedefs, which runs after the monomorphization typedefs exist — exactly howxc_arr_aliases were already handled.genPrimaryresolves a bareempty <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).Verification
examples/language/collection_alias_test.xi(Map with compound + primitive values, List, Set).state,Stack/Setaliases, nestedMap<String, List<Integer>>— all work.