升级了仿 Python 错误堆栈提示,修复了 bool 不存在的问题,将 bool 改为 i1 而非 i8,实现了 bool 的归一化

This commit is contained in:
TermiNexus committed 2026-07-31 04:12:12 +08:00
1 parent bfb55d68f4
commit 9dcf34599d
13 files changed
+938 -213

No files matched your search

+39 -4
View File
@@ -14,6 +14,7 @@ import lib.core.Handles.HandlesNonlocal as HandlesNonlocal
import lib.core.Handles.HandlesClassDef as HandlesClassDef
import lib.core.Handles.HandlesExprCall as HandlesExprCall
import lib.core.Handles.HandlesStruct as HandlesStruct
import lib.core.VLogger as VLogger
# ============================================================
@@ -310,7 +311,7 @@ class AnnAssignHandle(HandlesBase.Mixin):
builder: llvmlite.IRBuilder | t.CPtr = self.Trans._cur_builder
# 从 annotation 推断类型(修复:原硬编码 i32 导致 str 等类型错误)
var_ty: llvmlite.LLVMType | t.CPtr = llvmlite.Int32(pool)
var_ty: llvmlite.LLVMType | t.CPtr = None
annot: ast.AST | t.CPtr = aa.annotation
if annot is not None:
resolved: llvmlite.LLVMType | t.CPtr = HandlesType.resolve_annotation_type(
@@ -334,6 +335,18 @@ class AnnAssignHandle(HandlesBase.Mixin):
pool, annot, self.Trans._imported_modules, self.Trans._from_imports, self.Trans)
if resolved is not None:
var_ty = resolved
else:
# fast-fail: 注解类型解析失败,不再静默回退 i32(会导致变量类型错误)
VLogger.error(
f"变量类型注解解析失败,变量名: {nm.id}",
"HandlesAnnAssign")
var_ty = llvmlite.Int32(pool)
else:
# fast-fail: AnnAssign 必须有 annotation
VLogger.error(
f"AnnAssign 缺少类型注解,变量名: {nm.id}",
"HandlesAnnAssign")
var_ty = llvmlite.Int32(pool)
# 确保跨模块结构体的完整定义在当前模块中可用(供 alloca 分配空间)
HandlesStruct.ensure_struct_def_in_module(pool, self.Trans.Module, var_ty)
@@ -388,13 +401,25 @@ class AnnAssignHandle(HandlesBase.Mixin):
mod: llvmlite.LLVMModule | t.CPtr = self.Trans.Module
# 确定类型(从 annotation 推断)
var_ty: llvmlite.LLVMType | t.CPtr = llvmlite.Int32(pool)
var_ty: llvmlite.LLVMType | t.CPtr = None
annot: ast.AST | t.CPtr = aa.annotation
if annot is not None:
resolved: llvmlite.LLVMType | t.CPtr = HandlesType.resolve_annotation_type(
pool, annot, self.Trans._imported_modules, self.Trans._from_imports, self.Trans)
if resolved is not None:
var_ty = resolved
else:
# fast-fail: 注解类型解析失败,不再静默回退 i32
VLogger.error(
f"变量类型注解解析失败,变量名: {nm.id}",
"HandlesAnnAssign")
var_ty = llvmlite.Int32(pool)
else:
# fast-fail: AnnAssign 必须有 annotation
VLogger.error(
f"AnnAssign 缺少类型注解,变量名: {nm.id}",
"HandlesAnnAssign")
var_ty = llvmlite.Int32(pool)
# global 变量:写入模块作用域中的全局变量
if HT.is_global_name(self.Trans, nm.id) != 0:
@@ -423,9 +448,19 @@ class AnnAssignHandle(HandlesBase.Mixin):
nl_ptr: llvmlite.Value | t.CPtr = HandlesNonlocal.get_nonlocal_var_ptr(
self.Trans, nm.id)
if nl_ptr is not None:
i32_ty: llvmlite.LLVMType | t.CPtr = llvmlite.Int32(pool)
# 从 nl_ptr 的类型推断 nonlocal 变量实际类型,
# 不再强制 i32(会截断指针/结构体/i64 导致闭包数据损坏)
nl_target_ty: llvmlite.LLVMType | t.CPtr = None
if nl_ptr.Ty is not None:
nl_target_ty = nl_ptr.Ty.Pointee
if nl_target_ty is None:
# fast-fail: nl_ptr 类型信息丢失,无法安全 store
VLogger.error(
f"nonlocal 变量类型信息丢失,无法写入: {nm.id}",
"HandlesAnnAssign")
return 0
rhs_coerced: llvmlite.Value | t.CPtr = HandlesExpr.coerce_to_type(
builder, rhs_val, i32_ty)
builder, rhs_val, nl_target_ty)
llvmlite.build_store(builder, rhs_coerced, nl_ptr)
return 0