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:

  1. widen_returns_for_mixed_paths's class-instance-method arm was always falsely-skipping. The guard @unified_imeth_returns .index(tag) != nil was 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 != nil is always true and every class method was being skipped -- Host#stringify_keys(value) with return value if !value.is_a? (Hash); pointer_expr never got widened, the function declared a pointer return, and the early return 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.

  2. 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 != nil predicate intentionally -- in spinel that flips to always-true, which has been the working behavior for preserving concrete stored return types across infer_all_returns re-derivation. Leaving that consumer alone preserves the existing behavior; widen_returns_for_mixed_paths is 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

@@ -16196,7 +16196,7 @@
1619616196 # types skip this slot rather than reverting to the
1619716197 # raise-only "int" default.
1619816198 tag = ci.to_s + ":" + mi.to_s
16199- if @unified_imeth_returns.index(tag) == nil
16199+ if @unified_imeth_returns.include?(tag) == false
1620016200 @unified_imeth_returns.push(tag)
1620116201 end
1620216202 dci2 = 0
@@ -16210,7 +16210,7 @@
1621016210 @cls_meth_returns[dci2] = d_returns2.join(";")
1621116211 @cls_meth_return_cache = {}
1621216212 dtag = dci2.to_s + ":" + dmidx2.to_s
16213- if @unified_imeth_returns.index(dtag) == nil
16213+ if @unified_imeth_returns.include?(dtag) == false
1621416214 @unified_imeth_returns.push(dtag)
1621516215 end
1621616216 end
@@ -16367,15 +16367,21 @@
1636716367 # and silently re-widen to poly. Same `@unified_imeth_returns`
1636816368 # marker the imeth-unify pass uses.
1636916369 imeth_tag = ci.to_s + ":" + j.to_s
16370- if @unified_imeth_returns != nil && @unified_imeth_returns.index(imeth_tag) != nil
16371- @current_method_name = saved_meth
16372- j = j + 1
16373- next
16370+ # NOTE: use `.include?` (consistent bool in CRuby + spinel)
16371+ # rather than `.index(...) != nil`. spinel's Array#index
16372+ # returns -1 for not-found while CRuby returns nil, so
16373+ # `!= nil` is always true in spinel and the guard
16374+ # would false-positive on every method. Issue #585.
16375+ skip_this = 0
16376+ if @unified_imeth_returns != nil && @unified_imeth_returns.include?(imeth_tag)
16377+ skip_this = 1
1637416378 end
16375- if widen_check_with_scope(bid, cur_rt, pnames, ptypes) == 1
16376- returns[j] = "poly"
16377- changed = 1
16378- @needs_rb_value = 1
16379+ if skip_this == 0
16380+ if widen_check_with_scope(bid, cur_rt, pnames, ptypes) == 1
16381+ returns[j] = "poly"
16382+ changed = 1
16383+ @needs_rb_value = 1
16384+ end
1637916385 end
1638016386 @current_method_name = saved_meth
1638116387 end