Commit 36cc6fb9

36cc6fb9bd66Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): infer poly method return type as common-or-poly, not first match

Both infer_recv_method_type and compile_poly_method_call decided the return type of recv.mname(...) (where recv is poly) by returning the first matching class's return type. When user classes disagreed on the return type for the same method name, the inferred type was silently wrong.

Reproducer:

class Foo def name; 42; end # mrb_int end class Bar def name; "bar"; end # const char * end def show(a); a.name; end puts show(Foo.new) puts show(Bar.new)

Master picks Foo's mrb_int for a.name, so sp_show is declared to return mrb_int, and the C compiler then refuses the const char * from sp_Bar_name returning out of sp_show.

Replace both call sites with a shared helper:

def poly_dispatch_return_type(mname) common = "" ci = 0 while ci < @cls_names.length if cls_find_method_direct(ci, mname) >= 0 rt = cls_method_return(ci, mname) if common == "" common = rt else if common != rt return "poly" # disagree → genuine sp_RbVal end end end ci = ci + 1 end common == "" ? "int" : common end

If all classes agree, the result keeps its concrete C type and compile_poly_method_call emits a typed temp as before. If they disagree, the temp is sp_RbVal and each dispatch branch boxes its concrete return value via box_val_to_poly.

Adds test/bm_poly_method_mixed_return.rb as a small reproducer.

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

Details

Commit
36cc6fb9bd66
Parent
8bb4c059f4db

spinel_codegen.rbmodifiedView file

@@ -2717,16 +2717,10 @@
27172717 if mname == "nil?"
27182718 return "bool"
27192719 end
2720- # Check all classes for this method and return the first matching return type
2721- ci = 0
2722- while ci < @cls_names.length
2723- midx = cls_find_method_direct(ci, mname)
2724- if midx >= 0
2725- return cls_method_return(ci, mname)
2726- end
2727- ci = ci + 1
2728- end
2729- return "int"
2720+ # Scan every user class that defines this method. If they all
2721+ # agree on the return type, the call has that concrete type.
2722+ # If they disagree, the call is genuinely polymorphic.
2723+ return poly_dispatch_return_type(mname)
27302724 end
27312725 # Method call on int (possible IntArray element storing object pointers)
27322726 if rt == "int"
@@ -15700,6 +15694,33 @@
1570015694 end
1570115695
1570215696
15697+ # Inferred return type for `recv.mname(...)` when `recv` is poly.
15698+ # If every user class that defines mname agrees on the return type,
15699+ # that concrete type is used. If any two disagree, the call is
15700+ # genuinely polymorphic and the caller must treat the result as
15701+ # an sp_RbVal.
15702+ def poly_dispatch_return_type(mname)
15703+ common = ""
15704+ ci = 0
15705+ while ci < @cls_names.length
15706+ if cls_find_method_direct(ci, mname) >= 0
15707+ rt = cls_method_return(ci, mname)
15708+ if common == ""
15709+ common = rt
15710+ else
15711+ if common != rt
15712+ return "poly"
15713+ end
15714+ end
15715+ end
15716+ ci = ci + 1
15717+ end
15718+ if common == ""
15719+ return "int"
15720+ end
15721+ common
15722+ end
15723+
1570315724 def compile_poly_method_call(nid, rc, mname)
1570415725 @needs_rb_value = 1
1570515726 if mname == "nil?"
@@ -15708,19 +15729,32 @@
1570815729 if mname == "to_s"
1570915730 return "sp_poly_to_s(" + rc + ")"
1571015731 end
15711- # For object method calls, dispatch based on cls_id
15712- # Generate: switch on v.tag and cls_id
15732+ # For object method calls, dispatch based on cls_id. The result
15733+ # temp is typed by the method's return type. If user classes
15734+ # disagree on that type, the result is sp_RbVal and each branch
15735+ # boxes its concrete return value.
15736+ ret_type = poly_dispatch_return_type(mname)
15737+ is_poly_ret = 0
15738+ if ret_type == "poly"
15739+ is_poly_ret = 1
15740+ end
15741+ ret_ct = c_type(ret_type)
15742+ ret_def = c_default_val(ret_type)
1571315743 tmp = new_temp
15714- emit(" const char *" + tmp + " = \"\";")
15744+ emit(" " + ret_ct + " " + tmp + " = " + ret_def + ";")
1571515745 emit(" if (" + rc + ".tag == SP_TAG_OBJ) {")
15716- # Dispatch to each possible class
1571715746 i = 0
1571815747 while i < @cls_names.length
1571915748 cname = @cls_names[i]
15720- # Check if this class has the method
1572115749 midx = cls_find_method_direct(i, mname)
1572215750 if midx >= 0
15723- emit(" if (" + rc + ".v.cls_id == " + i.to_s + ") " + tmp + " = sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + rc + ".v.p);")
15751+ call_expr = "sp_" + cname + "_" + sanitize_name(mname) + "((sp_" + cname + " *)" + rc + ".v.p)"
15752+ rhs = call_expr
15753+ if is_poly_ret == 1
15754+ this_rt = cls_method_return(i, mname)
15755+ rhs = box_val_to_poly(call_expr, this_rt)
15756+ end
15757+ emit(" if (" + rc + ".v.cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";")
1572415758 end
1572515759 i = i + 1
1572615760 end