Commit cfd3bd79

cfd3bd793e6bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

analyze: register define_method block as a class instance method

`define_method(:name) { ... }` inside a class body silently dropped on the floor -- the analyzer's class-body scan only handled DefNode + a small set of CallNode names (include, private, attr_*). Calls to the dynamically defined method then fell through to the "cannot resolve" warning and emitted 0.

Add a "define_method" arm to the class-body CallNode scan that routes through a new collect_define_method_in_class helper. It mirrors the existing top-level collect_define_method but appends to the per-class method tables via append_cls_meth, so the method participates in the same static dispatch as a regular `def name`.

Block params with required positionals (`|a, b|`) carry through; default arity stays "int" matching the top-level collector.

Fixes #718

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
cfd3bd793e6b
Parent
470384e41da1

spinel_analyze.rbmodifiedView file

@@ -8221,7 +8221,11 @@
82218221 end
82228222 if @nd_type[sid] == "CallNode"
82238223 cn = @nd_name[sid]
8224- if cn != "include"
8224+ if cn == "define_method"
8225+ # Issue #718: define_method(:name) { ... } in a class body
8226+ # registers an instance method on the surrounding class.
8227+ collect_define_method_in_class(ci, sid)
8228+ elsif cn != "include"
82258229 if cn != "private"
82268230 collect_attr_call(ci, sid)
82278231 end
@@ -10662,6 +10666,51 @@
1066210666 0
1066310667 end
1066410668
10669+ # Issue #718: `define_method(:name) { ... }` inside a class body
10670+ # registers an instance method on that class. Mirrors
10671+ # collect_define_method (which handles the top-level form) but
10672+ # routes through append_cls_meth so the method participates in
10673+ # per-class dispatch.
10674+ def collect_define_method_in_class(ci, nid)
10675+ args_id = @nd_arguments[nid]
10676+ if args_id < 0
10677+ return
10678+ end
10679+ arg_ids = get_args(args_id)
10680+ if arg_ids.length < 1
10681+ return
10682+ end
10683+ mname = @nd_content[arg_ids[0]]
10684+ if mname == ""
10685+ mname = @nd_name[arg_ids[0]]
10686+ end
10687+ blk = @nd_block[nid]
10688+ if blk < 0
10689+ return
10690+ end
10691+ body_id = @nd_body[blk]
10692+ params_str = ""
10693+ ptypes_str = ""
10694+ bp = @nd_parameters[blk]
10695+ if bp >= 0
10696+ inner = @nd_parameters[bp]
10697+ if inner >= 0
10698+ reqs = parse_id_list(@nd_requireds[inner])
10699+ k = 0
10700+ while k < reqs.length
10701+ if params_str != ""
10702+ params_str = params_str + ","
10703+ ptypes_str = ptypes_str + ","
10704+ end
10705+ params_str = params_str + @nd_name[reqs[k]]
10706+ ptypes_str = ptypes_str + "int"
10707+ k = k + 1
10708+ end
10709+ end
10710+ end
10711+ append_cls_meth(ci, mname, params_str, ptypes_str, "int", body_id, "")
10712+ end
10713+
1066510714 def collect_define_method(nid)
1066610715 # define_method(:name) { |args| body }
1066710716 args_id = @nd_arguments[nid]