Commit e5a9ae21

e5a9ae21ea02Merged

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

Description

fix(codegen): receiver-aware skip of name-based hardcodes (#407)

infer_method_name_type had unconditional return arms like if mname == "hash"; return "int"; end -- correct for obj.hash against the Kernel default but wrong for Klass.hash(...) where the user defined def self.hash(plain) -> String. The Kernel-side hardcode pre-empted the receiver-aware resolution infer_constant_recv_type runs later, locking the expression's inferred type at int. Downstream stored = Auth::Password.hash("hunter2") then declared lv_stored as mrb_int, and the next use site (a fputs call, a string-concat, etc.) tripped the C compile with passing 'mrb_int' to const char *.

Fix: at the top of infer_method_name_type, return "" when recv is a ConstantReadNode / ConstantPathNode resolving to a known class or module that defines a class method named mname. The caller falls through to infer_constant_recv_type, which already does the cls_cmethod_return_inherited walk for classes and the synthetic <Mod>_cls_<m> lookup for modules.

This is the receiver-aware-dispatch shape from issue #407: when the receiver is statically known, defer to that class's/module's definition rather than name-based defaults that target unrelated classes' methods sharing the name.

Test cmeth_name_collision_with_kernel.rb: covers the canonical Klass.hash shape (the bcrypt-gem-like factory), .methods (collides with Object#methods), and .inspect_kind (a fresh name to verify the gate without leaning on infrastructure that might already redirect via a different path). All three call sites flow into local-variable assignments that exercise the inferred return type end-to-end.

Refs #407

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

Details

Commit
e5a9ae21ea02
Parent
f896e1b484b5

spinel_codegen.rbmodifiedView file

@@ -3640,6 +3640,31 @@
36403640 end
36413641
36423642 def infer_method_name_type(nid, mname, recv)
3643+ # Issue #407: when recv is a class/module constant ref whose
3644+ # class/module defines a class method of the given name, defer
3645+ # to the receiver-aware resolution that
3646+ # infer_constant_recv_type runs later. Without this guard, the
3647+ # name-based hardcodes below (`hash -> int`, `to_s -> string`,
3648+ # etc.) would pre-empt user-defined `def self.hash(plain) ->
3649+ # String` cmeths, widening downstream local types to int and
3650+ # cascading into incompatible-pointer C errors at the next
3651+ # use site. Return "" so the caller falls through to
3652+ # infer_constant_recv_type, which already does the
3653+ # cls_cmethod_return_inherited / `<Mod>_cls_<m>` lookup.
3654+ if recv >= 0 && (@nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode")
3655+ rcname_407 = constructor_class_name(recv)
3656+ if rcname_407 != ""
3657+ ci_407 = find_class_idx(rcname_407)
3658+ if ci_407 >= 0 && cls_cmethod_owner(ci_407, mname) >= 0
3659+ return ""
3660+ end
3661+ if module_name_exists(rcname_407) == 1
3662+ if find_method_idx(rcname_407 + "_cls_" + mname) >= 0
3663+ return ""
3664+ end
3665+ end
3666+ end
3667+ end
36433668 if mname == "length"
36443669 return "int"
36453670 end