Commit 9a43cb15

9a43cb152fd8Merged

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

Description

Support module_function with explicit method-name arguments

module_function :foo (the argument form) promotes an already-defined instance method to a module method callable as M.foo. Only the no-arg form (which flips subsequent defs) was handled, so M.foo on a named method raised "undefined method for class". The module-body scan now pre-collects the names listed in argument-form module_function calls and registers those defs as cls, independent of whether the def precedes or follows the call.

Fixes #1035

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

Details

Commit
9a43cb152fd8
Parent
ddbb87544ebe

spinel_analyze.rbmodifiedView file

@@ -12003,6 +12003,27 @@
1200312003 }
1200412004
1200512005 in_module_function = 0
12006+ # `module_function :a, :b` (argument form) promotes already-defined
12007+ # instance methods to module (class) methods. Pre-scan the names so
12008+ # the DefNode pass below registers them as <Mod>_cls_<name>
12009+ # regardless of whether the def precedes or follows the call.
12010+ mf_named = "".split(";", -1)
12011+ body_stmts.each { |sid_mf|
12012+ if @nd_type[sid_mf] == "CallNode" && @nd_receiver[sid_mf] < 0 && @nd_name[sid_mf] == "module_function"
12013+ aid_mf = @nd_arguments[sid_mf]
12014+ if aid_mf >= 0
12015+ aargs_mf = get_args(aid_mf)
12016+ ai_mf = 0
12017+ while ai_mf < aargs_mf.length
12018+ an_mf = aargs_mf[ai_mf]
12019+ if @nd_type[an_mf] == "SymbolNode" || @nd_type[an_mf] == "StringNode"
12020+ mf_named.push(@nd_content[an_mf])
12021+ end
12022+ ai_mf = ai_mf + 1
12023+ end
12024+ end
12025+ end
12026+ }
1200612027 body_stmts.each { |sid|
1200712028 if @nd_type[sid] == "ConstantWriteNode"
1200812029 collect_scoped_constant(mname, sid)
@@ -12028,7 +12049,7 @@
1202812049 if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"
1202912050 is_self_def = 1
1203012051 end
12031- if is_self_def == 1 || (in_module_function == 1 && @nd_receiver[sid] < 0)
12052+ if is_self_def == 1 || (in_module_function == 1 && @nd_receiver[sid] < 0) || (@nd_receiver[sid] < 0 && not_in(@nd_name[sid], mf_named) == 0)
1203212053 dmname = @nd_name[sid]
1203312054 full_dn = mname + "_cls_" + dmname
1203412055 # CRuby's module re-open: last definition wins. If the same
@@ -12080,7 +12101,7 @@
1208012101 if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"
1208112102 is_self_def_iv = 1
1208212103 end
12083- if is_self_def_iv == 1 || (in_module_function == 1 && @nd_receiver[sid] < 0)
12104+ if is_self_def_iv == 1 || (in_module_function == 1 && @nd_receiver[sid] < 0) || (@nd_receiver[sid] < 0 && not_in(@nd_name[sid], mf_named) == 0)
1208412105 dbody = @nd_body[sid]
1208512106 if dbody >= 0
1208612107 register_module_def_ivars(mname, dbody)