Commit 0114f219

0114f2199821Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~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
0114f2199821
Parent
906671d83beb

spinel_analyze.rbmodifiedView file

@@ -3106,6 +3106,31 @@
31063106 end
31073107
31083108 def infer_method_name_type(nid, mname, recv)
3109+ # Issue #407: when recv is a class/module constant ref whose
3110+ # class/module defines a class method of the given name, defer
3111+ # to the receiver-aware resolution that
3112+ # infer_constant_recv_type runs later. Without this guard, the
3113+ # name-based hardcodes below (`hash -> int`, `to_s -> string`,
3114+ # etc.) would pre-empt user-defined `def self.hash(plain) ->
3115+ # String` cmeths, widening downstream local types to int and
3116+ # cascading into incompatible-pointer C errors at the next
3117+ # use site. Return "" so the caller falls through to
3118+ # infer_constant_recv_type, which already does the
3119+ # cls_cmethod_return_inherited / `<Mod>_cls_<m>` lookup.
3120+ if recv >= 0 && (@nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode")
3121+ rcname_407 = constructor_class_name(recv)
3122+ if rcname_407 != ""
3123+ ci_407 = find_class_idx(rcname_407)
3124+ if ci_407 >= 0 && cls_cmethod_owner(ci_407, mname) >= 0
3125+ return ""
3126+ end
3127+ if module_name_exists(rcname_407) == 1
3128+ if find_method_idx(rcname_407 + "_cls_" + mname) >= 0
3129+ return ""
3130+ end
3131+ end
3132+ end
3133+ end
31093134 if mname == "length"
31103135 return "int"
31113136 end