Commit 24e6a84a

24e6a84a7b96Merged

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

Description

fix(codegen): user-defined find/fetch wins over built-in dispatch

infer_method_name_type's find / fetch branches returned "int" unconditionally for receivers that didn't match a known built-in collection (str_array, float_array, str_*_hash). For a class receiver like Bag.find(id) — the canonical ActiveRecord finder shape — that "int" preempted the later user-defined class-method dispatch (line 3245+), which would have correctly inferred the body's return type. puts Bag.find(1) then formatted the const char * result with %lld and printed garbage; s = Bag.find(1) pinned lv_s as mrb_int and failed to compile.

Return "" instead of "int" in the unmatched-receiver path so the dispatch chain continues to infer_constant_recv_type, which handles the user-defined *_cls_<method> lookup correctly. The built-in collection paths (str_array.find, str_int_hash.fetch, ...) keep their existing concrete returns; only the fallback changes.

Same family as #127 (module-class-method String return mis-inferred as Integer).

Closes #204

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

Details

Commit
24e6a84a7b96
Parent
382796cd0226

spinel_codegen.rbmodifiedView file

@@ -2452,7 +2452,10 @@
24522452 return "string"
24532453 end
24542454 end
2455- return "int"
2455+ # Issue #204: don't claim "int" for fetch on receivers we don't
2456+ # recognize as a built-in collection — let later dispatch resolve
2457+ # a user-defined `def fetch` against the receiver class.
2458+ return ""
24562459 end
24572460 if mname == "has_key?" || mname == "key?" || mname == "member?"
24582461 return "bool"
@@ -2580,7 +2583,11 @@
25802583 return "string"
25812584 end
25822585 end
2583- return "int"
2586+ # Issue #204: same as fetch above — fall through so a user-
2587+ # defined `def find` (canonical ActiveRecord finder shape) wins
2588+ # over the built-in collection dispatch when the receiver isn't
2589+ # a built-in collection.
2590+ return ""
25842591 end
25852592 if mname == "keys"
25862593 if recv >= 0