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 end
1060210602 end
1060310603 end
10604+ # Forward-ref dispatch: when the receiver is statically `int`
10605+ # (a yet-untyped ivar / param read whose true class hasn't
10606+ # propagated through the inference fixpoint yet) but `mname`
10607+ # belongs to exactly one user class — and isn't shared with a
10608+ # primitive type's method of the same name — the int→class
10609+ # fallback at the bottom of compile_no_recv_call_expr will
10610+ # dispatch the call to that class's C function. Widen the
10611+ # callee's param types from this site's args so the C
10612+ # signatures match the values the fallback passes. Without
10613+ # this, file orderings that put the caller (e.g. `rom.rb`'s
10614+ # `@ppu.set_chr_mem(@chr_ref, @chr_ram)`) before the callee
10615+ # (PPU) leave the callee's params at the default `mrb_int`,
10616+ # producing Wint-conversion / incompatible-pointer errors at
10617+ # the int→class call site. unify_call_types collapses to
10618+ # poly when later sites disagree.
10619+ #
10620+ # Gate on the receiver being an ivar / local read — those are
10621+ # the only shapes the fallback realistically widens through.
10622+ # Skipping CallNode / IntegerNode / etc. avoids accidentally
10623+ # treating `(self <=> other) > 0` (where the recv is the int
10624+ # result of `<=>` and `>` is genuinely an int operator) as a
10625+ # forward-ref to `Temperature#>`, which would unify the
10626+ # already-correct `obj_Temperature` param against the literal
10627+ # `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) == 0
10631+ matched_ci_fwd = -1
10632+ ci_fwd = 0
10633+ while ci_fwd < @cls_names.length
10634+ if cls_find_method_direct(ci_fwd, mname) >= 0
10635+ if matched_ci_fwd >= 0
10636+ matched_ci_fwd = -2
10637+ break
10638+ end
10639+ matched_ci_fwd = ci_fwd
10640+ end
10641+ ci_fwd = ci_fwd + 1
10642+ end
10643+ if matched_ci_fwd >= 0
10644+ 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 >= 0
10647+ 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.length
10650+ ptypes_fwd = all_ptypes_fwd[midx_fwd].split(",")
10651+ kk_fwd = 0
10652+ while kk_fwd < arg_ids_fwd.length
10653+ at_fwd = infer_type(arg_ids_fwd[kk_fwd])
10654+ if kk_fwd < ptypes_fwd.length
10655+ ptypes_fwd[kk_fwd] = unify_call_types(ptypes_fwd[kk_fwd], at_fwd, arg_ids_fwd[kk_fwd])
10656+ end
10657+ kk_fwd = kk_fwd + 1
10658+ end
10659+ all_ptypes_fwd[midx_fwd] = ptypes_fwd.join(",")
10660+ @cls_meth_ptypes[matched_ci_fwd] = all_ptypes_fwd.join("|")
10661+ end
10662+ end
10663+ end
10664+ end
1060410665 # Poly-receiver widening: when the receiver is poly (e.g. a
1060510666 # method param widened to accept multiple user classes),
1060610667 # the per-class arms emitted by compile_poly_method_call