fix(flow): 嵌套/协程函数体独立 break/continue 目标,越界即 fail-fast
- HandlesFor._HandleNestedFunctionLlvm:进入嵌套函数体前清空 loop_break_targets / loop_continue_targets,退出恢复——原实现会让嵌套函数体内的 break/continue 继承 外层函数的跳转目标(生成跨函数 br,非法 IR) - HandlesAsyncFunctions:协程体内循环之外的 break/continue 改为 FailFastError, 不再静默不生成跳转
This commit is contained in:
1 parent
34c4eeff02
commit
4353efd8ff
2 files changed
+13
-4
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user