From 34c4eeff025f9f7a44882de88863371eef2d26bb Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Fri, 2 Oct 2026 22:35:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(flow):=20=E5=BE=AA=E7=8E=AF/match=20?= =?UTF-8?q?=E5=A4=96=E7=9A=84=20break/continue=20=E6=94=B9=E4=B8=BA=20fail?= =?UTF-8?q?-fast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit HandlesBody:loop_break_targets / loop_continue_targets 为空时不再静默不生成跳转, 直接 FailFastError 暴露——原实现会让 break/continue 无声无效果(不兜底原则)。 --- lib/core/Handles/HandlesBody.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/core/Handles/HandlesBody.py b/lib/core/Handles/HandlesBody.py index 740ab34..07a9550 100644 --- a/lib/core/Handles/HandlesBody.py +++ b/lib/core/Handles/HandlesBody.py @@ -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):