Commit 97459e90

97459e90443aMerged

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

Description

Address review: conditional cast + named locals in covariant dispatch

  • Emit the upcast only when the arm return C type differs from the base, so exact-match override arms keep full compiler diagnostics.
  • Lift the obj-prefix-stripped class names into locals and a covariant flag rather than inlining the find_class_idx/cls_is_descendant call, for readability.

No behavior change; make test 780/0/0.

Details

Commit
97459e90443a
Parent
42cedd53e021

spinel_codegen.rbmodifiedView file

@@ -19141,9 +19141,15 @@
1914119141 # suppresses dispatch entirely (matz/spinel#1277).
1914219142 base_bt = base_type(base_rt)
1914319143 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
19144+ covariant = 0
19145+ if is_obj_type(base_bt) == 1 && is_obj_type(cand_bt) == 1
19146+ # Strip the "obj_" prefix (codebase idiom) to name the
19147+ # 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+ end
19152+ if covariant == 1
1914719153 ol = ol + 1
1914819154 else
1914919155 rt_ok = 0
@@ -19174,11 +19180,17 @@
1917419180 cand_name = @cls_names[cand_owner2]
1917519181 cand_recv = "(sp_" + cand_name + " *)" + self_expr
1917619182 cand_call = "sp_" + cand_name + "_" + sanitize_name(mname) + "(" + cand_recv + build_call_tail(ca, bp, yargs) + ")"
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;")
19183+ # Upcast covariant object returns to the base C type: the
19184+ # arm's call yields a subtype pointer (sp_Sub *) while tmp is
19185+ # the base type (sp_Base *). Only emit the cast when the C
19186+ # types actually differ, so an exact-match arm keeps full
19187+ # compiler diagnostics (matz/spinel#1277).
19188+ cand_arm_rt = cls_method_return(cand_owner2, mname)
19189+ arm_val = cand_call
19190+ if c_type(cand_arm_rt) != rt_c
19191+ arm_val = "(" + rt_c + ")" + cand_call
19192+ end
19193+ emit(" case " + cand_cid.to_s + "LL: " + tmp + " = " + arm_val + "; break;")
1918219194 ol2 = ol2 + 1
1918319195 end
1918419196 emit(" default: " + tmp + " = " + default_call + "; break;")