Commit c465f83e

c465f83ed5c2Merged

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

Description

fix(codegen): bare sibling cmeth call inside def self.X resolves

A bare mname(args) inside def self.X previously tripped the "cannot resolve call to 'mname' on (no receiver) (emitting 0)" warning, with the callee's params defaulting to mrb_int because no call site contributed a typing signal. CRuby/JRuby/mruby treat the bare call as self.mname(...), dispatching to the same module / class's other class methods.

Three coordinated changes mirror that:

  • compile_no_recv_call_expr now resolves bare calls against the sibling cmeth tables. Module case: @current_method_name carries the synthetic <Mod>_cls_<m> form; the marker scan extracts the owner and looks the sibling up in @meth_*. Real-class case: emit_class_level_method sets only @current_class_idx + @current_method_has_self = 0 (no marker on @current_method_name), so the second arm gates on those signals and looks the sibling up in @cls_cmeth_names.

  • infer_type's bare-call arm grew the same sibling-resolution path so callers like lv_y = inner(lv_x) infer a string return (instead of the int default), letting scan_locals declare lv_y correctly the first time around.

  • scan_new_calls's CallNode arm grew a parallel sibling-widening block alongside the existing inherited-instance-method one, so the bare call's args feed widen_ptypes_from_args on the callee's @cls_cmeth_ptypes (real class) or @meth_param_types entry (module synth name). Without this the callee's params stayed at mrb_int even after the dispatch resolved.

Test bare_sibling_cls_method_call.rb covers (a) the module shape from the issue (outer cmeth bare-calling a string-typed sibling), (b) the real-class form for the @cls_cmeth_* path, and (c) a multi-hop chain (start -> middle -> finish) so the iterative inference loop has to propagate types past the immediate sibling to cover transitive bare-call shapes.

Closes #405

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

Details

Commit
c465f83ed5c2
Parent
e9cbb408b27d

spinel_codegen.rbmodifiedView file

@@ -3247,6 +3247,49 @@
32473247 if mi_user >= 0
32483248 return @meth_return_types[mi_user]
32493249 end
3250+ # Issue #405: bare call inside a `def self.X` body resolves to
3251+ # a sibling cmeth on the same class/module. Two signals reach
3252+ # here at different stages: inference sets @current_method_name
3253+ # to "<Class>_cls_<m>" (so the marker scan succeeds); emission
3254+ # sets only @current_method_has_self == 0 + @current_class_idx
3255+ # for real-class cmeths (with @current_method_name = plain
3256+ # mname, no marker).
3257+ if @current_method_name != ""
3258+ mark_cm_405 = @current_method_name.index("_cls_")
3259+ if mark_cm_405 != nil && mark_cm_405 >= 0
3260+ owning_cm_405 = @current_method_name[0, mark_cm_405]
3261+ if module_name_exists(owning_cm_405) == 1
3262+ synth_cm_405 = owning_cm_405 + "_cls_" + mname
3263+ mi_cm_405 = find_method_idx(synth_cm_405)
3264+ if mi_cm_405 >= 0
3265+ return @meth_return_types[mi_cm_405]
3266+ end
3267+ end
3268+ cci_cm_405 = find_class_idx(owning_cm_405)
3269+ if cci_cm_405 >= 0
3270+ cmnames_cm_405 = @cls_cmeth_names[cci_cm_405].split(";")
3271+ cmreturns_cm_405 = @cls_cmeth_returns[cci_cm_405].split(";")
3272+ cmidx_cm_405 = 0
3273+ while cmidx_cm_405 < cmnames_cm_405.length
3274+ if cmnames_cm_405[cmidx_cm_405] == mname && cmidx_cm_405 < cmreturns_cm_405.length
3275+ return cmreturns_cm_405[cmidx_cm_405]
3276+ end
3277+ cmidx_cm_405 = cmidx_cm_405 + 1
3278+ end
3279+ end
3280+ end
3281+ end
3282+ if @current_class_idx >= 0 && @current_method_has_self == 0
3283+ cmnames_cm_405r = @cls_cmeth_names[@current_class_idx].split(";")
3284+ cmreturns_cm_405r = @cls_cmeth_returns[@current_class_idx].split(";")
3285+ cmidx_cm_405r = 0
3286+ while cmidx_cm_405r < cmnames_cm_405r.length
3287+ if cmnames_cm_405r[cmidx_cm_405r] == mname && cmidx_cm_405r < cmreturns_cm_405r.length
3288+ return cmreturns_cm_405r[cmidx_cm_405r]
3289+ end
3290+ cmidx_cm_405r = cmidx_cm_405r + 1
3291+ end
3292+ end
32503293 end
32513294
32523295 # Method name-based type inference
@@ -10840,6 +10883,53 @@
1084010883 end
1084110884 end
1084210885 end
10886+ # Issue #405: bare call inside a `def self.X` body resolves
10887+ # to a sibling cmeth on the same class/module. Widen the
10888+ # sibling's ptypes from this call site's args. Mirrors the
10889+ # explicit-`M.X(args)` widening branch below but keyed off
10890+ # @current_method_name's `<Class>_cls_<m>` marker (set by
10891+ # infer_function_body_call_types / infer_class_body_call_types
10892+ # before walking each cmeth body) since recv is absent here.
10893+ if @current_method_name != ""
10894+ marker_405 = @current_method_name.index("_cls_")
10895+ if marker_405 != nil && marker_405 >= 0
10896+ owning_405 = @current_method_name[0, marker_405]
10897+ cci_405 = find_class_idx(owning_405)
10898+ if cci_405 >= 0
10899+ cmnames_405 = @cls_cmeth_names[cci_405].split(";")
10900+ cmidx_405 = 0
10901+ while cmidx_405 < cmnames_405.length
10902+ if cmnames_405[cmidx_405] == mname
10903+ args_id_405 = @nd_arguments[nid]
10904+ if args_id_405 >= 0
10905+ arg_ids_405 = get_args(args_id_405)
10906+ cmpt_405 = cls_cmeth_ptypes_get(cci_405, cmidx_405)
10907+ if cmpt_405.length > 0
10908+ cmpn_405 = cls_cmeth_pnames_get(cci_405, cmidx_405)
10909+ widen_ptypes_from_args(arg_ids_405, cmpn_405, cmpt_405)
10910+ cls_cmeth_ptypes_put(cci_405, cmidx_405, cmpt_405)
10911+ end
10912+ end
10913+ end
10914+ cmidx_405 = cmidx_405 + 1
10915+ end
10916+ end
10917+ if module_name_exists(owning_405) == 1
10918+ synth_405 = owning_405 + "_cls_" + mname
10919+ sib_mi_405m = find_method_idx(synth_405)
10920+ if sib_mi_405m >= 0
10921+ args_id_405m = @nd_arguments[nid]
10922+ if args_id_405m >= 0
10923+ arg_ids_405m = get_args(args_id_405m)
10924+ ptypes_405m = @meth_param_types[sib_mi_405m].split(",")
10925+ pnames_405m = @meth_param_names[sib_mi_405m].split(",")
10926+ widen_ptypes_from_args(arg_ids_405m, pnames_405m, ptypes_405m)
10927+ @meth_param_types[sib_mi_405m] = ptypes_405m.join(",")
10928+ end
10929+ end
10930+ end
10931+ end
10932+ end
1084310933 end
1084410934 if @nd_name[nid] == "new"
1084510935 recv = @nd_receiver[nid]
@@ -24730,6 +24820,74 @@
2473024820 rk = rk + 1
2473124821 end
2473224822 end
24823+ # Issue #405: bare call inside a `def self.X` body resolves to a
24824+ # sibling class method on the same class/module. Without this,
24825+ # `def self.insert(t); rows_for(t)[1] = "x"; end` falls through
24826+ # to warn_unresolved_call and emits 0, while CRuby/JRuby/mruby
24827+ # treat it as `self.rows_for(t)`. Two emit-time signals reach
24828+ # this point: module cmeths set @current_method_name to the
24829+ # synthetic `<Mod>_cls_<m>` form (so the marker scan below
24830+ # succeeds); real-class cmeths set @current_class_idx + plain
24831+ # mname, with @current_method_has_self == 0 distinguishing them
24832+ # from instance methods (which would have already resolved
24833+ # above at the @current_class_idx >= 0 branch).
24834+ if @current_method_name != ""
24835+ cls_idx_marker_405 = @current_method_name.index("_cls_")
24836+ if cls_idx_marker_405 != nil && cls_idx_marker_405 >= 0
24837+ owning_405 = @current_method_name[0, cls_idx_marker_405]
24838+ # Real-class case via the marker (set by infer_*_call_types
24839+ # during inference; emission re-derives via the @current_class_idx
24840+ # arm below).
24841+ cci_405 = find_class_idx(owning_405)
24842+ if cci_405 >= 0
24843+ cmnames_405 = @cls_cmeth_names[cci_405].split(";")
24844+ cmidx_405 = 0
24845+ while cmidx_405 < cmnames_405.length
24846+ if cmnames_405[cmidx_405] == mname
24847+ ca_405 = compile_call_args(nid)
24848+ if ca_405 == ""
24849+ return "sp_" + owning_405 + "_cls_" + sanitize_name(mname) + "()"
24850+ end
24851+ return "sp_" + owning_405 + "_cls_" + sanitize_name(mname) + "(" + ca_405 + ")"
24852+ end
24853+ cmidx_405 = cmidx_405 + 1
24854+ end
24855+ end
24856+ # Module case: cmeth is stored as synthetic `<Mod>_cls_<m>`
24857+ # in @meth_*. find_method_idx already handles the lookup.
24858+ if module_name_exists(owning_405) == 1
24859+ synth_405 = owning_405 + "_cls_" + mname
24860+ mi_405 = find_method_idx(synth_405)
24861+ if mi_405 >= 0
24862+ ca_405m = compile_call_args(nid)
24863+ if ca_405m == ""
24864+ return "sp_" + sanitize_name(synth_405) + "()"
24865+ end
24866+ return "sp_" + sanitize_name(synth_405) + "(" + ca_405m + ")"
24867+ end
24868+ end
24869+ end
24870+ end
24871+ # Real-class cmeth emit path: emit_class_level_method sets
24872+ # @current_method_name to the bare mname (no `_cls_` marker)
24873+ # and @current_method_has_self to 0, distinguishing it from
24874+ # instance methods (which set has_self = 1). Resolve sibling
24875+ # cmeths against @cls_cmeth_names directly.
24876+ if @current_class_idx >= 0 && @current_method_has_self == 0
24877+ cmnames_405r = @cls_cmeth_names[@current_class_idx].split(";")
24878+ cmidx_405r = 0
24879+ while cmidx_405r < cmnames_405r.length
24880+ if cmnames_405r[cmidx_405r] == mname
24881+ ca_405r = compile_call_args(nid)
24882+ owning_405r = @cls_names[@current_class_idx]
24883+ if ca_405r == ""
24884+ return "sp_" + owning_405r + "_cls_" + sanitize_name(mname) + "()"
24885+ end
24886+ return "sp_" + owning_405r + "_cls_" + sanitize_name(mname) + "(" + ca_405r + ")"
24887+ end
24888+ cmidx_405r = cmidx_405r + 1
24889+ end
24890+ end
2473324891 # Unresolved bare-name call (`foobar(0)`, `foobar`). CRuby would
2473424892 # raise NoMethodError; Spinel can't, but warning at codegen so the
2473524893 # user sees the problem is far better than a silently-empty binary.