Commit c11200dd
c11200dd5775Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): infer "string" param type from String-only body methods
#450 cascade 2. A method body that calls .split, .start_with?,
.gsub, etc. on a param is strong evidence the param is a String,
but the existing infer_cls_meth_param_from_body pass only walked
the body for user-class methods and treated everything else as
either a container builtin (filtered out) or "no signal" — so a
def match_pattern(pattern, path); path.split("/"); end body
with no concrete-typed caller left path pinned at the placeholder
"int" / mrb_int default, and the C emit of .split on a mrb_int
fell over with cannot resolve call to 'split' on int.
Add infer_string_param_from_body, a sibling pass that runs in the
iterative analyze loop. For each method, if a param's stored type
is still "int", walk the body's CallNode receivers (already done by
the existing collect_param_methods helper) and check whether any
called method is in the new is_string_only_method list. That list
includes the String-specific methods that don't collide with Array
/ Hash / user-class surfaces:
split / start_with? / end_with? / chomp / chop / strip / lstrip / rstrip / upcase / downcase / swapcase / capitalize / tr / gsub / sub / scan / match / match? / ljust / rjust / center / bytes / bytesize / chars / lines / codepoints / encode / force_encoding / encoding / concat / ascii_only?
When at least one such method is observed on the param, promote
the slot to "string". Walks both top-level methods and class
instance methods; runs after narrow_param_types_from_body_method_calls
so a class-promotion path takes precedence (a user class with a
custom .split would have already matched via the existing
unify_param_class_from_observations).
In the cascade 2 chain:
Main.run(env, stdin, stdout) → Router.match(method, path, table)
→ match_pattern(pattern, path) → path.split("/")
the new pass promotes match_pattern's path to "string". The
call-site widening in match's body then sees the typed slot and
emits sp_int_to_s(lv_path) at the call instead of failing
type-check on the raw mrb_int.
Tests: 406 pass (+1 regression covering self.method form, top-level def form, and a non-trigger check that container-only methods leave the param as int), bench 52, optcarrot checksum 59662.
Refs #450 (cascade 2 of 3)
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- c11200dd5775
- Parent
- cc45317ae7cd
spinel_analyze.rbmodifiedView file
end end # Methods that exist on String but not on any user-overrideable # receiver, and not on the container builtins. A body that calls # one of these on a param is strong evidence the param is a # String. Used by infer_string_param_from_body to lift "int" # default ptypes to "string" when the body's only user signal is # a String-specific receiver method (#450 cascade 2: # `def match(method, path, table); ...; path.split("/"); end` # left `path` typed mrb_int because called_methods_only_on_ # container_builtins filtered `split` out and no user class was # a perfect fit). def is_string_only_method(mname) if mname == "split" || mname == "start_with?" || mname == "end_with?" return 1 end if mname == "chomp" || mname == "chop" || mname == "strip" || mname == "lstrip" || mname == "rstrip" return 1 end if mname == "upcase" || mname == "downcase" || mname == "swapcase" || mname == "capitalize" return 1 end if mname == "tr" || mname == "gsub" || mname == "sub" || mname == "scan" || mname == "match" || mname == "match?" return 1 end if mname == "ljust" || mname == "rjust" || mname == "center" return 1 end if mname == "bytes" || mname == "bytesize" || mname == "chars" || mname == "lines" || mname == "codepoints" return 1 end if mname == "encode" || mname == "force_encoding" || mname == "encoding" return 1 end if mname == "concat" || mname == "ascii_only?" return 1 end 0 end # Walk every method body. If a param is still typed "int" (the # placeholder default) and the body calls at least one # String-specific method on it, promote to "string". Mirrors # infer_cls_meth_param_from_body's class-promotion shape, but # specialised for the String case where the canonical receiver # is the primitive rather than a user class. def infer_string_param_from_body 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" called = "".split(",") collect_param_methods(bid, pnames[pk], called) sawk = 0 kk = 0 while kk < called.length if is_string_only_method(called[kk]) == 1 sawk = 1 end kk = kk + 1 end if sawk == 1 ptypes[pk] = "string" 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" called_c = "".split(",") collect_param_methods(bid_j, pnames_j[pk], called_c) sawc = 0 kk = 0 while kk < called_c.length if is_string_only_method(called_c[kk]) == 1 sawc = 1 end kk = kk + 1 end if sawc == 1 ptypes_j[pk] = "string" 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 # Widen each method's stored parameter types to encompass any # in-body reassignments to the parameter name. Without this, # a body shape like infer_ivar_types_from_writers infer_param_array_type_from_body narrow_param_types_from_body_method_calls infer_string_param_from_body narrow_param_hash_types_from_body_writes # propagate hash-each block-arg types into # nested cmeth/method-call param widening. Runs insidetest/string_param_inference_from_body.rbaddedView file
Loading diff…
test/string_param_inference_from_body.rb.expectedaddedView file
Loading diff…