Repository navigation
lib: give same-named user structs and globals distinct identities - #217
Merged
Merged
Conversation
A user declaration with the same source name as a loaded library declaration used to share the library layout: stdlib literals bound the user struct (Unknown field keys), and globals deduplicated by name so a user global read the stdlib slot. - Type identity: collect_decls assigns an internal unique name to colliding struct declarations from different libraries. Symbols and exports keep the source name; TYP_NAMED carries the unique name, so layouts and method type keys remain separate. find_struct_by_name resolves through the visibility- aware type symbol instead of the first declaration table row. - Global slots: g_ir_global_lib records the owner. reg_one_global deduplicates by (name, owner); find_global/global_var_of/global const and optional lookups prefer the owner of the declaration site. Manual runtime registrations take the owner of the source declaration, so canary global layout is unchanged. - Symbol lookup prefers the current library (find_sym/find_gsym/find_func), then falls back to the unique visible pub declaration. - Cross-library function/method duplicate names remain rejected: IR_CALL stores a source name, so allowing two same-named functions would make the target ambiguous until link names are library-qualified. Tests: user IntMap + imported hashmap keeps both layouts and runs; user BITS32_BITS + imported bits reads its own slot. Canary 5/5 unchanged; selfhost/bootstrap/check green.
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.
背景
跨库同名声明此前只做了“符号可见性”,没有区分声明身份。实测两个静默错误:
struct IntMap+import hashmap:stdlibhashmap.cr内部的IntMap { keys = ... }绑到用户结构体,报TS02 Unknown field 'keys'。BITS32_BITS : int = 1+import bits:全局槽按名字去重,读到 stdlib 的 32。修法
collect_decls对来自不同库的同名 struct 分配内部唯一名;符号和导出表仍用源名,TYP_NAMED用唯一名。find_struct_by_name改为经可见性感知的类型符号解析,不再取声明表第一行。g_ir_global_lib记录每个槽的 owner;reg_one_global按(name, owner)去重,find_global/global_var_of/全局常量与可选声明查询优先当前声明库。手工 runtime 全局取源声明的 owner,canary 布局不变。find_sym/find_gsym/find_func先找当前库,再找唯一可见的 pub 声明。边界(本批未做)
跨库同名函数/方法仍按 N11 拒绝:
IR_CALL存的是源名,两个同名函数在链接面无法区分。要支持函数级同名,需要把链接名也按库限定(g_func_link+ checker 改写调用名 + ccr/backend 跟随),这是下一批。判据
IntMap+import hashmap:两个布局并存,运行 42。BITS32_BITS = 1+import bits:读回 1。selfhost-tests/bootstrap-tests/check全绿。canary5/5 不变。