Commit ef8ecbc5

ef8ecbc5ecf7Merged

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

Description

Register included-module methods on reopened built-ins (fixes #1036)

class Integer; include M; end (and the same on String/Float/...) left M's instance methods undispatchable on the primitive: the open-class branch in collect_class_with_prefix registered the built-in's own defs as __oc_<Class>_<m> top-level functions but only walked DefNodes and returned, silently dropping any include. So 42.to_words fell through to "cannot resolve ... on int" while a direct def to_words on Integer worked.

Route include M inside a reopened built-in through the same __oc_<Class>_<m> registration the direct-def path uses (register_open_class_def, factored out and shared). def self.x module functions are skipped and names already registered are not duplicated.

Together with the constant-alias reopen pre-pass (9aeb124), the to_words gem's INTEGER_KLASS = 1.class; class INTEGER_KLASS; include ToWords; end now yields 42.to_words correctly instead of 0.

Fixes #1036

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

Details

Commit
ef8ecbc5ecf7
Parent
25442aaf8464

spinel_analyze.rbmodifiedView file

@@ -8919,6 +8919,70 @@
89198919 @const_scope_names.push(scope_name)
89208920 end
89218921
8922+ # Register a single method as an open-class top-level function
8923+ # (`__oc_<cname>_<mname>`) -- the form primitive dispatch resolves for
8924+ # built-in reopens (`class Integer; def to_words`). Shared by the
8925+ # direct-def and include-into-built-in paths.
8926+ def register_open_class_def(cname, mname, sid)
8927+ @meth_names.push("__oc_" + cname + "_" + mname)
8928+ @meth_param_names.push(collect_params_str(sid))
8929+ @meth_param_types.push(collect_ptypes_str(sid, -1))
8930+ @meth_param_empty.push("")
8931+ @meth_return_types.push("int")
8932+ @meth_body_ids.push(@nd_body[sid])
8933+ @meth_has_yield.push(0)
8934+ @meth_has_defaults.push("0")
8935+ @meth_rest_index.push(collect_rest_index(sid))
8936+ @meth_kwrest_index.push(collect_kwrest_index(sid))
8937+ end
8938+
8939+ # Register the instance methods of an included module as open-class
8940+ # functions on a reopened built-in (`class Integer; include M`). Skips
8941+ # `def self.x` module functions and names already registered (so a
8942+ # direct def or a second include doesn't duplicate the C function).
8943+ def register_open_class_module_methods(cname, inc_nid, module_prefix)
8944+ inc_t = @nd_type[inc_nid]
8945+ if inc_t != "ConstantReadNode" && inc_t != "ConstantPathNode"
8946+ return
8947+ end
8948+ mod_name = const_ref_flat_name(inc_nid)
8949+ if mod_name == ""
8950+ return
8951+ end
8952+ eff_prefix = module_prefix
8953+ if const_ref_is_relative(inc_nid) == 0
8954+ eff_prefix = ""
8955+ end
8956+ resolved_mod = resolve_include_module_name(mod_name, eff_prefix)
8957+ mi = 0
8958+ while mi < @module_names.length
8959+ if @module_names[mi] == resolved_mod
8960+ mbody = @module_body_ids[mi]
8961+ if mbody >= 0
8962+ mstmts = get_stmts(mbody)
8963+ mk = 0
8964+ while mk < mstmts.length
8965+ sid = mstmts[mk]
8966+ if @nd_type[sid] == "DefNode"
8967+ is_cls_def = 0
8968+ if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"
8969+ is_cls_def = 1
8970+ end
8971+ if is_cls_def == 0
8972+ ocfull = "__oc_" + cname + "_" + @nd_name[sid]
8973+ if not_in(ocfull, @meth_names) == 1
8974+ register_open_class_def(cname, @nd_name[sid], sid)
8975+ end
8976+ end
8977+ end
8978+ mk = mk + 1
8979+ end
8980+ end
8981+ end
8982+ mi = mi + 1
8983+ end
8984+ end
8985+
89228986 def collect_class_with_prefix(nid, module_prefix)
89238987 ci = @cls_names.length
89248988 cname = ""
@@ -8941,20 +9005,22 @@
89419005 body_stmts = get_stmts(body)
89429006 body_stmts.each { |sid|
89439007 if @nd_type[sid] == "DefNode"
8944- # Add as top-level method with prefix
8945- mname = @nd_name[sid]
8946- # Store with special naming for lookup
8947- @meth_names.push("__oc_" + cname + "_" + mname)
8948- params = collect_params_str(sid)
8949- @meth_param_names.push(params)
8950- @meth_param_types.push(collect_ptypes_str(sid, -1))
8951- @meth_param_empty.push("")
8952- @meth_return_types.push("int")
8953- @meth_body_ids.push(@nd_body[sid])
8954- @meth_has_yield.push(0)
8955- @meth_has_defaults.push("0")
8956- @meth_rest_index.push(collect_rest_index(sid))
8957- @meth_kwrest_index.push(collect_kwrest_index(sid))
9008+ register_open_class_def(cname, @nd_name[sid], sid)
9009+ end
9010+ # `include M` inside a reopened built-in: the module's instance
9011+ # methods must also become `__oc_<cname>_<m>` top-level functions so
9012+ # primitive dispatch (e.g. `42.to_words`) finds them, exactly like a
9013+ # direct `def`. Without this the include was silently dropped here.
9014+ if @nd_type[sid] == "CallNode" && @nd_name[sid] == "include"
9015+ inc_args_oc = @nd_arguments[sid]
9016+ if inc_args_oc >= 0
9017+ inc_ids_oc = get_args(inc_args_oc)
9018+ iko = 0
9019+ while iko < inc_ids_oc.length
9020+ register_open_class_module_methods(cname, inc_ids_oc[iko], module_prefix)
9021+ iko = iko + 1
9022+ end
9023+ end
89589024 end
89599025 }
89609026 end