Commit 0a69b225
0a69b225ac5dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 13 files · +12 added · ~1 modified
Description
Merge pull request #360 from mame/pr/ensure-runs-on-return
fix(codegen): run ensure clause on return and raise exits
Details
- Commit
- 0a69b225ac5d
- Parent
- 90214e2fa8d5
- Merge parent
- fa5a85870694
spinel_codegen.rbmodifiedView file
@@ -309,6 +309,23 @@
309309
@rescue_cls_stack = "".split(",")310310
@rescue_msg_stack = "".split(",")311311
@rescue_depth = 0312+
# Stack of ensure-clause node IDs (encoded as strings) currently in313+
# scope. Each entry corresponds to an enclosing `begin..ensure..end`314+
# whose body is being compiled. When `return` is emitted from inside315+
# the body, each ensure body is replayed (innermost-first) before316+
# the C `return`, so writebacks in `ensure` execute on early return.317+
@ensure_stack = "".split(",")318+
# Number of `sp_exc_top++` pushes emitted along the static319+
# fall-through path leading to the current emit point but not320+
# yet matched by an emitted `sp_exc_top--`. An early `return`321+
# emits `sp_exc_top -= N` to balance them, so the caller doesn't322+
# longjmp into our stale stack frame after we've returned.323+
@setjmp_depth = 0324+
# Counter used to mint unique snapshot variable names325+
# (`_ensure_cls_<n>`, `_ensure_msg_<n>`) for re-raising the326+
# in-flight exception after an ensure body runs on the327+
# exception path of a `begin..ensure..end`.328+
@ensure_emit_depth = 0312329
# Exception variable bindings: parallel stacks of (var_name, cls_var).313330
# A `rescue => e` binds `e` to the message string and registers it314331
# here so that `e.message`, `e.class`, `e.to_s`, and `e.inspect`@@ -8153,6 +8170,12 @@
81538170
scan_ivars(ci, elems[k])81548171
k = k + 181558172
end8173+
if @nd_rescue_clause[nid] >= 08174+
scan_ivars(ci, @nd_rescue_clause[nid])8175+
end8176+
if @nd_ensure_clause[nid] >= 08177+
scan_ivars(ci, @nd_ensure_clause[nid])8178+
end81568179
end81578180
81588181
def infer_ivar_init_type(nid)@@ -30804,6 +30827,14 @@
3080430827
emit(" " + arr_tmp + "->_" + k.to_s + " = " + compile_expr(arg_ids[k]) + ";")3080530828
k = k + 13080630829
end30830+
# Replay any in-scope `ensure` bodies before the return.30831+
# Ruby semantics: return value is already materialized, ensure30832+
# may mutate locals/ivars but does not affect the saved value.30833+
# `sp_exc_top -= N` undoes the setjmps we pushed; emit it once30834+
# before the replays so a nested `return` inside an ensure30835+
# body doesn't double-pop.30836+
emit_setjmp_depth_unwind30837+
emit_ensure_replays3080730838
if @in_gc_scope == 13080830839
emit(" SP_GC_RESTORE();")3080930840
end@@ -30811,21 +30842,43 @@
3081130842
return3081230843
end3081330844
if arg_ids.length > 030845+
# `pre_return_unwind` = needs `sp_exc_top -= N;` and/or30846+
# ensure-body replays. When neither, we can leave the30847+
# original simple `return <expr>;` shape alone.30848+
pre_return_unwind = 030849+
if @ensure_stack.length > 030850+
pre_return_unwind = 130851+
end30852+
if @setjmp_depth > 030853+
pre_return_unwind = 130854+
end3081430855
if @current_method_return == "poly"3081530856
ret_expr = box_expr_to_poly(arg_ids[0])3081630857
if @in_gc_scope == 13081730858
tmp = new_temp3081830859
emit(" sp_RbVal " + tmp + " = " + ret_expr + ";")30860+
emit_setjmp_depth_unwind30861+
emit_ensure_replays3081930862
emit(" SP_GC_RESTORE();")3082030863
emit(" return " + tmp + ";")3082130864
else30822-
emit(" return " + ret_expr + ";")30865+
if pre_return_unwind == 130866+
tmp = new_temp30867+
emit(" sp_RbVal " + tmp + " = " + ret_expr + ";")30868+
emit_setjmp_depth_unwind30869+
emit_ensure_replays30870+
emit(" return " + tmp + ";")30871+
else30872+
emit(" return " + ret_expr + ";")30873+
end3082330874
end3082430875
return3082530876
end3082630877
rt = infer_type(arg_ids[0])3082730878
# return nil in a nullable pointer method → return NULL3082830879
if rt == "nil" && is_nullable_pointer_type(@current_method_return) == 130880+
emit_setjmp_depth_unwind30881+
emit_ensure_replays3082930882
if @in_gc_scope == 13083030883
emit(" SP_GC_RESTORE();")3083130884
end@@ -30833,13 +30886,24 @@
3083330886
return3083430887
end3083530888
if @in_gc_scope == 130836-
# Save return value, restore GC, then return30889+
# Save return value, run ensure replays (which may mutate30890+
# ivars/locals), restore GC, then return the saved value.3083730891
tmp = new_temp3083830892
emit(" " + c_type(rt) + " " + tmp + " = " + compile_expr(arg_ids[0]) + ";")30893+
emit_setjmp_depth_unwind30894+
emit_ensure_replays3083930895
emit(" SP_GC_RESTORE();")3084030896
emit(" return " + tmp + ";")3084130897
else30842-
emit(" return " + compile_expr(arg_ids[0]) + ";")30898+
if pre_return_unwind == 130899+
tmp = new_temp30900+
emit(" " + c_type(rt) + " " + tmp + " = " + compile_expr(arg_ids[0]) + ";")30901+
emit_setjmp_depth_unwind30902+
emit_ensure_replays30903+
emit(" return " + tmp + ";")30904+
else30905+
emit(" return " + compile_expr(arg_ids[0]) + ";")30906+
end3084330907
end3084430908
return3084530909
end@@ -30858,6 +30922,8 @@
3085830922
# NULLed the instance on the early-return branch; in the void3085930923
# `sp_<C>_initialize` form it was a `void function should not3086030924
# return a value` C error.30925+
emit_setjmp_depth_unwind30926+
emit_ensure_replays3086130927
if @in_gc_scope == 13086230928
emit(" SP_GC_RESTORE();")3086330929
end@@ -36431,6 +36497,24 @@
3643136497
end3643236498
end3643336499
36500+
# Outer setjmp wraps the body (and any rescue chain) so the ensure36501+
# clause runs on the exception path too. Without this, a `raise`36502+
# from inside the body would unwind past us with `ensure` skipped.36503+
ensure_cls_var = ""36504+
ensure_msg_var = ""36505+
if has_ensure36506+
@ensure_emit_depth = @ensure_emit_depth + 136507+
ensure_cls_var = "_ensure_cls_" + @ensure_emit_depth.to_s36508+
ensure_msg_var = "_ensure_msg_" + @ensure_emit_depth.to_s36509+
emit(" const char *" + ensure_cls_var + " = (const char *)0;")36510+
emit(" const char *" + ensure_msg_var + " = (const char *)0;")36511+
emit(" sp_exc_top++;")36512+
emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")36513+
@indent = @indent + 136514+
@setjmp_depth = @setjmp_depth + 136515+
@ensure_stack.push(@nd_ensure_clause[nid].to_s)36516+
end36517+
3643436518
if has_retry == 13643536519
emit(" for (;;) {")3643636520
@indent = @indent + 1@@ -36438,6 +36522,7 @@
3643836522
3643936523
if has_rescue3644036524
emit(" sp_exc_top++;")36525+
@setjmp_depth = @setjmp_depth + 13644136526
emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")3644236527
@indent = @indent + 13644336528
compile_stmts_body(@nd_body[nid])@@ -36457,6 +36542,7 @@
3645736542
@indent = @indent - 13645836543
end3645936544
emit(" }")36545+
@setjmp_depth = @setjmp_depth - 13646036546
else3646136547
compile_stmts_body(@nd_body[nid])3646236548
end@@ -36467,10 +36553,77 @@
3646736553
end3646836554
3646936555
if has_ensure36556+
@ensure_stack.pop36557+
# End of the if-success branch: pop our setjmp and close the36558+
# brace. After this the codegen is conceptually back at the36559+
# surrounding scope — drop the depth bump even though more36560+
# code (the else branch and fall-through ensure) is still36561+
# being emitted; both run with sp_exc_top already restored.36562+
emit(" sp_exc_top--;")36563+
@indent = @indent - 136564+
@setjmp_depth = @setjmp_depth - 136565+
emit(" } else {")36566+
@indent = @indent + 136567+
# Exception path: pop our setjmp, snapshot the in-flight36568+
# exception (because nested raises inside the ensure body36569+
# would clobber sp_last_exc_cls / sp_exc_msg), run the36570+
# ensure body, then re-raise so the exception keeps36571+
# propagating. If the ensure body contains its own `return`,36572+
# the re-raise becomes dead code — that's the explicit36573+
# "ensure overrides exception with return" semantics.36574+
emit(" sp_exc_top--;")36575+
emit(" " + ensure_cls_var + " = (const char *)sp_last_exc_cls;")36576+
emit(" " + ensure_msg_var + " = sp_exc_msg[sp_exc_top];")3647036577
ec = @nd_ensure_clause[nid]3647136578
if ec >= 03647236579
compile_stmts_body(@nd_body[ec])3647336580
end36581+
emit(" sp_raise_cls(" + ensure_cls_var + ", " + ensure_msg_var + ");")36582+
@indent = @indent - 136583+
emit(" }")36584+
# Normal-path ensure (fall-through after success branch).36585+
if ec >= 036586+
compile_stmts_body(@nd_body[ec])36587+
end36588+
@ensure_emit_depth = @ensure_emit_depth - 136589+
end36590+
end36591+
36592+
# Replay all in-scope ensure bodies inline (innermost-first) ahead36593+
# of an early-exit `return`. Each ensure is popped from the stack36594+
# *before* its body is emitted, so a nested `return` inside the36595+
# ensure body sees only the *outer* ensures still active and36596+
# doesn't replay the same ensure recursively. Stack is restored36597+
# afterwards so the caller continues with the same view.36598+
#36599+
# @setjmp_depth is stashed to 0 during the replay because the36600+
# caller is responsible for emitting `sp_exc_top -= N` *once*36601+
# before the replays — a nested `return` inside an ensure body36602+
# would otherwise re-emit that decrement and over-pop the stack.36603+
def emit_ensure_replays36604+
if @ensure_stack.length == 036605+
return36606+
end36607+
saved_setjmp_depth = @setjmp_depth36608+
@setjmp_depth = 036609+
popped = "".split(",")36610+
while @ensure_stack.length > 036611+
ec_str = @ensure_stack.pop36612+
popped.push(ec_str)36613+
compile_stmts_body(@nd_body[ec_str.to_i])36614+
end36615+
while popped.length > 036616+
@ensure_stack.push(popped.pop)36617+
end36618+
@setjmp_depth = saved_setjmp_depth36619+
end36620+
36621+
# Emit the `sp_exc_top -= N;` that an early `return` needs in36622+
# order to leave sp_exc_top balanced. Called immediately before36623+
# `emit_ensure_replays` at every `return` emission site.36624+
def emit_setjmp_depth_unwind36625+
if @setjmp_depth > 036626+
emit(" sp_exc_top -= " + @setjmp_depth.to_s + ";")3647436627
end3647536628
end3647636629
test/ensure_raise_overrides_body_raise.rbaddedView file
Loading diff…
test/ensure_raise_overrides_body_raise.rb.expectedaddedView file
Loading diff…
test/ensure_runs_on_raise.rbaddedView file
Loading diff…
test/ensure_runs_on_raise.rb.expectedaddedView file
Loading diff…
test/ensure_runs_on_raise_nested.rbaddedView file
Loading diff…
test/ensure_runs_on_raise_nested.rb.expectedaddedView file
Loading diff…
test/ensure_runs_on_return.rbaddedView file
Loading diff…
test/ensure_runs_on_return.rb.expectedaddedView file
Loading diff…
test/ensure_runs_on_return_from_rescue.rbaddedView file
Loading diff…
test/ensure_runs_on_return_from_rescue.rb.expectedaddedView file
Loading diff…
test/ensure_runs_on_return_nested.rbaddedView file
Loading diff…
test/ensure_runs_on_return_nested.rb.expectedaddedView file
Loading diff…