Commit be9c264c

be9c264c5528Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

analyze,codegen: yield pads missing block params with nil

def foo; yield; end; foo { |a, b| a.inspect } printed 0/0 instead of nil/nil: block params the method's yield never fills were typed int and padded with 0.

Type a block param as int? when its index is >= the minimum arg count over all the method's yields (a bare yield passes 0). Min, not max, is the key: in a mixed-arity method (yield 1,2; yield 3) param 1 is filled by the first yield but nil on the second, so it must be nullable and each yield site's inline pad fills or nil-pads it — giving correct per-yield results without per-yield typing machinery. The pad now uses c_default_val so int? lands on SP_INT_NIL.

The earlier-deferred concern was that block-param int? widening would cascade like the *IntHash case (#801) and crash self-host. It doesn't: the widened params are genuinely never-filled on the relevant path, so no legitimate value flows through the sentinel. Bootstrap + 626 tests pass, optcarrot checksum 59662 unchanged. Refined the stale *IntHash comment that claimed the generic cascade.

Fixes #997

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

Details

Commit
be9c264c5528
Parent
65aacf420ed1

spinel_analyze.rbmodifiedView file

@@ -6008,15 +6008,16 @@
60086008 end
60096009 return tuple_elem_type_at(rt, 0)
60106010 end
6011- # Issue #833: *StrHash get now returns NULL for missing keys
6012- # (was sp_str_empty). *IntHash analyze widening to int? is
6013- # deferred -- body-usage widening cascades the change through
6014- # declare_var et al., crashing the self-host build. The current
6015- # change ships NULL for missing string keys (already nullable
6016- # C-side) and SP_INT_NIL for int keys at the runtime level;
6017- # int values surface as "int" at analyze, so `h[k].inspect`
6018- # of a missing int key prints INT64_MIN's decimal until the
6019- # follow-up phase that widens the LV slot.
6011+ # *StrHash get returns NULL for missing keys (was sp_str_empty).
6012+ # *IntHash `h[k]` still surfaces as "int" here, not "int?": every
6013+ # such read would widen, and unlike the always-nil block params in
6014+ # #997 (which are nullable on every path), a typed-int-hash read is
6015+ # usually a hit, so widening the whole slot to int? cascades
6016+ # int-sentinel arithmetic through legitimate values (the #801
6017+ # Phase 4 concern). So `h[k].inspect` of a *missing* int key prints
6018+ # INT64_MIN's decimal until that narrower follow-up lands. (Block
6019+ # params CAN be int? — see method_yield_min_argc — because there
6020+ # the slot is genuinely never filled.)
60206021 if rt == "str_int_hash"
60216022 return "int"
60226023 end
@@ -28202,6 +28203,15 @@
2820228203 ybid_628 = @meth_body_ids[mi_628]
2820328204 if ybid_628 >= 0
2820428205 arity_628 = method_yield_arity(mi_628)
28206+ # Block param past the number of args the method's `yield`
28207+ # actually passes is never filled; Ruby gives it nil. Type as
28208+ # int? so codegen pads with SP_INT_NIL and .inspect/.nil?
28209+ # dispatch correctly. Use the true argc (a bare `yield` passes
28210+ # 0, even though the C-signature arity floors to 1). Issue #997.
28211+ if bk >= method_yield_min_argc(mi_628)
28212+ types.push("int?")
28213+ pushed_block_param_628 = 1
28214+ end
2820528215 ytypes_628 = "".split(",", -1)
2820628216 ka_628 = 0
2820728217 while ka_628 < arity_628
@@ -29516,6 +29526,46 @@
2951629526 current
2951729527 end
2951829528
29529+ # The MIN arg count over all `yield`s in the method (a bare `yield`
29530+ # passes 0). A block param at index >= this is left nil by at least
29531+ # the smallest yield, so it must be nullable; the per-yield inline
29532+ # pad then fills it (for the larger yields) or nil-pads it (for the
29533+ # smaller). Using min, not max, makes mixed-arity methods
29534+ # (`yield 1,2; yield 3`) correct. Returns 0 if no yield is found
29535+ # (callers gate on @meth_has_yield). Issue #997.
29536+ def method_yield_min_argc(mi)
29537+ if mi < 0 || mi >= @meth_body_ids.length
29538+ return 0
29539+ end
29540+ r = body_min_yield_argc(@meth_body_ids[mi], -1)
29541+ r < 0 ? 0 : r
29542+ end
29543+ def body_min_yield_argc(nid, current)
29544+ if nid < 0
29545+ return current
29546+ end
29547+ if @nd_type[nid] == "YieldNode"
29548+ n = 0
29549+ if @nd_arguments[nid] >= 0
29550+ n = get_args(@nd_arguments[nid]).length
29551+ end
29552+ if current < 0 || n < current
29553+ current = n
29554+ end
29555+ end
29556+ if @nd_type[nid] == "DefNode"
29557+ return current
29558+ end
29559+ cs = []
29560+ push_child_ids(nid, cs)
29561+ k = 0
29562+ while k < cs.length
29563+ current = body_min_yield_argc(cs[k], current)
29564+ k = k + 1
29565+ end
29566+ current
29567+ end
29568+
2951929569 # ---- Return type inference ----
2952029570
2952129571 # Narrow pre-pass for `rewrite_instance_eval_calls`: walk top-level
@@ -30610,6 +30660,13 @@
3061030660 ybid_bp = @meth_body_ids[mi_bp]
3061130661 if ybid_bp >= 0
3061230662 arity_bp = method_yield_arity(mi_bp)
30663+ # Param past the number of args the method's `yield` actually
30664+ # passes is never filled; Ruby fills it with nil. Type int? so
30665+ # the pad uses SP_INT_NIL. Bare `yield` passes 0 args (the
30666+ # C-signature arity floors to 1). Issue #997. Mirrors scan_locals.
30667+ if pi >= method_yield_min_argc(mi_bp)
30668+ return "int?"
30669+ end
3061330670 ytypes_bp = "".split(",", -1)
3061430671 ka_bp = 0
3061530672 while ka_bp < arity_bp
@@ -30647,6 +30704,13 @@
3064730704 ybid_bp = @meth_body_ids[mi_bp]
3064830705 if ybid_bp >= 0
3064930706 arity_bp = method_yield_arity(mi_bp)
30707+ # Param past the number of args the method's `yield` actually
30708+ # passes is never filled; Ruby fills it with nil. Type int? so
30709+ # the pad uses SP_INT_NIL. Bare `yield` passes 0 args (the
30710+ # C-signature arity floors to 1). Issue #997. Mirrors scan_locals.
30711+ if pi >= method_yield_min_argc(mi_bp)
30712+ return "int?"
30713+ end
3065030714 ytypes_bp = "".split(",", -1)
3065130715 ka_bp = 0
3065230716 while ka_bp < arity_bp