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

@@ -1288,6 +1288,22 @@
12881288 if pred_id < 0
12891289 return ["", ""]
12901290 end
1291+ # AndNode unwrap: `v.is_a?(C) && other_cond` should still narrow
1292+ # `v` to C in the then-arm — when the if-predicate is an AndNode
1293+ # whose left or right operand is a recognizable is_a?, peel that
1294+ # operand out and re-parse. Conservative: don't narrow across
1295+ # OrNode (one branch only carries the constraint).
1296+ if @nd_type[pred_id] == "AndNode"
1297+ l_isa = parse_is_a_predicate(@nd_left[pred_id])
1298+ if l_isa[0] != ""
1299+ return l_isa
1300+ end
1301+ r_isa = parse_is_a_predicate(@nd_right[pred_id])
1302+ if r_isa[0] != ""
1303+ return r_isa
1304+ end
1305+ return ["", ""]
1306+ end
12911307 if @nd_type[pred_id] != "CallNode"
12921308 return ["", ""]
12931309 end