Commit 69db8f5a
69db8f5aec04Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): infer obj's user-defined []'s return type for chain coercion
bag[:id].to_i silently dropped .to_i from the emitted C
because infer_type's mname == "[]" branch ended its built-in
collection / hash dispatch with a tail return "int" — the
user-defined-class def [](k) case never reached
cls_method_return. The outer .to_i then dispatched on int recv,
which is the identity, so the underlying const char * flowed
straight through into a printf %lld cast.
Same family as #207's class-method param widening — an obj recv that wasn't being routed through cls_method_return. Add the is_obj_type branch right before the tail "int" fallback. Mirrors the obj-method lookup at line 3757 (infer_recv_method_type).
Closes #219
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 69db8f5aec04
- Parent
- 311d03089966
spinel_codegen.rbmodifiedView file
if rt == "lambda" return "lambda" end # Issue #219: user-defined `def [](k)` on an obj recv. Walk # the class's method table the same way infer_recv_method_type # does for arbitrary mname — without this, the fallback at the # tail of this branch returns "int" and downstream `.to_i` / # `.length` etc. dispatch on the wrong recv type. if is_obj_type(rt) == 1 bt = base_type(rt) cname = bt[4, bt.length - 4] ci = find_class_idx(cname) if ci >= 0 mr = cls_method_return(ci, "[]") if mr != "" && mr != "int" return mr end end end end return "int" endtest/issue219_aref_chain.rbaddedView file
Loading diff…
test/issue219_aref_chain.rb.expectedaddedView file
Loading diff…