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

@@ -10844,6 +10844,135 @@
1084410844 end
1084510845 end
1084610846
10847+ # Methods that exist on String but not on any user-overrideable
10848+ # receiver, and not on the container builtins. A body that calls
10849+ # one of these on a param is strong evidence the param is a
10850+ # String. Used by infer_string_param_from_body to lift "int"
10851+ # default ptypes to "string" when the body's only user signal is
10852+ # a String-specific receiver method (#450 cascade 2:
10853+ # `def match(method, path, table); ...; path.split("/"); end`
10854+ # left `path` typed mrb_int because called_methods_only_on_
10855+ # container_builtins filtered `split` out and no user class was
10856+ # a perfect fit).
10857+ def is_string_only_method(mname)
10858+ if mname == "split" || mname == "start_with?" || mname == "end_with?"
10859+ return 1
10860+ end
10861+ if mname == "chomp" || mname == "chop" || mname == "strip" || mname == "lstrip" || mname == "rstrip"
10862+ return 1
10863+ end
10864+ if mname == "upcase" || mname == "downcase" || mname == "swapcase" || mname == "capitalize"
10865+ return 1
10866+ end
10867+ if mname == "tr" || mname == "gsub" || mname == "sub" || mname == "scan" || mname == "match" || mname == "match?"
10868+ return 1
10869+ end
10870+ if mname == "ljust" || mname == "rjust" || mname == "center"
10871+ return 1
10872+ end
10873+ if mname == "bytes" || mname == "bytesize" || mname == "chars" || mname == "lines" || mname == "codepoints"
10874+ return 1
10875+ end
10876+ if mname == "encode" || mname == "force_encoding" || mname == "encoding"
10877+ return 1
10878+ end
10879+ if mname == "concat" || mname == "ascii_only?"
10880+ return 1
10881+ end
10882+ 0
10883+ end
10884+
10885+ # Walk every method body. If a param is still typed "int" (the
10886+ # placeholder default) and the body calls at least one
10887+ # String-specific method on it, promote to "string". Mirrors
10888+ # infer_cls_meth_param_from_body's class-promotion shape, but
10889+ # specialised for the String case where the canonical receiver
10890+ # is the primitive rather than a user class.
10891+ def infer_string_param_from_body
10892+ mi = 0
10893+ while mi < @meth_names.length
10894+ bid = @meth_body_ids[mi]
10895+ if bid >= 0
10896+ pnames = @meth_param_names[mi].split(",")
10897+ ptypes = @meth_param_types[mi].split(",")
10898+ changed_top = 0
10899+ pk = 0
10900+ while pk < pnames.length
10901+ if pk < ptypes.length && ptypes[pk] == "int"
10902+ called = "".split(",")
10903+ collect_param_methods(bid, pnames[pk], called)
10904+ sawk = 0
10905+ kk = 0
10906+ while kk < called.length
10907+ if is_string_only_method(called[kk]) == 1
10908+ sawk = 1
10909+ end
10910+ kk = kk + 1
10911+ end
10912+ if sawk == 1
10913+ ptypes[pk] = "string"
10914+ changed_top = 1
10915+ end
10916+ end
10917+ pk = pk + 1
10918+ end
10919+ if changed_top == 1
10920+ @meth_param_types[mi] = ptypes.join(",")
10921+ end
10922+ end
10923+ mi = mi + 1
10924+ end
10925+ ci = 0
10926+ while ci < @cls_names.length
10927+ mnames = @cls_meth_names[ci].split(";")
10928+ bodies = @cls_meth_bodies[ci].split(";")
10929+ cls_changed = 0
10930+ mj = 0
10931+ while mj < mnames.length
10932+ if mnames[mj] != "initialize"
10933+ pnames_j = cls_meth_pnames_get(ci, mj)
10934+ ptypes_j = cls_meth_ptypes_get(ci, mj)
10935+ bid_j = -1
10936+ if mj < bodies.length
10937+ bid_j = bodies[mj].to_i
10938+ end
10939+ if bid_j >= 0
10940+ m_changed = 0
10941+ pk = 0
10942+ while pk < pnames_j.length
10943+ if pk < ptypes_j.length && ptypes_j[pk] == "int"
10944+ called_c = "".split(",")
10945+ collect_param_methods(bid_j, pnames_j[pk], called_c)
10946+ sawc = 0
10947+ kk = 0
10948+ while kk < called_c.length
10949+ if is_string_only_method(called_c[kk]) == 1
10950+ sawc = 1
10951+ end
10952+ kk = kk + 1
10953+ end
10954+ if sawc == 1
10955+ ptypes_j[pk] = "string"
10956+ m_changed = 1
10957+ end
10958+ end
10959+ pk = pk + 1
10960+ end
10961+ if m_changed == 1
10962+ cls_meth_ptypes_put(ci, mj, ptypes_j)
10963+ cls_changed = 1
10964+ end
10965+ end
10966+ end
10967+ mj = mj + 1
10968+ end
10969+ if cls_changed == 1
10970+ @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
10971+ end
10972+ ci = ci + 1
10973+ end
10974+ end
10975+
1084710976 # Widen each method's stored parameter types to encompass any
1084810977 # in-body reassignments to the parameter name. Without this,
1084910978 # a body shape like
@@ -15368,6 +15497,7 @@
1536815497 infer_ivar_types_from_writers
1536915498 infer_param_array_type_from_body
1537015499 narrow_param_types_from_body_method_calls
15500+ infer_string_param_from_body
1537115501 narrow_param_hash_types_from_body_writes
1537215502 # propagate hash-each block-arg types into
1537315503 # nested cmeth/method-call param widening. Runs inside