fix(tpc): 返回 fail-fast 误报修复——末句控制流掉出末尾不报错

- 检查未区分普通语句掉出与末句控制流(match/if)掉出:match 翻译后停在合成 after 块(未 terminated)是机械产物,所有 arm 已 terminated 属正常(fatal_error + return None 样板),被误判为掉出缺陷
- 修复:Node.body[-1] 是 If/Match/While/For/Try/With 时豁免 fail-fast(符合隐式返回规则:末句控制流 -> unreachable)
- NegativeTest missing_return(末句 Assign)仍报错
This commit is contained in:
TermiNexus committed 2026-08-22 04:08:43 +08:00
1 parent 3117ab3dec
commit 09d03f4525
1 file changed
+8 -1
+8 -1
View File
@@ -1841,7 +1841,14 @@ class FunctionHandle(BaseHandle):
# → 立即报错,不静默 ret 0(用户隐式返回规则)。
# -> None / -> t.CVoid 视为 Void(掉出 ret void 合法);无注解 → C 风格
# 过程(ConstZero,保持现状);其余注解(int/str/...)掉出末尾报错。
if Node.returns is not None:
# 例外:末句是控制流(If/Match/While/For/Try/With)→ 掉出末尾合法
# (隐式返回规则:末句控制流 → unreachable)。match/if 等翻译后停在
# 合成 after 块(未 terminated)是机械产物,所有 arm 已 terminated 属
# 正常(如 fatal_error + return None 样板),不能误判为普通语句掉出。
_last_stmt = Node.body[-1] if Node.body else None
_is_ctrl_tail: bool = isinstance(_last_stmt, (
ast.If, ast.Match, ast.While, ast.For, ast.Try, ast.With))
if Node.returns is not None and not _is_ctrl_tail:
_ret_annot_void: bool = (
isinstance(Node.returns, ast.Constant) and Node.returns.value is None) or (
isinstance(Node.returns, ast.Attribute) and getattr(Node.returns, 'attr', '') == 'CVoid')