Commit 09c88133

09c88133b198Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): yield returns block value in inlined method body

def f; yield; end; f { 99 } returned 0 instead of 99. The compile_yield_call_expr path inlines f's body; when the last stmt is YieldNode it was routed through the generic stmt-shaped path that drops the block body's value into a dead 99LL; expression and leaves the result temp at its int default.

Three coordinated changes:

  1. compile_yield_call_expr's last-stmt dispatch: when the last stmt is YieldNode, use a new compile_yield_inline_capture helper that emits the block-param assignment, walks the block body's non-last stmts, and captures the last expr into result_tmp.

  2. New @inline_yield_blk / @inline_yield_bp_names ivars carry the block context through the inlining window (set in compile_yield_call_expr and compile_yield_call_stmt, cleared on exit).

  3. compile_expr and compile_expr_remap grow a YieldNode arm that fires when @inline_yield_blk >= 0. Materializes the block body's last value into a temp and returns the temp expression. This catches yield-as-sub-expression shapes that previously emitted 0: arr.push(yield), yield * 2, and the ActiveRecord ConnectionPool factory pattern in #693.

Fixes #693

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
09c88133b198
Parent
1a848867956f

spinel_codegen.rbmodifiedView file

@@ -647,6 +647,14 @@
647647 @inline_rename_map_from = nil
648648 @inline_rename_map_to = nil
649649
650+ # When inlining a yield-using method body in expression context,
651+ # any YieldNode encountered as a sub-expression (e.g. `arr.push(yield)`,
652+ # `yield * 2`) needs to materialize the block body's last value into
653+ # a temp. These fields carry the necessary block context through
654+ # compile_expr_remap. `_blk` is -1 when not inlining.
655+ @inline_yield_blk = -1
656+ @inline_yield_bp_names = nil
657+
650658 # Symbol type Phase 2 Step 1: intern table (infrastructure only; unused yet).
651659 @sym_names = "".split(",")
652660
@@ -4044,6 +4052,9 @@
40444052 if t == "proc"
40454053 return 1
40464054 end
4055+ if t == "exception"
4056+ return 1
4057+ end
40474058 if is_obj_type(t) == 1
40484059 cname = t[4, t.length - 4]
40494060 ci = find_class_idx(cname)
@@ -12345,6 +12356,16 @@
1234512356 return "0"
1234612357 end
1234712358 t = @nd_type[nid]
12359+ if t == "YieldNode" && @inline_yield_blk >= 0
12360+ # Yield used as a sub-expression inside an inlined yield-using
12361+ # method body (`arr.push(yield)`, `yield * 2`, etc.). Materialize
12362+ # the block body's last value into a temp and return that temp.
12363+ # The @inline_yield_blk guard restricts this expansion to the
12364+ # inlining window; outside of it, YieldNode in expression context
12365+ # is meaningless and falls through to the default int-0.
12366+ empty_map = "".split(",")
12367+ return compile_yield_inline_expr(nid, @inline_yield_blk, @inline_yield_bp_names, empty_map, empty_map)
12368+ end
1234812369 if t == "UnsupportedNode"
1234912370 # The parser emitted this sentinel because it hit a Prism node
1235012371 # type it doesn't know how to serialize. Refusing to compile is
@@ -38122,6 +38143,10 @@
3812238143 saved_in_rmt = @inline_rename_map_to
3812338144 @inline_rename_map_from = param_map_from
3812438145 @inline_rename_map_to = param_map_to
38146+ saved_iyb = @inline_yield_blk
38147+ saved_iybp = @inline_yield_bp_names
38148+ @inline_yield_blk = blk
38149+ @inline_yield_bp_names = bp_names
3812538150 if bid >= 0
3812638151 stmts = get_stmts(bid)
3812738152 k = 0
@@ -38132,6 +38157,8 @@
3813238157 end
3813338158 @inline_rename_map_from = saved_in_rmf
3813438159 @inline_rename_map_to = saved_in_rmt
38160+ @inline_yield_blk = saved_iyb
38161+ @inline_yield_bp_names = saved_iybp
3813538162 end
3813638163
3813738164 # expression-context counterpart of compile_yield_call_stmt.
@@ -38226,6 +38253,10 @@
3822638253 saved_in_rmt = @inline_rename_map_to
3822738254 @inline_rename_map_from = param_map_from
3822838255 @inline_rename_map_to = param_map_to
38256+ saved_iyb = @inline_yield_blk
38257+ saved_iybp = @inline_yield_bp_names
38258+ @inline_yield_blk = blk
38259+ @inline_yield_bp_names = bp_names
3822938260 if bid >= 0
3823038261 stmts = get_stmts(bid)
3823138262 n = stmts.length
@@ -38237,7 +38268,13 @@
3823738268 if n > 0
3823838269 last_y = stmts.last
3823938270 last_t = @nd_type[last_y]
38240- if last_t == "YieldNode" || last_t == "IfNode" || last_t == "CaseNode" ||
38271+ if last_t == "YieldNode"
38272+ # `def f; yield; end` shape: the yield IS the return value. The
38273+ # generic stmt-shaped path would drop the block body's value;
38274+ # inline the yield and capture the block body's last expr into
38275+ # result_tmp so `f { 99 }` returns 99 instead of 0.
38276+ compile_yield_inline_capture(last_y, blk, bp_names, param_map_from, param_map_to, result_tmp, rt_yc)
38277+ elsif last_t == "IfNode" || last_t == "CaseNode" ||
3824138278 last_t == "WhileNode" || last_t == "BeginNode" || last_t == "ReturnNode" ||
3824238279 last_t == "LocalVariableWriteNode" || last_t == "InstanceVariableWriteNode" ||
3824338280 last_t == "ClassVariableWriteNode" || last_t == "GlobalVariableWriteNode"
@@ -38261,9 +38298,156 @@
3826138298 end
3826238299 @inline_rename_map_from = saved_in_rmf
3826338300 @inline_rename_map_to = saved_in_rmt
38301+ @inline_yield_blk = saved_iyb
38302+ @inline_yield_bp_names = saved_iybp
3826438303 result_tmp
3826538304 end
3826638305
38306+ # Yield in expression position (as a sub-expression of any
38307+ # statement / expression in the inlined body). Returns the C
38308+ # expression that holds the block body's last value. Emits the
38309+ # block-param assignment and the block body's non-last stmts as
38310+ # side effects.
38311+ def compile_yield_inline_expr(yield_nid, blk, bp_names, map_from, map_to)
38312+ args_id_ye = @nd_arguments[yield_nid]
38313+ assigned_ye = 0
38314+ if args_id_ye >= 0
38315+ aids_ye = get_args(args_id_ye)
38316+ kye = 0
38317+ while kye < aids_ye.length
38318+ if kye < bp_names.length
38319+ bp_t_ye = find_var_type(bp_names[kye])
38320+ arg_v_ye = compile_expr_remap(aids_ye[kye], map_from, map_to)
38321+ arg_t_ye = infer_type(aids_ye[kye])
38322+ arg_emit_bigint_ye = expr_emit_is_bigint(aids_ye[kye]) == 1
38323+ if bp_t_ye == "bigint" && arg_t_ye == "int" && !arg_emit_bigint_ye
38324+ @needs_bigint = 1
38325+ arg_v_ye = "sp_bigint_new_int(" + arg_v_ye + ")"
38326+ elsif bp_t_ye == "int" && (arg_t_ye == "bigint" || arg_emit_bigint_ye)
38327+ @needs_bigint = 1
38328+ arg_v_ye = "sp_bigint_to_int((sp_Bigint *)" + arg_v_ye + ")"
38329+ end
38330+ emit(" lv_" + bp_names[kye] + " = " + arg_v_ye + ";")
38331+ assigned_ye = assigned_ye + 1
38332+ end
38333+ kye = kye + 1
38334+ end
38335+ end
38336+ while assigned_ye < bp_names.length
38337+ bp_rt_ye = find_var_type(bp_names[assigned_ye])
38338+ if bp_rt_ye == "bigint"
38339+ @needs_bigint = 1
38340+ emit(" lv_" + bp_names[assigned_ye] + " = sp_bigint_new_int(0);")
38341+ else
38342+ emit(" lv_" + bp_names[assigned_ye] + " = 0;")
38343+ end
38344+ assigned_ye = assigned_ye + 1
38345+ end
38346+ body_ye = @nd_body[blk]
38347+ if body_ye < 0
38348+ return "0"
38349+ end
38350+ bstmts_ye = get_stmts(body_ye)
38351+ bn_ye = bstmts_ye.length
38352+ if bn_ye == 0
38353+ return "0"
38354+ end
38355+ bk_ye = 0
38356+ while bk_ye < bn_ye - 1
38357+ compile_stmt(bstmts_ye[bk_ye])
38358+ bk_ye = bk_ye + 1
38359+ end
38360+ last_be = bstmts_ye.last
38361+ last_bet = @nd_type[last_be]
38362+ if last_bet == "IfNode" || last_bet == "CaseNode" ||
38363+ last_bet == "WhileNode" || last_bet == "BeginNode" || last_bet == "ReturnNode" ||
38364+ last_bet == "LocalVariableWriteNode" || last_bet == "InstanceVariableWriteNode" ||
38365+ last_bet == "ClassVariableWriteNode" || last_bet == "GlobalVariableWriteNode"
38366+ compile_stmt(last_be)
38367+ return "0"
38368+ end
38369+ last_be_t = infer_type(last_be)
38370+ res_ye = new_temp
38371+ emit(" " + c_type(last_be_t) + " " + res_ye + " = " + compile_expr(last_be) + ";")
38372+ res_ye
38373+ end
38374+
38375+ # Yield in expression position when it is the inlined method body's
38376+ # last statement: emit the block-param assignment for the yielded
38377+ # args (same as the YieldNode arm in compile_stmt_with_block), then
38378+ # walk the block body emitting each stmt except the last as a stmt,
38379+ # and capture the last expression into `result_tmp`. Coerces the
38380+ # captured value if rt_yc is bigint and the expr is plain int (and
38381+ # the inverse), matching the coercion the expression-shaped branch
38382+ # of compile_yield_call_expr already does.
38383+ def compile_yield_inline_capture(yield_nid, blk, bp_names, map_from, map_to, result_tmp, rt_yc)
38384+ args_id_yic = @nd_arguments[yield_nid]
38385+ assigned_yic = 0
38386+ if args_id_yic >= 0
38387+ aids_yic = get_args(args_id_yic)
38388+ kyic = 0
38389+ while kyic < aids_yic.length
38390+ if kyic < bp_names.length
38391+ bp_t_yic = find_var_type(bp_names[kyic])
38392+ arg_v_yic = compile_expr_remap(aids_yic[kyic], map_from, map_to)
38393+ arg_t_yic = infer_type(aids_yic[kyic])
38394+ arg_emit_bigint_yic = expr_emit_is_bigint(aids_yic[kyic]) == 1
38395+ if bp_t_yic == "bigint" && arg_t_yic == "int" && !arg_emit_bigint_yic
38396+ @needs_bigint = 1
38397+ arg_v_yic = "sp_bigint_new_int(" + arg_v_yic + ")"
38398+ elsif bp_t_yic == "int" && (arg_t_yic == "bigint" || arg_emit_bigint_yic)
38399+ @needs_bigint = 1
38400+ arg_v_yic = "sp_bigint_to_int((sp_Bigint *)" + arg_v_yic + ")"
38401+ end
38402+ emit(" lv_" + bp_names[kyic] + " = " + arg_v_yic + ";")
38403+ assigned_yic = assigned_yic + 1
38404+ end
38405+ kyic = kyic + 1
38406+ end
38407+ end
38408+ while assigned_yic < bp_names.length
38409+ bp_rt_yic = find_var_type(bp_names[assigned_yic])
38410+ if bp_rt_yic == "bigint"
38411+ @needs_bigint = 1
38412+ emit(" lv_" + bp_names[assigned_yic] + " = sp_bigint_new_int(0);")
38413+ else
38414+ emit(" lv_" + bp_names[assigned_yic] + " = 0;")
38415+ end
38416+ assigned_yic = assigned_yic + 1
38417+ end
38418+ body_yic = @nd_body[blk]
38419+ if body_yic >= 0
38420+ bstmts = get_stmts(body_yic)
38421+ bn = bstmts.length
38422+ bk = 0
38423+ while bk < bn - 1
38424+ compile_stmt(bstmts[bk])
38425+ bk = bk + 1
38426+ end
38427+ if bn > 0
38428+ last_b = bstmts.last
38429+ last_bt = @nd_type[last_b]
38430+ if last_bt == "IfNode" || last_bt == "CaseNode" ||
38431+ last_bt == "WhileNode" || last_bt == "BeginNode" || last_bt == "ReturnNode" ||
38432+ last_bt == "LocalVariableWriteNode" || last_bt == "InstanceVariableWriteNode" ||
38433+ last_bt == "ClassVariableWriteNode" || last_bt == "GlobalVariableWriteNode"
38434+ compile_stmt(last_b)
38435+ else
38436+ last_v_yic = compile_expr(last_b)
38437+ last_et = infer_type(last_b)
38438+ if base_type(rt_yc) == "bigint" && last_et == "int"
38439+ @needs_bigint = 1
38440+ last_v_yic = "sp_bigint_new_int(" + last_v_yic + ")"
38441+ elsif base_type(rt_yc) == "int" && last_et == "bigint"
38442+ @needs_bigint = 1
38443+ last_v_yic = "sp_bigint_to_int((sp_Bigint *)" + last_v_yic + ")"
38444+ end
38445+ emit(" " + result_tmp + " = " + last_v_yic + ";")
38446+ end
38447+ end
38448+ end
38449+ end
38450+
3826738451 def compile_stmt_with_block(nid, blk, bp_names, map_from, map_to)
3826838452 if nid < 0
3826938453 return
@@ -38569,6 +38753,14 @@
3856938753 return "0"
3857038754 end
3857138755 t = @nd_type[nid]
38756+ if t == "YieldNode" && @inline_yield_blk >= 0
38757+ # Yield used as a sub-expression inside an inlined yield-using
38758+ # method body (e.g. `arr.push(yield)`, `yield * 2`). Pre-emit the
38759+ # block-param assignment + the block body's non-last stmts, then
38760+ # capture the block body's last expr into a temp and return that
38761+ # temp. compile_yield_inline_expr handles all the bookkeeping.
38762+ return compile_yield_inline_expr(nid, @inline_yield_blk, @inline_yield_bp_names, map_from, map_to)
38763+ end
3857238764 if t == "LocalVariableReadNode"
3857338765 rname = remap_local(@nd_name[nid], map_from, map_to)
3857438766 # Mirror compile_expr's LocalVariableReadNode unbox arm so a
@@ -40118,7 +40310,11 @@
4011840310 # type) to bigint at analyze time, so a literal/int-typed
4011940311 # fall-through last expression must be wrapped before return.
4012040312 # The reverse direction covers callers that still want mrb_int.
40121- emit(" return " + maybe_bigint_coerce(val, expr_type, return_type) + ";")
40313+ # When the body raises (sets @needs_setjmp) the LV slot may be
40314+ # declared `volatile T *`; the non-volatile return signature
40315+ # rejects without an explicit cast. cast_away_volatile_arg
40316+ # detects this shape and emits the `(T *)` strip.
40317+ emit(" return " + maybe_bigint_coerce(cast_away_volatile_arg(last, val), expr_type, return_type) + ";")
4012240318 end
4012340319 else
4012440320 compile_stmt(last)