fix(core): 局部数组+列表字面量改走逐元素初始化,消除 list[T] 误特化崩溃

This commit is contained in:
TermiNexus committed 2026-08-07 22:36:22 +08:00
1 parent 9322ac469f
commit 762d535a86
1 file changed
+30 -12
+30 -12
View File
@@ -519,18 +519,36 @@ class AnnAssignHandle(HandlesBase.Mixin):
# 如果有初始值,store # 如果有初始值,store
if aa.value is not None: if aa.value is not None:
rhs_val: llvmlite.Value | t.CPtr = HandlesExpr.translate_value( # 局部数组:列表字面量 → 逐元素 GEP + store 初始化
builder, pool, mod, aa.value, # 修复:此前局部 CArray[...] = [x, y] 会走 translate_list_literal,
None, 0, self.Trans) # 错误触发 list[T] 泛型特化(堆对象)导致崩溃(如 this.vp 的
if rhs_val is not None: # t.CArray[t.CChar, 256] = [0])。局部 alloca 未零初始化,
# 类型转换:将 rhs_val 转换为 alloca 的 pointee 类型 # 先整体置零再写列表元素(与全局数组默认零初始化语义一致)。
# 修复:整数字面量是 i32,但变量可能是 i8/i16/i64,需 trunc/sext local_arr_ty: llvmlite.LLVMType | t.CPtr = None
target_ty: llvmlite.LLVMType | t.CPtr = None if aa.value.kind() == ast.ASTKind.List and alloca.Ty is not None:
if alloca.Ty is not None: pt_l: llvmlite.LLVMType | t.CPtr = alloca.Ty.Pointee
target_ty = alloca.Ty.Pointee if HandlesExpr.is_array_type(pt_l) != 0:
if target_ty is not None: local_arr_ty = pt_l
rhs_val = HandlesExpr.coerce_to_type(builder, rhs_val, target_ty, node) if local_arr_ty is not None:
llvmlite.build_store(builder, rhs_val, alloca) zero_arr: llvmlite.Value | t.CPtr = llvmlite.ConstZero(
pool, local_arr_ty)
if zero_arr is not None:
llvmlite.build_store(builder, zero_arr, alloca)
_init_global_array_from_list(
builder, pool, mod, alloca, aa.value, self.Trans)
else:
rhs_val: llvmlite.Value | t.CPtr = HandlesExpr.translate_value(
builder, pool, mod, aa.value,
None, 0, self.Trans)
if rhs_val is not None:
# 类型转换:将 rhs_val 转换为 alloca 的 pointee 类型
# 修复:整数字面量是 i32,但变量可能是 i8/i16/i64,需 trunc/sext
target_ty: llvmlite.LLVMType | t.CPtr = None
if alloca.Ty is not None:
target_ty = alloca.Ty.Pointee
if target_ty is not None:
rhs_val = HandlesExpr.coerce_to_type(builder, rhs_val, target_ty, node)
llvmlite.build_store(builder, rhs_val, alloca)
return 0 return 0