Commit c07d880f
c07d880fcf7aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 7 files · +6 added · ~1 modified
Description
Merge pull request #380 from mame/pr/forward-call-param-type
fix(codegen): widen forward-ref user-class method ptypes from int-recv call sites
Details
- Commit
- c07d880fcf7a
- Parent
- 9000016d8a3f
- Merge parent
- 6971e2a092a7
spinel_codegen.rbmodifiedView file
@@ -10601,6 +10601,67 @@
1060110601
end1060210602
end1060310603
end10604+
# Forward-ref dispatch: when the receiver is statically `int`10605+
# (a yet-untyped ivar / param read whose true class hasn't10606+
# propagated through the inference fixpoint yet) but `mname`10607+
# belongs to exactly one user class — and isn't shared with a10608+
# primitive type's method of the same name — the int→class10609+
# fallback at the bottom of compile_no_recv_call_expr will10610+
# dispatch the call to that class's C function. Widen the10611+
# callee's param types from this site's args so the C10612+
# signatures match the values the fallback passes. Without10613+
# this, file orderings that put the caller (e.g. `rom.rb`'s10614+
# `@ppu.set_chr_mem(@chr_ref, @chr_ram)`) before the callee10615+
# (PPU) leave the callee's params at the default `mrb_int`,10616+
# producing Wint-conversion / incompatible-pointer errors at10617+
# the int→class call site. unify_call_types collapses to10618+
# poly when later sites disagree.10619+
#10620+
# Gate on the receiver being an ivar / local read — those are10621+
# the only shapes the fallback realistically widens through.10622+
# Skipping CallNode / IntegerNode / etc. avoids accidentally10623+
# treating `(self <=> other) > 0` (where the recv is the int10624+
# result of `<=>` and `>` is genuinely an int operator) as a10625+
# forward-ref to `Temperature#>`, which would unify the10626+
# already-correct `obj_Temperature` param against the literal10627+
# `0` arg and collapse it to poly.10628+
recv_iow_fwd = @nd_receiver[nid]10629+
recv_is_ivar_or_local = recv_iow_fwd >= 0 && (@nd_type[recv_iow_fwd] == "InstanceVariableReadNode" || @nd_type[recv_iow_fwd] == "LocalVariableReadNode")10630+
if rt == "int" && recv_is_ivar_or_local && primitive_method_shared_with_user_class(mname) == 010631+
matched_ci_fwd = -110632+
ci_fwd = 010633+
while ci_fwd < @cls_names.length10634+
if cls_find_method_direct(ci_fwd, mname) >= 010635+
if matched_ci_fwd >= 010636+
matched_ci_fwd = -210637+
break10638+
end10639+
matched_ci_fwd = ci_fwd10640+
end10641+
ci_fwd = ci_fwd + 110642+
end10643+
if matched_ci_fwd >= 010644+
midx_fwd = cls_find_method_direct(matched_ci_fwd, mname)10645+
args_id_fwd = @nd_arguments[nid]10646+
if args_id_fwd >= 0 && midx_fwd >= 010647+
arg_ids_fwd = get_args(args_id_fwd)10648+
all_ptypes_fwd = @cls_meth_ptypes[matched_ci_fwd].split("|")10649+
if midx_fwd < all_ptypes_fwd.length10650+
ptypes_fwd = all_ptypes_fwd[midx_fwd].split(",")10651+
kk_fwd = 010652+
while kk_fwd < arg_ids_fwd.length10653+
at_fwd = infer_type(arg_ids_fwd[kk_fwd])10654+
if kk_fwd < ptypes_fwd.length10655+
ptypes_fwd[kk_fwd] = unify_call_types(ptypes_fwd[kk_fwd], at_fwd, arg_ids_fwd[kk_fwd])10656+
end10657+
kk_fwd = kk_fwd + 110658+
end10659+
all_ptypes_fwd[midx_fwd] = ptypes_fwd.join(",")10660+
@cls_meth_ptypes[matched_ci_fwd] = all_ptypes_fwd.join("|")10661+
end10662+
end10663+
end10664+
end1060410665
# Poly-receiver widening: when the receiver is poly (e.g. a1060510666
# method param widened to accept multiple user classes),1060610667
# the per-class arms emitted by compile_poly_method_calltest/forward_call_param_type_inference.rbaddedView file
Loading diff…
test/forward_call_param_type_inference.rb.expectedaddedView file
Loading diff…
test/forward_call_param_type_int_array.rbaddedView file
Loading diff…
test/forward_call_param_type_int_array.rb.expectedaddedView file
Loading diff…
test/forward_call_param_type_obj.rbaddedView file
Loading diff…
test/forward_call_param_type_obj.rb.expectedaddedView file
Loading diff…