From 762d535a869d68e9832f10166effc3cdcc77a70e Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Fri, 7 Aug 2026 22:36:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20=E5=B1=80=E9=83=A8=E6=95=B0?= =?UTF-8?q?=E7=BB=84+=E5=88=97=E8=A1=A8=E5=AD=97=E9=9D=A2=E9=87=8F?= =?UTF-8?q?=E6=94=B9=E8=B5=B0=E9=80=90=E5=85=83=E7=B4=A0=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E6=B6=88=E9=99=A4=20list[T]=20=E8=AF=AF?= =?UTF-8?q?=E7=89=B9=E5=8C=96=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/lib/core/Handles/HandlesAnnAssign.vp | 42 +++++++++++++++++------- 1 file changed, 30 insertions(+), 12 deletions(-) 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