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
+18
+18
View File
@@ -519,6 +519,24 @@ class AnnAssignHandle(HandlesBase.Mixin):
# 如果有初始值,store
if aa.value is not None:
# 局部数组:列表字面量 → 逐元素 GEP + store 初始化
# 修复:此前局部 CArray[...] = [x, y] 会走 translate_list_literal,
# 错误触发 list[T] 泛型特化(堆对象)导致崩溃(如 this.vp 的
# t.CArray[t.CChar, 256] = [0])。局部 alloca 未零初始化,
# 先整体置零再写列表元素(与全局数组默认零初始化语义一致)。
local_arr_ty: llvmlite.LLVMType | t.CPtr = None
if aa.value.kind() == ast.ASTKind.List and alloca.Ty is not None:
pt_l: llvmlite.LLVMType | t.CPtr = alloca.Ty.Pointee
if HandlesExpr.is_array_type(pt_l) != 0:
local_arr_ty = pt_l
if local_arr_ty is not None:
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)