Commit d958d311

d958d3114ef1Merged

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

Description

fix(codegen): widen + box module-singleton dispatch through module candidates

Mod.accessor.method(args) (the module-singleton-accessor dispatch from #126/#304) had two paired gaps when the resolved candidate was a module rather than a class:

  1. scan_new_calls' #304 widening reached the per-class @cls_cmeth_ptypes for class candidates only. Module class methods (def self.foo under module_function) live in @meth_* as <Mod>_cls_<m> (mirrors #239's split between @cls_cmeth_* and @meth_*). A poly-arg call site never widened the module candidate's params, so they stayed at the default int while callers passed sp_RbVal.

  2. compile_call_expr's module-accessor branch emitted args via raw compile_expr without consulting the candidate's ptypes. Once #304 widened the ptype to poly, the call site still emitted a mrb_int arg into a sp_RbVal slot — Wint-conversion / hard incompatible-pointer-conversion.

Both gaps fixed in parallel:

  • scan_new_calls (#304 path): added a module_name_exists branch that walks @meth_* for <Mod>_cls_<m> and runs widen_ptypes_from_args on it.
  • compile_call_expr: pre-resolve the candidate's target ptypes (whether class via @cls_cmeth_ptypes or module via @meth_param_types), then box each arg through box_expr_to_poly when the slot's base type is poly.

Surfaced via Roundhouse's ActiveRecord.adapter.update(table, @id, attrs) shape: @id widened to poly across heterogeneous AR-Base subclasses, but InMemoryAdapter.update (a module class method) kept lv_id at mrb_int. matz/spinel#314 follow-up — drops 4-of-5 archive errors per test in one shot (update, delete, find×2 across the 4 emitted tests).

Refs #314

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

Details

Commit
d958d3114ef1
Parent
a015263931da

spinel_codegen.rbmodifiedView file

@@ -10363,6 +10363,22 @@
1036310363 cmidx_304 = cmidx_304 + 1
1036410364 end
1036510365 end
10366+ # Issue #314 follow-up: when the candidate is a
10367+ # module (not a class), its `def self.method` is
10368+ # stored in `@meth_*` as `<Mod>_cls_<m>` rather
10369+ # than in `@cls_cmeth_*`. Mirror #239's parallel
10370+ # branch so a `Mod.accessor.method(args)` call site
10371+ # widens the synthetic top-level function's params.
10372+ if module_name_exists(cn_304) == 1
10373+ mfn_304m = cn_304 + "_cls_" + outer_mname_304
10374+ mi_304m = find_method_idx(mfn_304m)
10375+ if mi_304m >= 0
10376+ pnames_304m = @meth_param_names[mi_304m].split(",")
10377+ ptypes_304m = @meth_param_types[mi_304m].split(",")
10378+ widen_ptypes_from_args(arg_ids_304, pnames_304m, ptypes_304m)
10379+ @meth_param_types[mi_304m] = ptypes_304m.join(",")
10380+ end
10381+ end
1036610382 }
1036710383 end
1036810384 end
@@ -21125,6 +21141,36 @@
2112521141 rconsts = module_acc_resolved(mod_name, inner_mname)
2112621142 if rconsts != "" && rconsts != "?"
2112721143 args_id = @nd_arguments[nid]
21144+ cands = rconsts.split(";")
21145+ # Issue #314 follow-up: scan_new_calls' #304 widening
21146+ # has already unified the candidates' per-arg ptypes,
21147+ # so any candidate's ptypes table tells us the target
21148+ # types here. Box each arg expression to match — without
21149+ # this, an int arg passed to a poly-widened param emits
21150+ # raw `mrb_int` to a `sp_RbVal` slot (Wint-conversion).
21151+ target_ptypes = "".split(",")
21152+ cands.each { |cn_t|
21153+ cci_t = find_class_idx(cn_t)
21154+ if cci_t >= 0
21155+ pall_t = @cls_cmeth_ptypes[cci_t].split("|")
21156+ cmnames_t = @cls_cmeth_names[cci_t].split(";")
21157+ cmidx_t = 0
21158+ while cmidx_t < cmnames_t.length
21159+ if cmnames_t[cmidx_t] == mname && cmidx_t < pall_t.length
21160+ target_ptypes = pall_t[cmidx_t].split(",")
21161+ cmidx_t = cmnames_t.length
21162+ end
21163+ cmidx_t = cmidx_t + 1
21164+ end
21165+ end
21166+ if target_ptypes.length == 0 && module_name_exists(cn_t) == 1
21167+ mfn_t = cn_t + "_cls_" + mname
21168+ mi_t = find_method_idx(mfn_t)
21169+ if mi_t >= 0
21170+ target_ptypes = @meth_param_types[mi_t].split(",")
21171+ end
21172+ end
21173+ }
2112821174 arg_strs = ""
2112921175 if args_id >= 0
2113021176 aargs = get_args(args_id)
@@ -21133,11 +21179,14 @@
2113321179 if k > 0
2113421180 arg_strs = arg_strs + ", "
2113521181 end
21136- arg_strs = arg_strs + compile_expr(aargs[k])
21182+ if k < target_ptypes.length && base_type(target_ptypes[k]) == "poly"
21183+ arg_strs = arg_strs + box_expr_to_poly(aargs[k])
21184+ else
21185+ arg_strs = arg_strs + compile_expr(aargs[k])
21186+ end
2113721187 k = k + 1
2113821188 end
2113921189 end
21140- cands = rconsts.split(";")
2114121190 if cands.length == 1
2114221191 return "sp_" + cands[0] + "_cls_" + sanitize_name(mname) + "(" + arg_strs + ")"
2114321192 end