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
cand_owner = p[1].to_i cand_rt = cls_method_return(cand_owner, mname) if c_type(cand_rt) != base_rt_c rt_ok = 0 ol = ovr_list.length # Covariant object return: the override returns a proper # subtype of the base's declared return (e.g. an RBS base # `() -> Base` with a subclass `() -> Sub`, or `self`- # returning per-model primitives). The C types differ # (sp_Sub* vs sp_Base*) but the dispatch is still valid — # the switch arm upcasts the result to the base C type. # Without this, an RBS signature on the base method # suppresses dispatch entirely (matz/spinel#1277). base_bt = base_type(base_rt) cand_bt = base_type(cand_rt) if is_obj_type(base_bt) == 1 && is_obj_type(cand_bt) == 1 && cls_is_descendant(find_class_idx(cand_bt[4, cand_bt.length - 4]), find_class_idx(base_bt[4, base_bt.length - 4])) == 1 ol = ol + 1 else rt_ok = 0 ol = ovr_list.length end else ol = ol + 1 end cand_name = @cls_names[cand_owner2] cand_recv = "(sp_" + cand_name + " *)" + self_expr cand_call = "sp_" + cand_name + "_" + sanitize_name(mname) + "(" + cand_recv + build_call_tail(ca, bp, yargs) + ")" emit(" case " + cand_cid.to_s + "LL: " + tmp + " = " + cand_call + "; break;") # Upcast the override result to the base return C type: # with a covariant object return the arm's call yields a # subtype pointer (sp_Sub *) while tmp is the base type # (sp_Base *). A no-op when the types already match. emit(" case " + cand_cid.to_s + "LL: " + tmp + " = (" + rt_c + ")" + cand_call + "; break;") ol2 = ol2 + 1 end emit(" default: " + tmp + " = " + default_call + "; break;")