Commit 940dc298

940dc2986209Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 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
940dc2986209
Parent
fa72f5e3d3df

spinel_analyze.rbmodifiedView file

@@ -2713,6 +2713,49 @@
27132713 if mi_user >= 0
27142714 return @meth_return_types[mi_user]
27152715 end
2716+ # Issue #405: bare call inside a `def self.X` body resolves to
2717+ # a sibling cmeth on the same class/module. Two signals reach
2718+ # here at different stages: inference sets @current_method_name
2719+ # to "<Class>_cls_<m>" (so the marker scan succeeds); emission
2720+ # sets only @current_method_has_self == 0 + @current_class_idx
2721+ # for real-class cmeths (with @current_method_name = plain
2722+ # mname, no marker).
2723+ if @current_method_name != ""
2724+ mark_cm_405 = @current_method_name.index("_cls_")
2725+ if mark_cm_405 != nil && mark_cm_405 >= 0
2726+ owning_cm_405 = @current_method_name[0, mark_cm_405]
2727+ if module_name_exists(owning_cm_405) == 1
2728+ synth_cm_405 = owning_cm_405 + "_cls_" + mname
2729+ mi_cm_405 = find_method_idx(synth_cm_405)
2730+ if mi_cm_405 >= 0
2731+ return @meth_return_types[mi_cm_405]
2732+ end
2733+ end
2734+ cci_cm_405 = find_class_idx(owning_cm_405)
2735+ if cci_cm_405 >= 0
2736+ cmnames_cm_405 = @cls_cmeth_names[cci_cm_405].split(";")
2737+ cmreturns_cm_405 = @cls_cmeth_returns[cci_cm_405].split(";")
2738+ cmidx_cm_405 = 0
2739+ while cmidx_cm_405 < cmnames_cm_405.length
2740+ if cmnames_cm_405[cmidx_cm_405] == mname && cmidx_cm_405 < cmreturns_cm_405.length
2741+ return cmreturns_cm_405[cmidx_cm_405]
2742+ end
2743+ cmidx_cm_405 = cmidx_cm_405 + 1
2744+ end
2745+ end
2746+ end
2747+ end
2748+ if @current_class_idx >= 0 && @current_method_has_self == 0
2749+ cmnames_cm_405r = @cls_cmeth_names[@current_class_idx].split(";")
2750+ cmreturns_cm_405r = @cls_cmeth_returns[@current_class_idx].split(";")
2751+ cmidx_cm_405r = 0
2752+ while cmidx_cm_405r < cmnames_cm_405r.length
2753+ if cmnames_cm_405r[cmidx_cm_405r] == mname && cmidx_cm_405r < cmreturns_cm_405r.length
2754+ return cmreturns_cm_405r[cmidx_cm_405r]
2755+ end
2756+ cmidx_cm_405r = cmidx_cm_405r + 1
2757+ end
2758+ end
27162759 end
27172760
27182761 # Method name-based type inference
@@ -9358,6 +9401,53 @@
93589401 end
93599402 end
93609403 end
9404+ # Issue #405: bare call inside a `def self.X` body resolves
9405+ # to a sibling cmeth on the same class/module. Widen the
9406+ # sibling's ptypes from this call site's args. Mirrors the
9407+ # explicit-`M.X(args)` widening branch below but keyed off
9408+ # @current_method_name's `<Class>_cls_<m>` marker (set by
9409+ # infer_function_body_call_types / infer_class_body_call_types
9410+ # before walking each cmeth body) since recv is absent here.
9411+ if @current_method_name != ""
9412+ marker_405 = @current_method_name.index("_cls_")
9413+ if marker_405 != nil && marker_405 >= 0
9414+ owning_405 = @current_method_name[0, marker_405]
9415+ cci_405 = find_class_idx(owning_405)
9416+ if cci_405 >= 0
9417+ cmnames_405 = @cls_cmeth_names[cci_405].split(";")
9418+ cmidx_405 = 0
9419+ while cmidx_405 < cmnames_405.length
9420+ if cmnames_405[cmidx_405] == mname
9421+ args_id_405 = @nd_arguments[nid]
9422+ if args_id_405 >= 0
9423+ arg_ids_405 = get_args(args_id_405)
9424+ cmpt_405 = cls_cmeth_ptypes_get(cci_405, cmidx_405)
9425+ if cmpt_405.length > 0
9426+ cmpn_405 = cls_cmeth_pnames_get(cci_405, cmidx_405)
9427+ widen_ptypes_from_args(arg_ids_405, cmpn_405, cmpt_405)
9428+ cls_cmeth_ptypes_put(cci_405, cmidx_405, cmpt_405)
9429+ end
9430+ end
9431+ end
9432+ cmidx_405 = cmidx_405 + 1
9433+ end
9434+ end
9435+ if module_name_exists(owning_405) == 1
9436+ synth_405 = owning_405 + "_cls_" + mname
9437+ sib_mi_405m = find_method_idx(synth_405)
9438+ if sib_mi_405m >= 0
9439+ args_id_405m = @nd_arguments[nid]
9440+ if args_id_405m >= 0
9441+ arg_ids_405m = get_args(args_id_405m)
9442+ ptypes_405m = @meth_param_types[sib_mi_405m].split(",")
9443+ pnames_405m = @meth_param_names[sib_mi_405m].split(",")
9444+ widen_ptypes_from_args(arg_ids_405m, pnames_405m, ptypes_405m)
9445+ @meth_param_types[sib_mi_405m] = ptypes_405m.join(",")
9446+ end
9447+ end
9448+ end
9449+ end
9450+ end
93619451 end
93629452 if @nd_name[nid] == "new"
93639453 recv = @nd_receiver[nid]