fix(flow): 循环/match 外的 break/continue 改为 fail-fast

HandlesBody:loop_break_targets / loop_continue_targets 为空时不再静默不生成跳转,
直接 FailFastError 暴露——原实现会让 break/continue 无声无效果(不兜底原则)。
This commit is contained in:
TermiNexus committed 2026-10-02 22:35:26 +08:00
1 parent cc0615e519
commit 34c4eeff02
1 file changed
+13 -4
+13 -4
View File
@@ -41,11 +41,20 @@ class BodyHandle(BaseHandle):
elif isinstance(Node, ast.While):
self.Trans.WhileHandler._HandleWhileLlvm(Node)
elif isinstance(Node, ast.Break):
if Gen.loop_break_targets:
Gen.builder.branch(Gen.loop_break_targets[-1])
if not Gen.loop_break_targets:
# fail-fast:循环 / match-case 之外的 break 无处可跳,
# 静默忽略只会让"提前退出"变成无声无效果(不兜底)
from lib.core.LLVMCG.FuncGen import FailFastError
raise FailFastError(
"break 只能用在循环或 match-case 体内(当前没有可跳出的目标)")
Gen.builder.branch(Gen.loop_break_targets[-1])
elif isinstance(Node, ast.Continue):
if Gen.loop_continue_targets:
Gen.builder.branch(Gen.loop_continue_targets[-1])
if not Gen.loop_continue_targets:
# fail-fast:循环之外的 continue 无处可跳(同上)
from lib.core.LLVMCG.FuncGen import FailFastError
raise FailFastError(
"continue 只能用在循环体内(当前没有可继续的循环)")
Gen.builder.branch(Gen.loop_continue_targets[-1])
elif isinstance(Node, ast.With):
self.Trans.WithHandler._HandleWithLlvm(Node)
elif isinstance(Node, ast.ClassDef):