Commit 517121c0
517121c02621Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): widen class-method returns + fix index-vs-nil idiom
Two intertwined fixes for #585:
-
widen_returns_for_mixed_paths's class-instance-method arm was always falsely-skipping. The guard@unified_imeth_returns .index(tag) != nilwas meant to skip slots already promoted by the imeth-family unify pass (so the #563 raise-stub + subclass-override family stays intact). spinel's Array#index returns -1 for not-found (CRuby returns nil), so-1 != nilis always true and every class method was being skipped --Host#stringify_keys(value)withreturn value if !value.is_a? (Hash); pointer_exprnever got widened, the function declared a pointer return, and the earlyreturn value(int) tried to return mrb_int from a pointer-typed function. Switch the producer (unify_imeth_override_returns) and the consumer (the widen-pass skip guard) to.include?which is bool-consistent in both runtimes. -
The producer-side fix has the side effect of actually populating @unified_imeth_returns in self-host (previously the tag-push was always-false-gated, so the marker stayed empty).
propagate_unified_returns_to_callers's consumer at line ~16759 still uses the inverted.index != nilpredicate intentionally -- in spinel that flips to always-true, which has been the working behavior for preserving concrete stored return types acrossinfer_all_returnsre-derivation. Leaving that consumer alone preserves the existing behavior;widen_returns_for_mixed_pathsis the only new consumer of the marker.
The #585 OP's full repro (result["y"] = Host.new.stringify_keys (42) where result is a str_int_hash) still fails at the call
site -- the hash slot's local widening doesn't observe the
poly-returning call. That's a separate gap (local hash type
widening on assignment of a poly value); the test fixture here
uses the simpler puts ... shape to verify the function-signature
side that #585 reported.
Fixes #585 (function-signature side).
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 517121c02621
- Parent
- fbb4b8174aac
spinel_analyze.rbmodifiedView file
# types skip this slot rather than reverting to the # raise-only "int" default. tag = ci.to_s + ":" + mi.to_s if @unified_imeth_returns.index(tag) == nil if @unified_imeth_returns.include?(tag) == false @unified_imeth_returns.push(tag) end dci2 = 0 @cls_meth_returns[dci2] = d_returns2.join(";") @cls_meth_return_cache = {} dtag = dci2.to_s + ":" + dmidx2.to_s if @unified_imeth_returns.index(dtag) == nil if @unified_imeth_returns.include?(dtag) == false @unified_imeth_returns.push(dtag) end end # and silently re-widen to poly. Same `@unified_imeth_returns` # marker the imeth-unify pass uses. imeth_tag = ci.to_s + ":" + j.to_s if @unified_imeth_returns != nil && @unified_imeth_returns.index(imeth_tag) != nil @current_method_name = saved_meth j = j + 1 next # NOTE: use `.include?` (consistent bool in CRuby + spinel) # rather than `.index(...) != nil`. spinel's Array#index # returns -1 for not-found while CRuby returns nil, so # `!= nil` is always true in spinel and the guard # would false-positive on every method. Issue #585. skip_this = 0 if @unified_imeth_returns != nil && @unified_imeth_returns.include?(imeth_tag) skip_this = 1 end if widen_check_with_scope(bid, cur_rt, pnames, ptypes) == 1 returns[j] = "poly" changed = 1 @needs_rb_value = 1 if skip_this == 0 if widen_check_with_scope(bid, cur_rt, pnames, ptypes) == 1 returns[j] = "poly" changed = 1 @needs_rb_value = 1 end end @current_method_name = saved_meth endtest/return_unify_isa_early_return.rbaddedView file
Loading diff…
test/return_unify_isa_early_return.rb.expectedaddedView file
Loading diff…