From 4353efd8ff92cee4b3d4d90d294604e3f75c255c Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Fri, 2 Oct 2026 22:40:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(flow):=20=E5=B5=8C=E5=A5=97/=E5=8D=8F?= =?UTF-8?q?=E7=A8=8B=E5=87=BD=E6=95=B0=E4=BD=93=E7=8B=AC=E7=AB=8B=20break/?= =?UTF-8?q?continue=20=E7=9B=AE=E6=A0=87=EF=BC=8C=E8=B6=8A=E7=95=8C?= =?UTF-8?q?=E5=8D=B3=20fail-fast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - HandlesFor._HandleNestedFunctionLlvm:进入嵌套函数体前清空 loop_break_targets / loop_continue_targets,退出恢复——原实现会让嵌套函数体内的 break/continue 继承 外层函数的跳转目标(生成跨函数 br,非法 IR) - HandlesAsyncFunctions:协程体内循环之外的 break/continue 改为 FailFastError, 不再静默不生成跳转 --- lib/core/Handles/HandlesAsyncFunctions.py | 15 +++++++++++---- lib/core/Handles/HandlesFor.py | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/core/Handles/HandlesAsyncFunctions.py b/lib/core/Handles/HandlesAsyncFunctions.py index 2bb8415..7b32ba4 100644 --- a/lib/core/Handles/HandlesAsyncFunctions.py +++ b/lib/core/Handles/HandlesAsyncFunctions.py @@ -531,12 +531,19 @@ class AsyncFunctionHandle(BaseHandle): self._HandleCoroFor(stmt, Gen) continue if isinstance(stmt, ast.Break): - if Gen.loop_break_targets: - Gen.builder.branch(Gen.loop_break_targets[-1]) + if not Gen.loop_break_targets: + # fail-fast:协程体内、循环之外的 break 无处可跳(不兜底) + from lib.core.LLVMCG.FuncGen import FailFastError + raise FailFastError( + "break 只能用在循环或 match-case 体内(当前没有可跳出的目标)") + Gen.builder.branch(Gen.loop_break_targets[-1]) continue if isinstance(stmt, ast.Continue): - if Gen.loop_continue_targets: - Gen.builder.branch(Gen.loop_continue_targets[-1]) + if not Gen.loop_continue_targets: + from lib.core.LLVMCG.FuncGen import FailFastError + raise FailFastError( + "continue 只能用在循环体内(当前没有可继续的循环)") + Gen.builder.branch(Gen.loop_continue_targets[-1]) continue if isinstance(stmt, ast.Pass): continue diff --git a/lib/core/Handles/HandlesFor.py b/lib/core/Handles/HandlesFor.py index 1dff047..b9d698c 100644 --- a/lib/core/Handles/HandlesFor.py +++ b/lib/core/Handles/HandlesFor.py @@ -417,6 +417,8 @@ class ForHandle(BaseHandle): self.Trans.FunctionHandler._EmitFunctionLlvm(Node, Gen, extra_params=extra_params if extra_params else None) Gen.builder = saved_builder Gen.func = saved_func + Gen.loop_break_targets = saved_loop_break_targets + Gen.loop_continue_targets = saved_loop_continue_targets Gen.variables = saved_variables Gen._reg_values = saved_reg_values Gen.global_vars = saved_global_vars