fix(compiler): 泛型注解收集类型实参时替换泛型参数 T + 属性访问 fail-fast traceback
- _get_type_name_from_annotation Name 分支应用 resolve_generic_type_name, 修复 ndarray[T] 二次递归特化崩溃(SimdTest numpy) - _specialize_generic_class 入口防御性替换残留类型参数名 - translate_attribute/get_attribute_ptr 非指针属性访问改 fatal_error(Python traceback 风格) - c.Deref(ptr).attr 属性访问 base 取指针本身不做 load - list[str] 容器 elem_size 4→8 修复(str 是指针 8 字节) - 泛型函数显式类型参数 xxx[T](x) 回退普通函数调用;T(v) 转换走 resolve_annotation_type
This commit is contained in:
1 parent
ba6beb439c
commit
ee8f532dc5
4 files changed
+136
-43
No files matched your search
@@ -4498,7 +4498,9 @@ def _extract_type_args_from_slice(pool: memhub.MemBuddy | t.CPtr,
|
||||
"""
|
||||
if slice_node is None or pool is None:
|
||||
return None
|
||||
result: list[str] | t.CPtr = list[str](pool, 4)
|
||||
# elem_size=8:str 是 i8* 指针(8 字节),若用 4 则双元素时 append 写 8 字节
|
||||
# 但步长按 4 计算,第 2 个元素覆盖第 1 个元素高 4 字节 → get 读出垃圾指针崩溃
|
||||
result: list[str] | t.CPtr = list[str](pool, 8)
|
||||
k: int = slice_node.kind()
|
||||
if k == ast.ASTKind.Tuple:
|
||||
tp: ast.Tuple | t.CPtr = (ast.Tuple | t.CPtr)(slice_node)
|
||||
@@ -4696,12 +4698,20 @@ def translate_call(pool: memhub.MemBuddy | t.CPtr,
|
||||
return _translate_generic_ctor_call(
|
||||
pool, builder, mod, spec_name, cl, trans)
|
||||
else:
|
||||
# 泛型类模板未找到:可能未 import 定义模块
|
||||
gt_err: str = pool.alloc(256)
|
||||
if gt_err is not None:
|
||||
viperlib.snprintf(gt_err, 256, "泛型类 '%s' 未找到模板,可能未 import 定义模块", gen_class_name)
|
||||
HandlesType.fatal_error(gen_nm_node, gt_err)
|
||||
HandlesType.fatal_error(gen_nm_node, "泛型类模板未找到")
|
||||
# 泛型类模板未找到:可能是泛型函数手动传类型参数调用 xxx[T](x)
|
||||
# (对应 TPC _HandleGenericCallExplicitLlvm;TPV 无泛型函数特化,
|
||||
# 泛型函数按普通函数编译,类型参数 [T] 仅在函数定义处消费)。
|
||||
# 检查当前模块是否已定义同名函数(def double[T] 编译后即普通函数
|
||||
# double),若是则丢弃 [t.CInt] 显式类型参数,退回普通函数调用。
|
||||
if find_func_in_module(mod, gen_class_name) is not None:
|
||||
func_name = gen_class_name
|
||||
else:
|
||||
# 泛型类模板未找到:可能未 import 定义模块
|
||||
gt_err: str = pool.alloc(256)
|
||||
if gt_err is not None:
|
||||
viperlib.snprintf(gt_err, 256, "泛型类 '%s' 未找到模板,可能未 import 定义模块", gen_class_name)
|
||||
HandlesType.fatal_error(gen_nm_node, gt_err)
|
||||
HandlesType.fatal_error(gen_nm_node, "泛型类模板未找到")
|
||||
if func_name is None:
|
||||
# 检查是否是 (A | t.CPtr)(value) 形式的类型转换
|
||||
# BinOp 节点作为 Call func: 联合类型注解用作类型转换
|
||||
@@ -4878,6 +4888,19 @@ def translate_call(pool: memhub.MemBuddy | t.CPtr,
|
||||
bare_ty = llvmlite.Double(pool)
|
||||
else:
|
||||
bare_ty = HandlesType.map_t_type(pool, bare_fn_name)
|
||||
# 泛型上下文替换出的类型名(如 T→"t.CInt")map_t_type 不识别(只认无前缀名),
|
||||
# 回退用 resolve_annotation_type 解析(支持 t.CXxx / 结构体类名 / 别名等)。
|
||||
# GenericTest4 Box[t.CInt].__init__ 的 self.value = T(v) 依赖此路径。
|
||||
if bare_ty is None and string.strcmp(bare_fn_name, func_name) != 0:
|
||||
gp_ast: ast.AST | t.CPtr = HandlesType._parse_type_string(pool, bare_fn_name, 0)
|
||||
if gp_ast is not None:
|
||||
imp_gp: str = None
|
||||
fi_gp: str = None
|
||||
if trans is not None:
|
||||
imp_gp = trans._imported_modules
|
||||
fi_gp = trans._from_imports
|
||||
bare_ty = HandlesType.resolve_annotation_type(
|
||||
pool, gp_ast, imp_gp, fi_gp, trans)
|
||||
if bare_ty is not None and can == 1:
|
||||
bare_arg_node: ast.AST | t.CPtr = cargs.get(0)
|
||||
if bare_arg_node is not None:
|
||||
|
||||
Reference in new issue
Block a user