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])) == 119144+
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 == 11914719153
ol = ol + 11914819154
else1914919155
rt_ok = 0@@ -19174,11 +19180,17 @@
1917419180
cand_name = @cls_names[cand_owner2]1917519181
cand_recv = "(sp_" + cand_name + " *)" + self_expr1917619182
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 a19179-
# subtype pointer (sp_Sub *) while tmp is the base type19180-
# (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: 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;")1918219194
ol2 = ol2 + 11918319195
end1918419196
emit(" default: " + tmp + " = " + default_call + "; break;")