Commit 83219f2a

83219f2acc5bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified

Description

Module singleton accessor: Stage-1 static constant fold

A module-level class << self; attr_accessor :x; end accessor assigned exactly one constant program-wide (ActiveRecord.adapter = SqliteAdapter) now folds at its read sites: ActiveRecord.adapter.answer compiles to the resolved constant's class method sp_SqliteAdapter_cls_answer().

  • comp_sg_const_binding scans for Class.base = Const writes and returns the constant's class index iff there is exactly one, constant-resolvable write (multiple distinct constants -> polymorphic, left to the runtime path).
  • comp_sg_reader_const resolves a Class.reader read node to that constant.
  • analyze infers the chained call's return type as the resolved class method's; codegen emits the folded class-method call.

Fixes module_singleton_accessor and module_accessor_delete_chain. Polymorphic cases (multiple writes / poly args) remain on the unfolded path.

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

Details

Commit
83219f2acc5b
Parent
db91c5102195

src/analyze.cmodifiedView file

@@ -1086,6 +1086,16 @@
10861086 return TY_INT;
10871087 }
10881088
1089+ /* `Module.accessor.cmethod(...)` where the singleton accessor statically
1090+ folds to a constant (Stage-1): dispatch as that constant's class method. */
1091+ if (recv >= 0) {
1092+ int fold_ci = comp_sg_reader_const(c, recv);
1093+ if (fold_ci >= 0) {
1094+ int mi = comp_cmethod_in_chain(c, fold_ci, name, NULL);
1095+ if (mi >= 0) return c->scopes[mi].ret;
1096+ }
1097+ }
1098+
10891099 /* Class.cmethod(...) / M::Sub.cmethod(...) -> the class method's return type */
10901100 if (recv >= 0) {
10911101 const char *rty = nt_type(nt, recv);