Skip to content

lib: give same-named user structs and globals distinct identities - #217

Merged
dslsdzc merged 1 commit into
developfrom
lib-identity-struct-global
Oct 27, 2026
Merged

dslsdzc merged 1 commit into
developfrom
lib-identity-struct-global

Conversation

@dslsdzc

@dslsdzc dslsdzc commented Oct 27, 2026

Copy link
Copy Markdown
Owner

背景

跨库同名声明此前只做了“符号可见性”,没有区分声明身份。实测两个静默错误:

  1. 用户 struct IntMap + import hashmap:stdlib hashmap.cr 内部的 IntMap { keys = ... } 绑到用户结构体,报 TS02 Unknown field 'keys'。
  2. 用户 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 声明。
  • 方法表:方法行的类型键重映射到 struct 的唯一名。

边界(本批未做)

跨库同名函数/方法仍按 N11 拒绝:IR_CALL 存的是源名,两个同名函数在链接面无法区分。要支持函数级同名,需要把链接名也按库限定(g_func_link + checker 改写调用名 + ccr/backend 跟随),这是下一批。

判据

  • 用户 IntMap + import hashmap:两个布局并存,运行 42。
  • 用户 BITS32_BITS = 1 + import bits:读回 1。
  • selfhost-tests / bootstrap-tests / check 全绿。
  • canary 5/5 不变。

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.
@dslsdzc
dslsdzc merged commit fc78671 into develop Oct 27, 2026
4 checks passed
@dslsdzc
dslsdzc deleted the lib-identity-struct-global branch October 27, 2026 00:36
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