Commit f25fe0d0

f25fe0d072a4Merged

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

Description

fix(analyze,codegen): body-usage widen int|nil param to poly for length-like methods

Sam Ruby's #552: methods like .length / .size / .empty? that exist on String / Array / Hash but NOT on Integer were deliberately excluded from #544/#545's Option C body-usage classifier because the safe-only ones it added (push/pop/etc.) would have widened optcarrot's poke(data) shape where << overlaps with Integer bitshift. The length-like family doesn't collide with Integer; widening int/nil-defaulted params to flat poly (rather than the narrower poly_array) is sound and routes through the poly-recv dispatch.

Three pieces:

  1. is_lengthlike_only_method classifier (analyze): returns 1 for length / size / empty?. Methods that DO exist on Integer (<<, &, |, +, -, *, []) are excluded; the optcarrot regression mode this avoids is the same one #545's commit message called out.

  2. infer_param_lengthlike_widen pass (analyze): for each method's int/nil-defaulted param whose body calls a length-like method on the param, widen the slot to poly. Runs ONCE post-fixpoint after the narrower poly_array pass so concrete callers pin to int_array / str_array / etc. first; only genuinely-untyped params reach the flat-poly widening.

  3. emit_poly_builtin_dispatch arms (codegen): length / size / empty? arms for every Array variant (int / float / str / sym / ptr / poly) and every always-emitted Hash variant (str_int / str_str / int_str / str_poly / sym_poly / poly_poly). Plus SP_TAG_STR empty? arm in compile_poly_method_call to match the existing SP_TAG_STR length arm.

nil semantics: nil.length returns 0 (result temp default) rather than CRuby's NoMethodError. The test fixture pins this spinel-specific behavior, not CRuby reference output.

Verified: 522 tests pass, optcarrot checksum 59662 preserved (the load-bearing constraint that ruled out the broader ambiguous-method widening).

Closes #552

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
f25fe0d072a4
Parent
0f83cfc1d3a8

spinel_analyze.rbmodifiedView file

@@ -12060,6 +12060,117 @@
1206012060 end
1206112061 end
1206212062
12063+ # Methods that exist on String / Array / Hash but NOT on
12064+ # Integer. When body calls one of these on a param defaulted
12065+ # to int/nil, the param's actual runtime value can't be an
12066+ # int -- widen to poly so codegen's poly-recv dispatch
12067+ # handles the call across the possible String/Array/Hash
12068+ # storage. Issue #552. Conservative list: only methods that
12069+ # truly don't exist on Integer (so widening is sound and no
12070+ # caller breaks from a previously-int-typed arg path).
12071+ def is_lengthlike_only_method(mname)
12072+ if mname == "length" || mname == "size"
12073+ return 1
12074+ end
12075+ if mname == "empty?"
12076+ return 1
12077+ end
12078+ 0
12079+ end
12080+
12081+ # Body-usage widening for length-like methods. Sibling of
12082+ # infer_array_param_from_body but widens to a flat `poly`
12083+ # rather than `poly_array` -- the runtime value can be
12084+ # String, any Array variant, or any Hash variant, so the
12085+ # narrower poly_array would mis-shape the slot. Runs ONCE
12086+ # post-fixpoint. Issue #552.
12087+ def infer_param_lengthlike_widen
12088+ mi = 0
12089+ while mi < @meth_names.length
12090+ bid = @meth_body_ids[mi]
12091+ if bid >= 0
12092+ pnames = @meth_param_names[mi].split(",")
12093+ ptypes = @meth_param_types[mi].split(",")
12094+ changed_top = 0
12095+ pk = 0
12096+ while pk < pnames.length
12097+ if pk < ptypes.length && (ptypes[pk] == "int" || ptypes[pk] == "nil")
12098+ called = "".split(",")
12099+ collect_param_methods(bid, pnames[pk], called)
12100+ saw = 0
12101+ kk = 0
12102+ while kk < called.length
12103+ if is_lengthlike_only_method(called[kk]) == 1
12104+ saw = 1
12105+ end
12106+ kk = kk + 1
12107+ end
12108+ if saw == 1
12109+ @needs_rb_value = 1
12110+ ptypes[pk] = "poly"
12111+ changed_top = 1
12112+ end
12113+ end
12114+ pk = pk + 1
12115+ end
12116+ if changed_top == 1
12117+ @meth_param_types[mi] = ptypes.join(",")
12118+ end
12119+ end
12120+ mi = mi + 1
12121+ end
12122+ ci = 0
12123+ while ci < @cls_names.length
12124+ mnames = @cls_meth_names[ci].split(";")
12125+ bodies = @cls_meth_bodies[ci].split(";")
12126+ cls_changed = 0
12127+ mj = 0
12128+ while mj < mnames.length
12129+ if mnames[mj] != "initialize"
12130+ pnames_j = cls_meth_pnames_get(ci, mj)
12131+ ptypes_j = cls_meth_ptypes_get(ci, mj)
12132+ bid_j = -1
12133+ if mj < bodies.length
12134+ bid_j = bodies[mj].to_i
12135+ end
12136+ if bid_j >= 0
12137+ m_changed = 0
12138+ pk = 0
12139+ while pk < pnames_j.length
12140+ if pk < ptypes_j.length && (ptypes_j[pk] == "int" || ptypes_j[pk] == "nil")
12141+ called_c = "".split(",")
12142+ collect_param_methods(bid_j, pnames_j[pk], called_c)
12143+ sawc = 0
12144+ kk = 0
12145+ while kk < called_c.length
12146+ if is_lengthlike_only_method(called_c[kk]) == 1
12147+ sawc = 1
12148+ end
12149+ kk = kk + 1
12150+ end
12151+ if sawc == 1
12152+ @needs_rb_value = 1
12153+ ptypes_j[pk] = "poly"
12154+ m_changed = 1
12155+ end
12156+ end
12157+ pk = pk + 1
12158+ end
12159+ if m_changed == 1
12160+ cls_meth_ptypes_put(ci, mj, ptypes_j)
12161+ cls_changed = 1
12162+ end
12163+ end
12164+ end
12165+ mj = mj + 1
12166+ end
12167+ if cls_changed == 1
12168+ @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
12169+ end
12170+ ci = ci + 1
12171+ end
12172+ end
12173+
1206312174 def is_string_only_method(mname)
1206412175 if mname == "split" || mname == "start_with?" || mname == "end_with?"
1206512176 return 1
@@ -18376,6 +18487,19 @@
1837618487 infer_function_body_call_types
1837718488 infer_class_body_call_types
1837818489 infer_all_returns
18490+ # Body-usage length-like inference (#552). Conservative
18491+ # widening to flat `poly`: only fires on `.length` / `.size` /
18492+ # `.empty?` -- methods that exist on String / Array / Hash
18493+ # but NOT on Integer, so a body that calls them on a param
18494+ # proves the param's runtime value isn't an int. Stays after
18495+ # the array-param pass so the narrow variants
18496+ # (int_array / str_array / etc.) win first; only genuinely-
18497+ # untyped params reach the flat `poly` widening here.
18498+ infer_param_lengthlike_widen
18499+ infer_all_returns
18500+ infer_function_body_call_types
18501+ infer_class_body_call_types
18502+ infer_all_returns
1837918503 # Fix lambda return types based on call-site usage
1838018504 fix_lambda_return_types
1838118505 # Pre-detect bigint variables before feature detection