Commit fe4eafb6

fe4eafb67f84Merged

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

Description

Const-fold Module#instance_methods(false) to a literal symbol array

Klass.instance_methods(false) (and Module#instance_methods(false)) was unimplemented: the call fell through to the generic class-receiver path and raised undefined method 'instance_methods'. The own (non- inherited) instance-method set of a compiled user class/module is statically known -- it's the method table the compiler already holds -- so this folds at compile time to a literal symbol array, with no runtime reflection machinery (no define_method / method_missing / send). This unblocks introspection that gems' own test runners lean on to self-discover test_* methods.

Implementation, with a single shared gate so the two passes can't drift:

  • compiler_helpers.rb: shared, context-free helpers instance_methods_fold_target (gate: ConstantRead receiver + a literal false arg + a known user class/module) and instance_methods_own_names (class: method table + attr readers + =-suffixed writers, matching class_has_method_local; module: body defs minus def self.x and module_function promotions, plus attr accessors).
  • analyze: register the own-method names in collect_sym_names so the array elements resolve to static sp_sym ids, and type the call sym_array.
  • codegen: emit the sym_array literal (sp_IntArray of sp_sym).

Scope (gate is identical on both sides, so anything unfolded stays untyped and degrades exactly as before -- no corrupt sym_array slot):

  • Only the literal (false) form folds. The no-arg / true (inherited) form can't be folded -- it would need Object/Kernel's full native method table, which the compiler doesn't model -- so it falls through unchanged.
  • ConstantPath receivers (A::B.instance_methods(false)) fall through (context-free resolution only matches top-level names).
  • spinel models no method visibility, so private defs are included (consistent with method_defined?).

Built-in classes aren't in @cls_names, so Integer.instance_methods( false) etc. correctly fall through rather than reporting a wrong set.

Full suite green (704) and optcarrot checksum unchanged (59662).

Fixes #1073

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

Details

Commit
fe4eafb67f84
Parent
12c49fd1bbe5

compiler_helpers.rbmodifiedView file

@@ -991,4 +991,175 @@
991991 end
992992 end
993993
994+ # ---- Module#instance_methods(false) const-fold support ----
995+ # `Klass.instance_methods(false)` reports a class/module's own (non-
996+ # inherited) instance-method names. For a *compiled* user class/module
997+ # that set is statically known, so the call folds to a literal symbol
998+ # array. These helpers are shared so the analyze pass (symbol-table
999+ # registration + result typing) and the codegen pass (the fold) agree
1000+ # exactly -- a name set or gate that drifted between passes would emit
1001+ # a `sym_array`-typed slot the other side can't populate.
1002+
1003+ # Resolved const name iff `nid` is a foldable
1004+ # `<ConstantRead>.instance_methods(false)` call, else "". Receiver
1005+ # resolution is context-free (the raw constant name) on purpose: the
1006+ # gate must be identical in a flat symbol-collection walk and in a
1007+ # class-scoped emit, so it deliberately does NOT consult lexical
1008+ # scope. ConstantPath receivers and the no-arg / `true` forms fall
1009+ # through unfolded (and stay untyped), matching today's behaviour.
1010+ def instance_methods_fold_target(nid)
1011+ recv = @nd_receiver[nid]
1012+ if recv < 0 || @nd_type[recv] != "ConstantReadNode"
1013+ return ""
1014+ end
1015+ aid = @nd_arguments[nid]
1016+ if aid < 0
1017+ return ""
1018+ end
1019+ aargs = get_args(aid)
1020+ if aargs.length != 1 || @nd_type[aargs[0]] != "FalseNode"
1021+ return ""
1022+ end
1023+ cn = @nd_name[recv]
1024+ if instance_methods_fold_const?(cn) == 0
1025+ return ""
1026+ end
1027+ cn
1028+ end
1029+
1030+ # 1 iff `cname` is a user-defined class or module whose own instance-
1031+ # method set is statically known. Built-in classes (Integer, String,
1032+ # ...) aren't in @cls_names, so they fall through -- spinel can't
1033+ # enumerate their native method tables.
1034+ def instance_methods_fold_const?(cname)
1035+ if cname == ""
1036+ return 0
1037+ end
1038+ if find_class_idx(cname) >= 0
1039+ return 1
1040+ end
1041+ mi = 0
1042+ while mi < @module_names.length
1043+ if @module_names[mi] == cname
1044+ return 1
1045+ end
1046+ mi = mi + 1
1047+ end
1048+ 0
1049+ end
1050+
1051+ # Ordered, de-duplicated own instance-method names (as strings) for a
1052+ # foldable const. Class: attr readers + `=`-suffixed writers + method
1053+ # table (matches class_has_method_local, which method_defined? uses).
1054+ # Module: body `def`s excluding `def self.x` and module_function
1055+ # promotions, plus attr accessors. spinel doesn't model visibility, so
1056+ # private defs are included -- consistent with method_defined?.
1057+ def instance_methods_own_names(cname)
1058+ acc = "".split(";", -1)
1059+ ci = find_class_idx(cname)
1060+ if ci >= 0
1061+ im_push_names(acc, @cls_attr_readers[ci].split(";", -1), "")
1062+ im_push_names(acc, @cls_attr_writers[ci].split(";", -1), "=")
1063+ im_push_names(acc, @cls_meth_names[ci].split(";", -1), "")
1064+ return acc
1065+ end
1066+ mi = 0
1067+ while mi < @module_names.length
1068+ if @module_names[mi] == cname && mi < @module_body_ids.length
1069+ im_push_module_body(acc, @module_body_ids[mi])
1070+ end
1071+ mi = mi + 1
1072+ end
1073+ acc
1074+ end
1075+
1076+ def im_push_names(acc, names, suffix)
1077+ k = 0
1078+ while k < names.length
1079+ if names[k] != ""
1080+ full = names[k] + suffix
1081+ if not_in(full, acc) == 1
1082+ acc.push(full)
1083+ end
1084+ end
1085+ k = k + 1
1086+ end
1087+ end
1088+
1089+ def im_push_module_body(acc, body)
1090+ if body < 0
1091+ return
1092+ end
1093+ stmts = get_stmts(body)
1094+ # Pre-scan `module_function :a, :b` named promotions: those defs
1095+ # become class methods, not instance methods.
1096+ mf_named = "".split(";", -1)
1097+ si = 0
1098+ while si < stmts.length
1099+ sid = stmts[si]
1100+ if @nd_type[sid] == "CallNode" && @nd_receiver[sid] < 0 && @nd_name[sid] == "module_function"
1101+ aid = @nd_arguments[sid]
1102+ if aid >= 0
1103+ margs = get_args(aid)
1104+ mk = 0
1105+ while mk < margs.length
1106+ if @nd_type[margs[mk]] == "SymbolNode" || @nd_type[margs[mk]] == "StringNode"
1107+ mf_named.push(@nd_content[margs[mk]])
1108+ end
1109+ mk = mk + 1
1110+ end
1111+ end
1112+ end
1113+ si = si + 1
1114+ end
1115+ # `module_function` (no args) flips subsequent bare `def`s into
1116+ # class methods. `def self.x` is always a class method.
1117+ in_mf = 0
1118+ si = 0
1119+ while si < stmts.length
1120+ sid = stmts[si]
1121+ t = @nd_type[sid]
1122+ if t == "CallNode" && @nd_receiver[sid] < 0
1123+ cn = @nd_name[sid]
1124+ if cn == "module_function"
1125+ aid2 = @nd_arguments[sid]
1126+ if aid2 < 0 || get_args(aid2).length == 0
1127+ in_mf = 1
1128+ end
1129+ end
1130+ if cn == "attr_reader" || cn == "attr_accessor"
1131+ im_push_attr_args(acc, sid, "")
1132+ end
1133+ if cn == "attr_writer" || cn == "attr_accessor"
1134+ im_push_attr_args(acc, sid, "=")
1135+ end
1136+ end
1137+ if t == "DefNode" && @nd_receiver[sid] < 0 && in_mf == 0
1138+ dn = @nd_name[sid]
1139+ if not_in(dn, mf_named) == 1 && not_in(dn, acc) == 1
1140+ acc.push(dn)
1141+ end
1142+ end
1143+ si = si + 1
1144+ end
1145+ end
1146+
1147+ def im_push_attr_args(acc, sid, suffix)
1148+ aid = @nd_arguments[sid]
1149+ if aid < 0
1150+ return
1151+ end
1152+ aargs = get_args(aid)
1153+ k = 0
1154+ while k < aargs.length
1155+ if @nd_type[aargs[k]] == "SymbolNode" || @nd_type[aargs[k]] == "StringNode"
1156+ full = @nd_content[aargs[k]] + suffix
1157+ if not_in(full, acc) == 1
1158+ acc.push(full)
1159+ end
1160+ end
1161+ k = k + 1
1162+ end
1163+ end
1164+
9941165 end