diff --git a/App/lib/core/Handles/HandlesAnnAssign.vp b/App/lib/core/Handles/HandlesAnnAssign.vp index 3fcd8ea..706cfa8 100644 --- a/App/lib/core/Handles/HandlesAnnAssign.vp +++ b/App/lib/core/Handles/HandlesAnnAssign.vp @@ -519,18 +519,36 @@ class AnnAssignHandle(HandlesBase.Mixin): # 如果有初始值,store if aa.value is not None: - 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) + # 局部数组:列表字面量 → 逐元素 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) + 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