Commit 5d3bcbba

5d3bcbbab6b0Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): #304 module-dispatch ternary widens target params

Two interlocking bugs in the Disp.adapter.method(args) shape where Disp.adapter was assigned one of N candidate classes via a class << self attr_accessor.

(1) module_sentinel(name) only looked up @module_names. The candidates assigned to Disp.adapter are class names (A, B), not module names — every candidate fell through the walk and returned 0. Both write sites (Disp.adapter = A / ... = B) emitted slot = 0 and the read-site dispatch ternary tested slot == 0 for both arms. The first arm always matched and the rest of the chain was unreachable. Extend the lookup with a class-table fallback, offsetting class sentinels above the module-sentinel range so the two namespaces don't collide.

(2) The dispatch ternary calls every candidate's class method, so scan_new_calls must unify the call-site args' types into every candidate's @cls_cmeth_ptypes. The existing widening paths only handled <Class>.method(...) and <Module>.method (...) directly — the chained Module.acc.method(...) shape was missed, so a poly-typed arg (e.g. @id widened to poly by #247) tripped a type mismatch against the candidates' still-narrow mrb_int params. Walk the chain pattern and unify into each candidate class's @cls_cmeth_ptypes.

Side effect of (2): when the unified candidates' return types differ (e.g. one widens to poly, another stays int), the dispatch ternary's default tail (0) and per-arm calls now type-mismatch. Compute the unified return at emit time, default-tail through c_default_val (or sp_box_nil for poly), and box each arm's call through box_value_to_poly when the arm's return is narrower than the unified shape. New helper lookup_cls_meth_return walks both @meth_* (module / synthetic top-level form) and @cls_cmeth_* (in-class def self.X).

Fixes #304

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

Details

Commit
5d3bcbbab6b0
Parent
d7f36aa3bd06

spinel_codegen.rbmodifiedView file

@@ -5283,7 +5283,42 @@
52835283 # Sentinel value for Stage 2 switch dispatch. Each module's index in
52845284 # `@module_names` doubles as its sentinel id; reading `Module` as a
52855285 # value lowers to this integer.
5286+ # Issue #304: look up the return type of a `<class_or_module>.<mname>`
5287+ # singleton method, walking @meth_* (module / synthetic top-level
5288+ # form) and @cls_cmeth_* (in-class `def self.X`). Returns "" when
5289+ # the method isn't registered. Used by the module-dispatch ternary
5290+ # default-tail and per-arm boxing paths.
5291+ def lookup_cls_meth_return(cn, mname)
5292+ mid = find_method_idx(cn + "_cls_" + mname)
5293+ if mid >= 0
5294+ return @meth_return_types[mid]
5295+ end
5296+ cci = find_class_idx(cn)
5297+ if cci >= 0
5298+ cmnames = @cls_cmeth_names[cci].split(";")
5299+ ki = 0
5300+ while ki < cmnames.length
5301+ if cmnames[ki] == mname
5302+ cmrets = @cls_cmeth_returns[cci].split(";")
5303+ if ki < cmrets.length
5304+ return cmrets[ki]
5305+ end
5306+ end
5307+ ki = ki + 1
5308+ end
5309+ end
5310+ ""
5311+ end
5312+
52865313 def module_sentinel(mname)
5314+ # Module-singleton accessors hold either a module reference (rare)
5315+ # or a class reference (the strategy-pattern shape: `Disp.adapter
5316+ # = A` / `Disp.adapter = B`). Both must produce *distinct* sentinel
5317+ # values so the dispatch ternary's per-candidate `slot == sentinel`
5318+ # arms don't all collapse to the same comparison. Issue #304: the
5319+ # class case used to fall through to 0 here, leaving every
5320+ # candidate's `slot == 0` arm matching the never-written default
5321+ # and the rest of the chain unreachable.
52875322 i = 0
52885323 while i < @module_names.length
52895324 if @module_names[i] == mname
@@ -5291,6 +5326,13 @@
52915326 end
52925327 i = i + 1
52935328 end
5329+ ci = find_class_idx(mname)
5330+ if ci >= 0
5331+ # Reserve [1 .. @module_names.length] for module sentinels and
5332+ # offset class sentinels above that range so the two namespaces
5333+ # don't collide.
5334+ return @module_names.length + 1 + ci
5335+ end
52945336 0
52955337 end
52965338
@@ -9453,6 +9495,59 @@
94539495 end
94549496 end
94559497 end
9498+ # Issue #304: module-dispatch ternary call sites
9499+ # (`Disp.adapter.method(args)` where Disp.adapter resolves to N
9500+ # candidate classes via the module-singleton-accessor table).
9501+ # The dispatch ternary calls every candidate's class method, so
9502+ # all of them need the args' types unified into their per-class
9503+ # @cls_cmeth_ptypes entry. Without this the ternary arms type-
9504+ # mismatch when the caller passes a poly value but the targets'
9505+ # params are still mrb_int.
9506+ if @nd_type[nid] == "CallNode" && @nd_receiver[nid] >= 0
9507+ outer_recv_304 = @nd_receiver[nid]
9508+ if @nd_type[outer_recv_304] == "CallNode"
9509+ inner_recv_304 = @nd_receiver[outer_recv_304]
9510+ inner_mname_304 = @nd_name[outer_recv_304]
9511+ if inner_recv_304 >= 0 && @nd_type[inner_recv_304] == "ConstantReadNode"
9512+ mod_name_304 = @nd_name[inner_recv_304]
9513+ if module_name_exists(mod_name_304) == 1
9514+ rconsts_304 = module_acc_resolved(mod_name_304, inner_mname_304)
9515+ if rconsts_304 != "" && rconsts_304 != "?"
9516+ cands_304 = rconsts_304.split(";")
9517+ outer_mname_304 = @nd_name[nid]
9518+ args_id_304 = @nd_arguments[nid]
9519+ if args_id_304 >= 0
9520+ arg_ids_304 = get_args(args_id_304)
9521+ cands_304.each { |cn_304|
9522+ cci_304 = find_class_idx(cn_304)
9523+ if cci_304 >= 0
9524+ cmnames_304 = @cls_cmeth_names[cci_304].split(";")
9525+ cmpall_304 = @cls_cmeth_ptypes[cci_304].split("|")
9526+ cmidx_304 = 0
9527+ while cmidx_304 < cmnames_304.length
9528+ if cmnames_304[cmidx_304] == outer_mname_304 && cmidx_304 < cmpall_304.length
9529+ cmpt_304 = cmpall_304[cmidx_304].split(",")
9530+ kk_304 = 0
9531+ while kk_304 < arg_ids_304.length
9532+ at_304 = infer_type(arg_ids_304[kk_304])
9533+ if kk_304 < cmpt_304.length
9534+ cmpt_304[kk_304] = unify_call_types(cmpt_304[kk_304], at_304, arg_ids_304[kk_304])
9535+ end
9536+ kk_304 = kk_304 + 1
9537+ end
9538+ cmpall_304[cmidx_304] = cmpt_304.join(",")
9539+ @cls_cmeth_ptypes[cci_304] = cmpall_304.join("|")
9540+ end
9541+ cmidx_304 = cmidx_304 + 1
9542+ end
9543+ end
9544+ }
9545+ end
9546+ end
9547+ end
9548+ end
9549+ end
9550+ end
94569551 end
94579552 # Recurse into children
94589553 if @nd_body[nid] >= 0
@@ -19365,14 +19460,42 @@
1936519460 end
1936619461 # Stage 2: sentinel switch. The slot stores the assigned
1936719462 # module's sentinel; we walk the candidate list and dispatch
19368- # the first match. Default `0` mirrors the un-initialised
19369- # slot value (slot was zero-init, never written before read).
19463+ # the first match. The default tail must match the call's
19464+ # return type — when widening pushed the candidates to a
19465+ # poly return (issue #304), a literal `0` would type-mismatch
19466+ # the ternary's other arms. Use c_default_val of the unified
19467+ # return type the type system has already computed for us.
1937019468 slot = "sp_module_" + mod_name + "_" + sanitize_name(inner_mname)
19371- expr = "0"
19469+ ret_t_d = ""
19470+ cands.each { |cn_d|
19471+ rt_d = lookup_cls_meth_return(cn_d, mname)
19472+ if rt_d != ""
19473+ if ret_t_d == ""
19474+ ret_t_d = rt_d
19475+ elsif ret_t_d != rt_d
19476+ ret_t_d = "poly"
19477+ end
19478+ end
19479+ }
19480+ ret_t_d = "int" if ret_t_d == ""
19481+ default_tail = c_default_val(ret_t_d)
19482+ if ret_t_d == "poly"
19483+ @needs_rb_value = 1
19484+ default_tail = "sp_box_nil()"
19485+ end
19486+ expr = default_tail
1937219487 ki = cands.length - 1
1937319488 while ki >= 0
1937419489 cn = cands[ki]
1937519490 call_c = "sp_" + cn + "_cls_" + sanitize_name(mname) + "(" + arg_strs + ")"
19491+ # Box per-arm calls when the unified return is poly but
19492+ # this candidate's specific return is something narrower.
19493+ if ret_t_d == "poly"
19494+ arm_rt = lookup_cls_meth_return(cn, mname)
19495+ if arm_rt != "" && arm_rt != "poly"
19496+ call_c = box_value_to_poly(arm_rt, call_c)
19497+ end
19498+ end
1937619499 expr = "((" + slot + " == " + module_sentinel(cn).to_s + ") ? " + call_c + " : " + expr + ")"
1937719500 ki = ki - 1
1937819501 end