Commit 75d32b64

75d32b64f405Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): cast super-call args to parent's declared param types

When a subclass's auto-generated initialize (or an explicit super(...) call) forwards an arg whose static type narrowed through child-side inference but the parent's recorded param type is wider/different, the C call would type-mismatch the arg.

Look up the parent's init signature; for each forwarded arg whose static type doesn't match the parent's recorded type, prefix a cast to the parent's declared C type. Both the auto-forwarding path and the explicit-args path get the cast.

Details

Commit
75d32b64f405
Parent
60f1b75ea8d0

spinel_codegen.rbmodifiedView file

@@ -13803,6 +13803,23 @@
1380313803 if @cls_parents[ci] != ""
1380413804 pi = find_class_idx(@cls_parents[ci])
1380513805 if pi >= 0
13806+ # Look up the parent's init signature so we can cast
13807+ # each forwarded arg to the parent's declared type
13808+ # when it differs (parent's param-type inference may
13809+ # not see this child's type narrowing).
13810+ p_pnames = "".split(",")
13811+ p_ptypes = "".split(",")
13812+ p_init_idx = cls_find_method_direct(pi, "initialize")
13813+ if p_init_idx >= 0
13814+ p_all_params = @cls_meth_params[pi].split("|")
13815+ p_all_ptypes = @cls_meth_ptypes[pi].split("|")
13816+ if p_init_idx < p_all_params.length
13817+ p_pnames = p_all_params[p_init_idx].split(",")
13818+ end
13819+ if p_init_idx < p_all_ptypes.length
13820+ p_ptypes = p_all_ptypes[p_init_idx].split(",")
13821+ end
13822+ end
1380613823 super_args = ""
1380713824 if @nd_type[sid] == "ForwardingSuperNode"
1380813825 # Forward the current init's params 1:1.
@@ -13811,7 +13828,13 @@
1381113828 if fk > 0
1381213829 super_args = super_args + ", "
1381313830 end
13814- super_args = super_args + "lv_" + pnames[fk]
13831+ arg_expr = "lv_" + pnames[fk]
13832+ if fk < p_ptypes.length && fk < ptypes.length
13833+ if p_ptypes[fk] != ptypes[fk] && p_ptypes[fk] != ""
13834+ arg_expr = "(" + c_type(p_ptypes[fk]) + ")" + arg_expr
13835+ end
13836+ end
13837+ super_args = super_args + arg_expr
1381513838 fk = fk + 1
1381613839 end
1381713840 else
@@ -13823,7 +13846,14 @@
1382313846 if ak > 0
1382413847 super_args = super_args + ", "
1382513848 end
13826- super_args = super_args + compile_expr(arg_ids[ak])
13849+ arg_expr2 = compile_expr(arg_ids[ak])
13850+ if ak < p_ptypes.length
13851+ at_arg = infer_type(arg_ids[ak])
13852+ if p_ptypes[ak] != at_arg && p_ptypes[ak] != ""
13853+ arg_expr2 = "(" + c_type(p_ptypes[ak]) + ")" + arg_expr2
13854+ end
13855+ end
13856+ super_args = super_args + arg_expr2
1382713857 ak = ak + 1
1382813858 end
1382913859 end