fix(core): 泛型特化翻译并入模板模块 imports,消除跨模块误报
This commit is contained in:
1 parent
adff2f35c4
commit
9322ac469f
1 file changed
+42
-1
@@ -13,6 +13,7 @@ import lib.core.Handles.HandlesType as HandlesType
|
||||
import lib.core.Handles.HandlesStruct as HandlesStruct
|
||||
import lib.core.Handles.HandlesVar as HandlesVar
|
||||
import lib.core.Handles.HandlesEnum as HandlesEnum
|
||||
import lib.core.StubMerger as StubMerger
|
||||
|
||||
|
||||
# 枚举成员临时数组最大数量(本地常量)
|
||||
@@ -2218,6 +2219,38 @@ def _cache_spec(pool: memhub.MemBuddy | t.CPtr, spec_key: str, spec_name: str):
|
||||
_spec_count += 1
|
||||
|
||||
|
||||
# ============================================================
|
||||
# _merge_template_imports - 特化翻译前并入模板模块的 imports
|
||||
#
|
||||
# 泛型特化(_specialize_generic_class)用触发方文件的 Translator 翻译
|
||||
# 模板方法体,但模板方法体引用的模块(如 _list.vp 的 append 里
|
||||
# string.memcpy 依赖 string)来自模板文件自身的 import。若不并入,
|
||||
# 跨模块 fast-fail 检查会把 string.memcpy 误报为"模块 string 未导入"。
|
||||
# 调用方负责在特化翻译后恢复 trans._imported_modules。
|
||||
# ============================================================
|
||||
def _merge_template_imports(trans: HT.Translator | t.CPtr,
|
||||
template_sha1: str) -> None:
|
||||
"""将模板模块的 imports 并入 trans._imported_modules(空格分隔)"""
|
||||
if trans is None or template_sha1 is None:
|
||||
return
|
||||
tdeps: str = StubMerger.LookupIncludesDeps(template_sha1)
|
||||
if tdeps is None or tdeps[0] == '\0':
|
||||
return
|
||||
cur: str = trans._imported_modules
|
||||
if cur is None or cur[0] == '\0':
|
||||
trans._imported_modules = tdeps
|
||||
return
|
||||
cur_len: t.CSizeT = string.strlen(cur)
|
||||
td_len: t.CSizeT = string.strlen(tdeps)
|
||||
merged: str = trans.Pool.alloc(cur_len + td_len + 2)
|
||||
if merged is None:
|
||||
return
|
||||
string.strcpy(merged, cur)
|
||||
merged[cur_len] = ' '
|
||||
string.strcpy(merged + cur_len + 1, tdeps)
|
||||
trans._imported_modules = merged
|
||||
|
||||
|
||||
def _specialize_generic_class(trans: HT.Translator | t.CPtr,
|
||||
class_name: str,
|
||||
type_args: list[str] | t.CPtr) -> str:
|
||||
@@ -2287,11 +2320,14 @@ def _specialize_generic_class(trans: HT.Translator | t.CPtr,
|
||||
trans.GenericTypeArgs = type_args
|
||||
HandlesType.set_generic_context(tp_names_pre, type_args)
|
||||
saved_ms_pre: str = trans.ModuleSha1
|
||||
saved_imp_pre: str = trans._imported_modules
|
||||
template_sha1_pre: str = _find_generic_template_sha1(class_name)
|
||||
if template_sha1_pre is not None:
|
||||
trans.ModuleSha1 = template_sha1_pre
|
||||
_merge_template_imports(trans, template_sha1_pre)
|
||||
_translate_oop_methods(trans, spec_cd_pre, pre_existing.Ty, spec_name, 0)
|
||||
trans.ModuleSha1 = saved_ms_pre
|
||||
trans._imported_modules = saved_imp_pre
|
||||
trans.GenericTypeParamNames = None
|
||||
trans.GenericTypeArgs = None
|
||||
HandlesType.clear_generic_context()
|
||||
@@ -2361,13 +2397,18 @@ def _specialize_generic_class(trans: HT.Translator | t.CPtr,
|
||||
# 切换 trans.ModuleSha1 为泛型模板定义模块的 SHA1,
|
||||
# 使结构体类型名和方法定义名都用定义模块的 SHA1 前缀,
|
||||
# 与调用端 get_struct_sha1(class_name) 查到的 SHA1 一致。
|
||||
# 同时并入模板模块的 imports(见 _merge_template_imports 注释),
|
||||
# 避免模板方法体内的跨模块调用被误报"模块未导入"。
|
||||
saved_module_sha1: str = trans.ModuleSha1
|
||||
saved_imported: str = trans._imported_modules
|
||||
template_sha1: str = _find_generic_template_sha1(class_name)
|
||||
if template_sha1 is not None:
|
||||
trans.ModuleSha1 = template_sha1
|
||||
_merge_template_imports(trans, template_sha1)
|
||||
translate_class_def(trans, spec_cd)
|
||||
# 恢复原始 ModuleSha1
|
||||
# 恢复原始 ModuleSha1 与 imports
|
||||
trans.ModuleSha1 = saved_module_sha1
|
||||
trans._imported_modules = saved_imported
|
||||
|
||||
# 8. 清除 type_map 上下文
|
||||
trans.GenericTypeParamNames = None
|
||||
|
||||
Reference in new issue
Block a user