fix(classdef,base,attr): 成员注解解析静默兜底改 fail-fast;新增按需输出的字段布局探针

HandlesBase.CollectAnnAssignMember: 注解解析返回 None / _ctype_to_llvm 得 void / 抛异常三处静默 i32、i8* 兜底改 fail-fast,报错带成员名+注解原文+BaseType/PtrCount 现场

HandlesClassDef: 方法内 self.x: T = ... 的同类三处兜底同样改 fail-fast(带类名/成员名/注解原文)

HandlesExprAttr: 新增 _diag_member_layout(默认静默,仅当 offset 为 None / members[offset] 名不符 / 类型与 elements[offset] 不一致时打一行,上限 15 行)
This commit is contained in:
TermiNexus committed 2026-09-26 21:25:52 +08:00
1 parent c83fc752c6
commit 50cea6c7b3
3 files changed
+104 -16

No files matched your search

+27 -10
View File
@@ -1001,21 +1001,21 @@ class BaseHandle:
) -> tuple[str, _ir.Type, CTypeInfo | None] | None:
"""从 ast.AnnAssign 节点解析成员信息。
返回 (VarName, MemberType, TypeInfo);TypeInfo 为 None 表示解析回退到 i32。
返回 (VarName, MemberType, TypeInfo)。
**不再静默兜底**(原实现:注解解析返回 None → i32;_ctype_to_llvm 得 void → i8*;
任何异常 → i32 + TypeInfo=None)。这些兜底会把"注解没解析出来"伪装成一个
具体类型,下游于是报出与真因无关的错——实测:`free_lists: uint64*`(脱糖为
`uint64 | t.CPtr`)在解析异常时被当成 **i32**,`self.free_lists = base`(i8*)
便误报"指针隐式转整数已禁止"。类型信息缺失一律响亮失败。
非 AnnAssign/Name 目标时返回 None。
"""
if not isinstance(item, ast.AnnAssign) or not isinstance(item.target, ast.Name):
return None
VarName: str = item.target.id
AnnotationText: str = ast.unparse(item.annotation)
try:
TypeInfo: CTypeInfo | None = self.ResolveAnnotationType(item.annotation)
if TypeInfo is None:
TypeInfo = CTypeInfo()
TypeInfo.BaseType = t.CInt()
MemberType: _ir.Type = Gen._ctype_to_llvm(TypeInfo)
if isinstance(MemberType, _ir.VoidType):
MemberType = _ir.PointerType(_ir.IntType(8))
return VarName, MemberType, TypeInfo
except RecursionError:
# RecursionError 不是"可忽略异常":它是类型解析/声明发射链**成环**的
# 真缺陷信号。此前被下方 `except Exception` 吞成一行
@@ -1026,8 +1026,25 @@ class BaseHandle:
# 让环路根因暴露在错误栈顶,不再被 WARN 掩埋。
raise
except Exception as e:
_vlog().warning(f"HandlesBase: 忽略异常 {e}", exc_info=e)
return VarName, _ir.IntType(32), None
raise ValueError(
f"[FATAL] 成员 '{VarName}' 的注解 '{AnnotationText}' 解析失败:"
f"{type(e).__name__}: {e} —— 拒绝静默退化为 i32"
) from e
if TypeInfo is None:
raise ValueError(
f"[FATAL] 成员 '{VarName}' 的注解 '{AnnotationText}' 解析不出类型"
f"(ResolveAnnotationType 返回 None)—— 拒绝静默退化为 i32"
)
MemberType: _ir.Type = Gen._ctype_to_llvm(TypeInfo)
if isinstance(MemberType, _ir.VoidType):
raise ValueError(
f"[FATAL] 成员 '{VarName}' 的注解 '{AnnotationText}' 解析为 void"
f"(BaseType={getattr(TypeInfo, 'BaseType', None)} "
f"PtrCount={getattr(TypeInfo, 'PtrCount', None)} "
f"IsTypedef={getattr(TypeInfo, 'IsTypedef', None)} "
f"Name={getattr(TypeInfo, 'Name', None)})—— 拒绝静默退化为 i8*"
)
return VarName, MemberType, TypeInfo
def LookupFunctionSymbol(self, name: str, *, module_path: str | None = None) -> CTypeInfo | None:
"""查找函数符号,先尝试 module_path.name,再回退到 name。