Commit b885f637

b885f637dd8fMerged

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

Description

Resolve self. to module singleton accessors

self.x / self.x = v for a module-level singleton accessor (class << self; attr_accessor :x) did not resolve: only an explicit Mod.x / Mod.x = v receiver was recognized, so a self receiver typed as int and fell through to the NoMethodError gate. A write in a module body was therefore lost (self.x = [2,3] then M.x read nil).

Add a shared current_module_scope helper (the enclosing module name when self denotes a module -- a module body or a def self.x) and teach the four resolution sites to accept a SelfNode receiver via it, mirroring the existing ConstantReadNode paths: analyze read typing, codegen read (cst__), codegen write, and the body-emission filter (so self.x = v runs at definition time as a direct slot write).

This completes part B of the report; together with the constant interpolation fix (part A) and the body side-effect execution (part B1), the module/class body cases are now handled. The class-level singleton accessor form (class C; class << self; attr_accessor) is a separate pre-existing gap, not addressed here.

Fixes #1256

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

Details

Commit
b885f637dd8f
Parent
7c14aa1022d8

compiler_helpers.rbmodifiedView file

@@ -436,6 +436,24 @@
436436 result
437437 end
438438
439+ # The enclosing module's name when `self` denotes a module -- i.e.
440+ # inside a module body or a `def self.x` module class-method.
441+ # current_lexical_scope_name already yields the module name in both
442+ # contexts (directly from @current_lexical_scope for a body, or by
443+ # stripping `_cls_<m>` off @current_method_name for a class method);
444+ # this just gates it on the scope actually being a module so `self`
445+ # in a plain class isn't mistaken for a module singleton receiver.
446+ # Returns "" when self is not a module. Used to route `self.<acc>` /
447+ # `self.<acc> = v` to the same module-singleton-accessor slot that
448+ # `Mod.<acc>` resolves to.
449+ def current_module_scope
450+ scope = current_lexical_scope_name
451+ if scope != "" && module_name_exists(scope) == 1
452+ return scope
453+ end
454+ ""
455+ end
456+
439457 # ---- AST node table storage (parallel arrays by node ID) ----
440458 # The 46 @nd_* arrays are initialized in each host's `initialize`
441459 # and operated on here. Fields are listed explicitly (not via