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 :handleon 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 canonicali = Item.new; i.hashpattern 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
"" end # Issue #407: does any user class declare `mname` as an # instance method? Used by the hardcoded name-based inference # arms below to defer when the recv's type isn't pinned yet # but a user-class definition exists that could win at runtime. # Falling through to infer_recv_method_type lets the # cls_method_return path pick up the user's return type while # still terminating at "int" if no obj_<C> resolution lands. def any_user_class_defines_imeth(mname) ck = 0 while ck < @cls_names.length names_407 = @cls_meth_names[ck].split(";") kk = 0 while kk < names_407.length if names_407[kk] == mname return 1 end kk = kk + 1 end ck = ck + 1 end 0 end # Issue #407: recv is plausibly a user-class instance at runtime # even when its current inferred type is "int" or "" (because the # var-type table hasn't been populated yet during the iterative # inference loop). Returns 1 for LocalVariableReadNode / # CallNode / InstanceVariableReadNode whose actual runtime type # is obj_<C>. False for literal nodes (IntegerNode, StringNode, # etc.) where the recv is statically a primitive. def recv_could_be_obj(recv) if recv < 0 return 0 end t_407rc = @nd_type[recv] 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" return 0 end 1 end def infer_method_name_type(nid, mname, recv) # Issue #407: when recv is a class/module constant ref whose # class/module defines a class method of the given name, defer # use site. Return "" so the caller falls through to # infer_constant_recv_type, which already does the # cls_cmethod_return_inherited / `<Mod>_cls_<m>` lookup. # # Symmetric instance-method case: when recv is a statically # typed obj_<C> instance and C (or any ancestor) defines an # instance method named mname, defer to infer_recv_method_type # so its cls_method_return path returns the user's type # instead of the hardcoded Object#... default. Mirrors the # cmeth defer above; both target the imeth shape Ori called # out (`Item#hash` shadowed by Object#hash returning int). if recv >= 0 rt_407i = infer_type(recv) if is_obj_type(rt_407i) == 1 bt_407i = base_type(rt_407i) cname_407i = bt_407i[4, bt_407i.length - 4] ci_407i = find_class_idx(cname_407i) if ci_407i >= 0 && cls_find_method(ci_407i, mname) >= 0 return "" end end end if recv >= 0 && (@nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode") rcname_407 = constructor_class_name(recv) if rcname_407 != "" end end if mname == "length" if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1 return "" end return "int" end if mname == "to_s" if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1 return "" end return "string" end if mname == "inspect" end end if mname == "to_i" if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1 return "" end return "int" end if mname == "to_f" if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1 return "" end return "float" end # Issue #414: Time#iso8601 and Time#strftime — both return a end end if mname == "hash" if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1 return "" end return "int" end if mname == "strip" return "int" end if mname == "count" if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1 return "" end return "int" end if mname == "size" if recv_could_be_obj(recv) == 1 && any_user_class_defines_imeth(mname) == 1 return "" end return "int" end if mname == "index" || mname == "find_index" || mname == "rindex"test/method_name_collision_imeth.rbaddedView file
Loading diff…
test/method_name_collision_imeth.rb.expectedaddedView file
Loading diff…