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:
-
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. -
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. -
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
end end # Methods that exist on String / Array / Hash but NOT on # Integer. When body calls one of these on a param defaulted # to int/nil, the param's actual runtime value can't be an # int -- widen to poly so codegen's poly-recv dispatch # handles the call across the possible String/Array/Hash # storage. Issue #552. Conservative list: only methods that # truly don't exist on Integer (so widening is sound and no # caller breaks from a previously-int-typed arg path). def is_lengthlike_only_method(mname) if mname == "length" || mname == "size" return 1 end if mname == "empty?" return 1 end 0 end # Body-usage widening for length-like methods. Sibling of # infer_array_param_from_body but widens to a flat `poly` # rather than `poly_array` -- the runtime value can be # String, any Array variant, or any Hash variant, so the # narrower poly_array would mis-shape the slot. Runs ONCE # post-fixpoint. Issue #552. def infer_param_lengthlike_widen mi = 0 while mi < @meth_names.length bid = @meth_body_ids[mi] if bid >= 0 pnames = @meth_param_names[mi].split(",") ptypes = @meth_param_types[mi].split(",") changed_top = 0 pk = 0 while pk < pnames.length if pk < ptypes.length && (ptypes[pk] == "int" || ptypes[pk] == "nil") called = "".split(",") collect_param_methods(bid, pnames[pk], called) saw = 0 kk = 0 while kk < called.length if is_lengthlike_only_method(called[kk]) == 1 saw = 1 end kk = kk + 1 end if saw == 1 @needs_rb_value = 1 ptypes[pk] = "poly" changed_top = 1 end end pk = pk + 1 end if changed_top == 1 @meth_param_types[mi] = ptypes.join(",") end end mi = mi + 1 end ci = 0 while ci < @cls_names.length mnames = @cls_meth_names[ci].split(";") bodies = @cls_meth_bodies[ci].split(";") cls_changed = 0 mj = 0 while mj < mnames.length if mnames[mj] != "initialize" pnames_j = cls_meth_pnames_get(ci, mj) ptypes_j = cls_meth_ptypes_get(ci, mj) bid_j = -1 if mj < bodies.length bid_j = bodies[mj].to_i end if bid_j >= 0 m_changed = 0 pk = 0 while pk < pnames_j.length if pk < ptypes_j.length && (ptypes_j[pk] == "int" || ptypes_j[pk] == "nil") called_c = "".split(",") collect_param_methods(bid_j, pnames_j[pk], called_c) sawc = 0 kk = 0 while kk < called_c.length if is_lengthlike_only_method(called_c[kk]) == 1 sawc = 1 end kk = kk + 1 end if sawc == 1 @needs_rb_value = 1 ptypes_j[pk] = "poly" m_changed = 1 end end pk = pk + 1 end if m_changed == 1 cls_meth_ptypes_put(ci, mj, ptypes_j) cls_changed = 1 end end end mj = mj + 1 end if cls_changed == 1 @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1 end ci = ci + 1 end end def is_string_only_method(mname) if mname == "split" || mname == "start_with?" || mname == "end_with?" return 1 infer_function_body_call_types infer_class_body_call_types infer_all_returns # Body-usage length-like inference (#552). Conservative # widening to flat `poly`: only fires on `.length` / `.size` / # `.empty?` -- methods that exist on String / Array / Hash # but NOT on Integer, so a body that calls them on a param # proves the param's runtime value isn't an int. Stays after # the array-param pass so the narrow variants # (int_array / str_array / etc.) win first; only genuinely- # untyped params reach the flat `poly` widening here. infer_param_lengthlike_widen infer_all_returns infer_function_body_call_types infer_class_body_call_types infer_all_returns # Fix lambda return types based on call-site usage fix_lambda_return_types # Pre-detect bigint variables before feature detectionspinel_codegen.rbmodifiedView file
Loading diff…
test/param_lengthlike_body_widen.rbaddedView file
Loading diff…
test/param_lengthlike_body_widen.rb.expectedaddedView file
Loading diff…