feat(diag): 逃逸类 LifetimeError 追加 File lifetime leak 帧,标出泄漏点

StructGenMixin: 新增 escape_leak_line——错误路径重扫被调者函数体,取指定形参的**首个逃逸点**行号;
_NoteLeak 在三处置位来源采集(字段/全局写、return 形参、传递闭包调用),新增 EscLeakWant/EscLeakLine。

Diag: DiagEntry 增加 LeakFileName/LeakLine + set_leak + emit_leak_frame(标签 File lifetime leak),
打印顺序 = 调用链 → 错误点 → 泄漏点 → 类别: 消息。

TypeGenErr.fatal_error / GenApi.fail_if_stack_addr_escape 增加 leak_file/leak_line 透传;
CallGenMixin 调用点按失败实参位取行号。**仅同模块**:跨模块 EscFuncTable 无 AST → 返回 0,不打该帧。
This commit is contained in:
TermiNexus committed 2026-10-09 19:32:54 +08:00
1 parent c98730f717
commit 2353dca8ec
6 files changed
+112 -13

No files matched your search

+14 -3
View File
@@ -497,6 +497,14 @@ def get_param_escapes_hard_method(pool: memhub.MemBuddy*, class_name: str, metho
return 0
# escape_leak_line - **错误路径专用**:被调者第 want_pi 号形参的"首个逃逸写入点"行号。
# 只在 fail-fast 报错时调用(会重扫一次被调者函数体);跨模块拿不到 AST 时返回 0。
def escape_leak_line(name: str, want_pi: int) -> int:
"""形参 want_pi 的逃逸点行号;0 = 未知(含跨模块)"""
if _g_gen is not None: return _g_gen.StructGen.escape_leak_line(name, want_pi)
return 0
def get_param_willfree(name: str) -> int:
"""本文件函数的 willFree 形参掩码(第 k 位=第 k 个形参在本调用后失效);无记录返回 0"""
if _g_gen is not None: return _g_gen.StructGen.get_param_willfree(name)
@@ -725,10 +733,13 @@ def escape_fstring_copy(src: str) -> str:
# (b) `name` — a bare local ARRAY used where a pointer is expected (array decay);
# (c) propagated borrows — `s: str = l` gives Life(s)=FRAME, so `return s` is caught.
# A bare name with a matching aggregate target is a value copy, never an escape.
def fail_if_stack_addr_escape(node: ast.AST*, where: str, trans: HT.Translator*, target_ty: mlirlite.LLVMType* = None) -> int:
"""Fail fast (LifetimeError) when the address of a stack object escapes the frame."""
def fail_if_stack_addr_escape(node: ast.AST*, where: str, trans: HT.Translator*, target_ty: mlirlite.LLVMType* = None, leak_line: int = 0) -> int:
"""Fail fast (LifetimeError) when the address of a stack object escapes the frame.
leak_line > 0 时附带"泄漏点"帧(被调者那边造成逃逸的写入点行号,见 escape_leak_line)。
"""
if node is None or trans is None or _g_gen is None: return 0
return _g_gen.AssignGen.fail_if_life_escapes(node, where, trans, target_ty)
return _g_gen.AssignGen.fail_if_life_escapes(node, where, trans, target_ty, leak_line)