Commit 1368c289

1368c2896f92Merged

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

Description

fix(analyze): hardcoded-name imeth arms defer to user methods (#407)

Pre-fix the hardcoded mname == "hash" -> int, to_s -> string, to_i -> int, length -> int, count -> int, size -> int arms in infer_method_name_type pre-empted user-defined instance methods of the same name, even when the recv was statically a typed obj_ instance. Ori's repro:

class Item def hash; "item-hash:" + @tag; end end i = Item.new("a") puts i.hash # printed integer (Object#hash), not the string

The inferred return type of i.hash was pinned to int before infer_recv_method_type's cls_method_return path got a chance to find Item's user-defined return type.

Fix: each hardcoded arm defers (returns "") when (a) the recv is plausibly a user-class instance at runtime (LocalVariableReadNode / CallNode / InstanceVariableReadNode -- not a literal IntegerNode / StringNode / etc.), and (b) at least one user class declares the same imeth. The defer lets infer_recv_method_type's obj_ path resolve cls_method_return to the user's actual type. Primitive recvs (literal nodes) still hit the hardcode, so 5.hash / "x".to_i etc. stay untouched.

Two new helpers (any_user_class_defines_imeth(mname) and recv_could_be_obj(recv)) keep the per-arm guard a single line. Applied to: hash, to_s, to_i, to_f, length, size, count.

Out of scope for this fix:

  • Cross-class method-name widening without a hardcoded arm (Ori's case 2: attr_accessor :handle on one class + def handle(req, res) on another). The widening happens in poly_dispatch_return_type's user-class enumeration, not in the hardcoded name table. Needs receiver-aware dispatch based on statically pinned recv class -- separate, larger fix.
  • The same dynamic-recv var-type-timing issue matz flagged in the earlier #407 comment still applies: when the recv is a LocalVariableReadNode whose var-type table isn't yet populated, infer_type(recv) returns "int" / "" and the obj_ path doesn't fire. recv_could_be_obj sidesteps this by checking the AST shape directly, which covers the canonical i = Item.new; i.hash pattern but not every flow.

Coverage in test/method_name_collision_imeth.rb. Optcarrot checksum 59662 unchanged, fps 629.6 (within noise).

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

Details

Commit
1368c2896f92
Parent
c61deadfe652

spinel_analyze.rbmodifiedView file

@@ -3143,6 +3143,47 @@
31433143 ""
31443144 end
31453145
3146+ # Issue #407: does any user class declare `mname` as an
3147+ # instance method? Used by the hardcoded name-based inference
3148+ # arms below to defer when the recv's type isn't pinned yet
3149+ # but a user-class definition exists that could win at runtime.
3150+ # Falling through to infer_recv_method_type lets the
3151+ # cls_method_return path pick up the user's return type while
3152+ # still terminating at "int" if no obj_<C> resolution lands.
3153+ def any_user_class_defines_imeth(mname)
3154+ ck = 0
3155+ while ck < @cls_names.length
3156+ names_407 = @cls_meth_names[ck].split(";")
3157+ kk = 0
3158+ while kk < names_407.length
3159+ if names_407[kk] == mname
3160+ return 1
3161+ end
3162+ kk = kk + 1
3163+ end
3164+ ck = ck + 1
3165+ end
3166+ 0
3167+ end
3168+
3169+ # Issue #407: recv is plausibly a user-class instance at runtime
3170+ # even when its current inferred type is "int" or "" (because the
3171+ # var-type table hasn't been populated yet during the iterative
3172+ # inference loop). Returns 1 for LocalVariableReadNode /
3173+ # CallNode / InstanceVariableReadNode whose actual runtime type
3174+ # is obj_<C>. False for literal nodes (IntegerNode, StringNode,
3175+ # etc.) where the recv is statically a primitive.
3176+ def recv_could_be_obj(recv)
3177+ if recv < 0
3178+ return 0
3179+ end
3180+ t_407rc = @nd_type[recv]
3181+ if t_407rc == "IntegerNode" || t_407rc == "StringNode" || t_407rc == "FloatNode" || t_407rc == "SymbolNode" || t_407rc == "TrueNode" || t_407rc == "FalseNode" || t_407rc == "NilNode" || t_407rc == "ArrayNode" || t_407rc == "HashNode" || t_407rc == "RangeNode"
3182+ return 0
3183+ end
3184+ 1
3185+ end
3186+
31463187 def infer_method_name_type(nid, mname, recv)
31473188 # Issue #407: when recv is a class/module constant ref whose
31483189 # class/module defines a class method of the given name, defer
@@ -3155,6 +3196,25 @@
31553196 # use site. Return "" so the caller falls through to
31563197 # infer_constant_recv_type, which already does the
31573198 # cls_cmethod_return_inherited / `<Mod>_cls_<m>` lookup.
3199+ #
3200+ # Symmetric instance-method case: when recv is a statically
3201+ # typed obj_<C> instance and C (or any ancestor) defines an
3202+ # instance method named mname, defer to infer_recv_method_type
3203+ # so its cls_method_return path returns the user's type
3204+ # instead of the hardcoded Object#... default. Mirrors the
3205+ # cmeth defer above; both target the imeth shape Ori called
3206+ # out (`Item#hash` shadowed by Object#hash returning int).
3207+ if recv >= 0
3208+ rt_407i = infer_type(recv)
3209+ if is_obj_type(rt_407i) == 1
3210+ bt_407i = base_type(rt_407i)
3211+ cname_407i = bt_407i[4, bt_407i.length - 4]
3212+ ci_407i = find_class_idx(cname_407i)
3213+ if ci_407i >= 0 && cls_find_method(ci_407i, mname) >= 0
3214+ return ""
3215+ end
3216+ end
3217+ end
31583218 if recv >= 0 && (@nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode")
31593219 rcname_407 = constructor_class_name(recv)
31603220 if rcname_407 != ""
@@ -3170,9 +3230,15 @@
31703230 end
31713231 end
31723232 if mname == "length"
3233+ if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1
3234+ return ""
3235+ end
31733236 return "int"
31743237 end
31753238 if mname == "to_s"
3239+ if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1
3240+ return ""
3241+ end
31763242 return "string"
31773243 end
31783244 if mname == "inspect"
@@ -3221,9 +3287,15 @@
32213287 end
32223288 end
32233289 if mname == "to_i"
3290+ if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1
3291+ return ""
3292+ end
32243293 return "int"
32253294 end
32263295 if mname == "to_f"
3296+ if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1
3297+ return ""
3298+ end
32273299 return "float"
32283300 end
32293301 # Issue #414: Time#iso8601 and Time#strftime — both return a
@@ -3342,6 +3414,9 @@
33423414 end
33433415 end
33443416 if mname == "hash"
3417+ if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1
3418+ return ""
3419+ end
33453420 return "int"
33463421 end
33473422 if mname == "strip"
@@ -3653,9 +3728,15 @@
36533728 return "int"
36543729 end
36553730 if mname == "count"
3731+ if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1
3732+ return ""
3733+ end
36563734 return "int"
36573735 end
36583736 if mname == "size"
3737+ if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1
3738+ return ""
3739+ end
36593740 return "int"
36603741 end
36613742 if mname == "index" || mname == "find_index" || mname == "rindex"