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 >= 013806+
# Look up the parent's init signature so we can cast13807+
# each forwarded arg to the parent's declared type13808+
# when it differs (parent's param-type inference may13809+
# 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 >= 013814+
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.length13817+
p_pnames = p_all_params[p_init_idx].split(",")13818+
end13819+
if p_init_idx < p_all_ptypes.length13820+
p_ptypes = p_all_ptypes[p_init_idx].split(",")13821+
end13822+
end1380613823
super_args = ""1380713824
if @nd_type[sid] == "ForwardingSuperNode"1380813825
# Forward the current init's params 1:1.@@ -13811,7 +13828,13 @@
1381113828
if fk > 01381213829
super_args = super_args + ", "1381313830
end13814-
super_args = super_args + "lv_" + pnames[fk]13831+
arg_expr = "lv_" + pnames[fk]13832+
if fk < p_ptypes.length && fk < ptypes.length13833+
if p_ptypes[fk] != ptypes[fk] && p_ptypes[fk] != ""13834+
arg_expr = "(" + c_type(p_ptypes[fk]) + ")" + arg_expr13835+
end13836+
end13837+
super_args = super_args + arg_expr1381513838
fk = fk + 11381613839
end1381713840
else@@ -13823,7 +13846,14 @@
1382313846
if ak > 01382413847
super_args = super_args + ", "1382513848
end13826-
super_args = super_args + compile_expr(arg_ids[ak])13849+
arg_expr2 = compile_expr(arg_ids[ak])13850+
if ak < p_ptypes.length13851+
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_expr213854+
end13855+
end13856+
super_args = super_args + arg_expr21382713857
ak = ak + 11382813858
end1382913859
endtest/super_arg_cross_class_cast.rbaddedView file
Loading diff…
test/super_arg_cross_class_cast.rb.expectedaddedView file
Loading diff…