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
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 # to the receiver-aware resolution that # infer_constant_recv_type runs later. Without this guard, the # name-based hardcodes below (`hash -> int`, `to_s -> string`, # etc.) would pre-empt user-defined `def self.hash(plain) -> # String` cmeths, widening downstream local types to int and # cascading into incompatible-pointer C errors at the next # 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. if recv >= 0 && (@nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode") rcname_407 = constructor_class_name(recv) if rcname_407 != "" ci_407 = find_class_idx(rcname_407) if ci_407 >= 0 && cls_cmethod_owner(ci_407, mname) >= 0 return "" end if module_name_exists(rcname_407) == 1 if find_method_idx(rcname_407 + "_cls_" + mname) >= 0 return "" end end end end if mname == "length" return "int" endtest/cmeth_name_collision_with_kernel.rbaddedView file
Loading diff…
test/cmeth_name_collision_with_kernel.rb.expectedaddedView file
Loading diff…