Commit e6bdc7d8
e6bdc7d8f6ddMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Merge pull request #1278 from rubys/fix-covariant-return-dispatch
Dispatch implicit-self calls with covariant object return types (fixes #1277)
Details
- Commit
- e6bdc7d8f6dd
- Parent
- bf0d02eb0409
- Merge parent
- 97459e90443a
spinel_codegen.rbmodifiedView file
@@ -19131,8 +19131,30 @@
1913119131
cand_owner = p[1].to_i1913219132
cand_rt = cls_method_return(cand_owner, mname)1913319133
if c_type(cand_rt) != base_rt_c19134-
rt_ok = 019135-
ol = ovr_list.length19134+
# Covariant object return: the override returns a proper19135+
# subtype of the base's declared return (e.g. an RBS base19136+
# `() -> Base` with a subclass `() -> Sub`, or `self`-19137+
# returning per-model primitives). The C types differ19138+
# (sp_Sub* vs sp_Base*) but the dispatch is still valid —19139+
# the switch arm upcasts the result to the base C type.19140+
# Without this, an RBS signature on the base method19141+
# suppresses dispatch entirely (matz/spinel#1277).19142+
base_bt = base_type(base_rt)19143+
cand_bt = base_type(cand_rt)19144+
covariant = 019145+
if is_obj_type(base_bt) == 1 && is_obj_type(cand_bt) == 119146+
# Strip the "obj_" prefix (codebase idiom) to name the19147+
# classes, then test the subtype relationship.19148+
base_cls = find_class_idx(base_bt[4, base_bt.length - 4])19149+
cand_cls = find_class_idx(cand_bt[4, cand_bt.length - 4])19150+
covariant = cls_is_descendant(cand_cls, base_cls)19151+
end19152+
if covariant == 119153+
ol = ol + 119154+
else19155+
rt_ok = 019156+
ol = ovr_list.length19157+
end1913619158
else1913719159
ol = ol + 11913819160
end@@ -19158,7 +19180,17 @@
1915819180
cand_name = @cls_names[cand_owner2]1915919181
cand_recv = "(sp_" + cand_name + " *)" + self_expr1916019182
cand_call = "sp_" + cand_name + "_" + sanitize_name(mname) + "(" + cand_recv + build_call_tail(ca, bp, yargs) + ")"19161-
emit(" case " + cand_cid.to_s + "LL: " + tmp + " = " + cand_call + "; break;")19183+
# Upcast covariant object returns to the base C type: the19184+
# arm's call yields a subtype pointer (sp_Sub *) while tmp is19185+
# the base type (sp_Base *). Only emit the cast when the C19186+
# types actually differ, so an exact-match arm keeps full19187+
# compiler diagnostics (matz/spinel#1277).19188+
cand_arm_rt = cls_method_return(cand_owner2, mname)19189+
arm_val = cand_call19190+
if c_type(cand_arm_rt) != rt_c19191+
arm_val = "(" + rt_c + ")" + cand_call19192+
end19193+
emit(" case " + cand_cid.to_s + "LL: " + tmp + " = " + arm_val + "; break;")1916219194
ol2 = ol2 + 11916319195
end1916419196
emit(" default: " + tmp + " = " + default_call + "; break;")