fix(imports): extern 全局变量类型为 void 时 fail-fast

GlobalVariable 不接受 void 指针;解析为 void 时输出变量名、注解与行号,并让 TypeError 穿透外层忽略异常的兜底。
This commit is contained in:
TermiNexus committed 2026-10-18 15:49:35 +08:00
1 parent 4653abd653
commit a91bd8f35b
1 file changed
+8
+8
View File
@@ -1221,6 +1221,8 @@ class ImportHandle(BaseHandle):
# `_TryLoadClassMembersFromPyi ↔ _EmitExternalClassDeclLlvm ↔ _TryLoadStructFromStub`
# 环)。此处 fail-fast 上抛(保留原始栈),暴露上游根因而非掩盖。
raise
except TypeError:
raise
except Exception as e:
# NoneType 异常是数据缺失的正常兜底,静默不 WARN(干扰太多)
if not (isinstance(e, AttributeError) and "'NoneType'" in str(e)):
@@ -1366,6 +1368,12 @@ class ImportHandle(BaseHandle):
TypeInfo = CTypeInfo.FromNode(Node.annotation, self.Trans.SymbolTable)
if TypeInfo:
var_type = Gen._ctype_to_llvm(TypeInfo)
if isinstance(var_type, ir.VoidType):
raise TypeError(
f"[TPC] 全局变量 '{VarName}' 的类型注解 "
f"'{getattr(Node.annotation, 'id', type(Node.annotation).__name__)}' "
f"解析为 void(第 {getattr(Node, 'lineno', 0)} 行);"
f"请检查该类型是否已定义或声明顺序是否正确。")
IsPtr = TypeInfo.IsPtr
if isinstance(var_type, ir.IdentifiedStructType) and (var_type.elements is None or len(var_type.elements) == 0):
var_type = ir.IntType(32)