Commit 6971e2a0

6971e2a092a7Merged

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

Description

fix(codegen): widen forward-ref user-class method ptypes from int-recv call sites

scan_new_calls' receiver-method branch only widens callee param types when the static recv type already resolves to obj_. When the source order puts the caller before the callee — e.g. an ivar read whose constructor-arg path hasn't propagated to obj_ through the inference fixpoint yet — the recv stays at the placeholder int, the unify branch misses, and the callee's params freeze at the default mrb_int. The runtime int→class fallback in compile_no_recv_call_expr still emits the call (it iterates user classes and casts), so the C compile fails with Wint-conversion / incompatible-pointer-types when the call site passes typed pointers.

Add a forward-ref widening branch parallel to the obj-recv one: when recv is int, the recv expression is an ivar / local read, the method name is unique to one user class, and that name isn't shared with a primitive type's method, unify this site's arg types into that single class's ptypes. Picking only ivar / local reads keeps (self <=> other) > 0 (where the recv is the int result of <=> and > is genuinely the int operator) from accidentally unifying the obj-typed param to the literal 0.

This unblocks reordering optcarrot's single-file build to match lib/optcarrot.rb's require order — nes.rb first, rom.rb ahead of ppu.rb, etc. — which lets us drop the FOREVER_CLOCK / RP2A03_CC prepend from the build script.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
6971e2a092a7
Parent
31a59e30af5e

spinel_codegen.rbmodifiedView file

@@ -10663,6 +10663,67 @@
1066310663 end
1066410664 end
1066510665 end
10666+ # Forward-ref dispatch: when the receiver is statically `int`
10667+ # (a yet-untyped ivar / param read whose true class hasn't
10668+ # propagated through the inference fixpoint yet) but `mname`
10669+ # belongs to exactly one user class — and isn't shared with a
10670+ # primitive type's method of the same name — the int→class
10671+ # fallback at the bottom of compile_no_recv_call_expr will
10672+ # dispatch the call to that class's C function. Widen the
10673+ # callee's param types from this site's args so the C
10674+ # signatures match the values the fallback passes. Without
10675+ # this, file orderings that put the caller (e.g. `rom.rb`'s
10676+ # `@ppu.set_chr_mem(@chr_ref, @chr_ram)`) before the callee
10677+ # (PPU) leave the callee's params at the default `mrb_int`,
10678+ # producing Wint-conversion / incompatible-pointer errors at
10679+ # the int→class call site. unify_call_types collapses to
10680+ # poly when later sites disagree.
10681+ #
10682+ # Gate on the receiver being an ivar / local read — those are
10683+ # the only shapes the fallback realistically widens through.
10684+ # Skipping CallNode / IntegerNode / etc. avoids accidentally
10685+ # treating `(self <=> other) > 0` (where the recv is the int
10686+ # result of `<=>` and `>` is genuinely an int operator) as a
10687+ # forward-ref to `Temperature#>`, which would unify the
10688+ # already-correct `obj_Temperature` param against the literal
10689+ # `0` arg and collapse it to poly.
10690+ recv_iow_fwd = @nd_receiver[nid]
10691+ recv_is_ivar_or_local = recv_iow_fwd >= 0 && (@nd_type[recv_iow_fwd] == "InstanceVariableReadNode" || @nd_type[recv_iow_fwd] == "LocalVariableReadNode")
10692+ if rt == "int" && recv_is_ivar_or_local && primitive_method_shared_with_user_class(mname) == 0
10693+ matched_ci_fwd = -1
10694+ ci_fwd = 0
10695+ while ci_fwd < @cls_names.length
10696+ if cls_find_method_direct(ci_fwd, mname) >= 0
10697+ if matched_ci_fwd >= 0
10698+ matched_ci_fwd = -2
10699+ break
10700+ end
10701+ matched_ci_fwd = ci_fwd
10702+ end
10703+ ci_fwd = ci_fwd + 1
10704+ end
10705+ if matched_ci_fwd >= 0
10706+ midx_fwd = cls_find_method_direct(matched_ci_fwd, mname)
10707+ args_id_fwd = @nd_arguments[nid]
10708+ if args_id_fwd >= 0 && midx_fwd >= 0
10709+ arg_ids_fwd = get_args(args_id_fwd)
10710+ all_ptypes_fwd = @cls_meth_ptypes[matched_ci_fwd].split("|")
10711+ if midx_fwd < all_ptypes_fwd.length
10712+ ptypes_fwd = all_ptypes_fwd[midx_fwd].split(",")
10713+ kk_fwd = 0
10714+ while kk_fwd < arg_ids_fwd.length
10715+ at_fwd = infer_type(arg_ids_fwd[kk_fwd])
10716+ if kk_fwd < ptypes_fwd.length
10717+ ptypes_fwd[kk_fwd] = unify_call_types(ptypes_fwd[kk_fwd], at_fwd, arg_ids_fwd[kk_fwd])
10718+ end
10719+ kk_fwd = kk_fwd + 1
10720+ end
10721+ all_ptypes_fwd[midx_fwd] = ptypes_fwd.join(",")
10722+ @cls_meth_ptypes[matched_ci_fwd] = all_ptypes_fwd.join("|")
10723+ end
10724+ end
10725+ end
10726+ end
1066610727 # Poly-receiver widening: when the receiver is poly (e.g. a
1066710728 # method param widened to accept multiple user classes),
1066810729 # the per-class arms emitted by compile_poly_method_call