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

@@ -3443,6 +3443,22 @@
34433443 if rt == "lambda"
34443444 return "lambda"
34453445 end
3446+ # Issue #219: user-defined `def [](k)` on an obj recv. Walk
3447+ # the class's method table the same way infer_recv_method_type
3448+ # does for arbitrary mname — without this, the fallback at the
3449+ # tail of this branch returns "int" and downstream `.to_i` /
3450+ # `.length` etc. dispatch on the wrong recv type.
3451+ if is_obj_type(rt) == 1
3452+ bt = base_type(rt)
3453+ cname = bt[4, bt.length - 4]
3454+ ci = find_class_idx(cname)
3455+ if ci >= 0
3456+ mr = cls_method_return(ci, "[]")
3457+ if mr != "" && mr != "int"
3458+ return mr
3459+ end
3460+ end
3461+ end
34463462 end
34473463 return "int"
34483464 end