Commit 77845db9
77845db946abMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): is_a? narrow propagates through AndNode predicates
Step 3 narrow extension. if v.is_a?(C) && other_cond previously
lost the narrow on v inside the then-arm because
parse_is_a_predicate only matched a CallNode predicate directly;
an AndNode wrapping was rejected and the empty result left
scan_new_calls' narrow stack untouched. The downstream effect:
call sites inside the then-arm widened the callee param with
the unnarrowed type (typically poly) instead of the asserted
concrete type, leaving the C compile to emit boxed-poly args
into a const char * slot via .v.s unbox at the call site,
which forces the analyze-side to keep the param wider than it
should be.
Fix: when parse_is_a_predicate sees an AndNode, recurse on the left operand first, then the right. Conservative — doesn't peek into OrNode (only one branch of an OR carries the constraint, so inferring across the join would over-narrow). Symmetric for scan_cls_method_calls' and collect_return_types' callers that share the same parser.
Validation: tests 453 pass (+1 regression for the
if v.is_a?(String) && allow shape), bench 56/56, bootstrap
4-way fixpoint OK.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 77845db946ab
- Parent
- 09556c2b9d1f
spinel_analyze.rbmodifiedView file
if pred_id < 0 return ["", ""] end # AndNode unwrap: `v.is_a?(C) && other_cond` should still narrow # `v` to C in the then-arm — when the if-predicate is an AndNode # whose left or right operand is a recognizable is_a?, peel that # operand out and re-parse. Conservative: don't narrow across # OrNode (one branch only carries the constraint). if @nd_type[pred_id] == "AndNode" l_isa = parse_is_a_predicate(@nd_left[pred_id]) if l_isa[0] != "" return l_isa end r_isa = parse_is_a_predicate(@nd_right[pred_id]) if r_isa[0] != "" return r_isa end return ["", ""] end if @nd_type[pred_id] != "CallNode" return ["", ""] endtest/narrow_and_predicate.rbaddedView file
Loading diff…
test/narrow_and_predicate.rb.expectedaddedView file
Loading diff…