Commit 42cedd53

42cedd53e021Merged

Sam Ruby <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Dispatch implicit-self calls with covariant object return types

An implicit-self call to an overridable method, made inside an inherited base-class instance method, was compiled as a static call to the base implementation (no cls_id dispatch) when the override returned a subtype of the base method's declared return. The subclass override silently never ran. This surfaces when an RBS signature pins the base method's return to the base class while the subclass override returns the concrete subclass; without --rbs, inference unifies the return types and hides it, so the RBS signature regressed otherwise-working dispatch.

compile_no_recv_call_expr gated the dispatch switch on every override's return C type exactly equalling the base's. Accept a covariant object return (override class is a descendant of the base's declared return class) and upcast the override result to the base C type in the switch arm (a no-op when types already match). Non-covariant mismatches still correctly suppress the switch.

Fixes #1277.

Details

Commit
42cedd53e021
Parent
6d94883c4383

spinel_codegen.rbmodifiedView file

@@ -19131,8 +19131,24 @@
1913119131 cand_owner = p[1].to_i
1913219132 cand_rt = cls_method_return(cand_owner, mname)
1913319133 if c_type(cand_rt) != base_rt_c
19134- rt_ok = 0
19135- ol = ovr_list.length
19134+ # Covariant object return: the override returns a proper
19135+ # subtype of the base's declared return (e.g. an RBS base
19136+ # `() -> Base` with a subclass `() -> Sub`, or `self`-
19137+ # returning per-model primitives). The C types differ
19138+ # (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 method
19141+ # suppresses dispatch entirely (matz/spinel#1277).
19142+ base_bt = base_type(base_rt)
19143+ cand_bt = base_type(cand_rt)
19144+ if is_obj_type(base_bt) == 1 && is_obj_type(cand_bt) == 1 &&
19145+ cls_is_descendant(find_class_idx(cand_bt[4, cand_bt.length - 4]),
19146+ find_class_idx(base_bt[4, base_bt.length - 4])) == 1
19147+ ol = ol + 1
19148+ else
19149+ rt_ok = 0
19150+ ol = ovr_list.length
19151+ end
1913619152 else
1913719153 ol = ol + 1
1913819154 end
@@ -19158,7 +19174,11 @@
1915819174 cand_name = @cls_names[cand_owner2]
1915919175 cand_recv = "(sp_" + cand_name + " *)" + self_expr
1916019176 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;")
19177+ # Upcast the override result to the base return C type:
19178+ # with a covariant object return the arm's call yields a
19179+ # subtype pointer (sp_Sub *) while tmp is the base type
19180+ # (sp_Base *). A no-op when the types already match.
19181+ emit(" case " + cand_cid.to_s + "LL: " + tmp + " = (" + rt_c + ")" + cand_call + "; break;")
1916219182 ol2 = ol2 + 1
1916319183 end
1916419184 emit(" default: " + tmp + " = " + default_call + "; break;")