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
result end # The enclosing module's name when `self` denotes a module -- i.e. # inside a module body or a `def self.x` module class-method. # current_lexical_scope_name already yields the module name in both # contexts (directly from @current_lexical_scope for a body, or by # stripping `_cls_<m>` off @current_method_name for a class method); # this just gates it on the scope actually being a module so `self` # in a plain class isn't mistaken for a module singleton receiver. # Returns "" when self is not a module. Used to route `self.<acc>` / # `self.<acc> = v` to the same module-singleton-accessor slot that # `Mod.<acc>` resolves to. def current_module_scope scope = current_lexical_scope_name if scope != "" && module_name_exists(scope) == 1 return scope end "" end # ---- AST node table storage (parallel arrays by node ID) ---- # The 46 @nd_* arrays are initialized in each host's `initialize` # and operated on here. Fields are listed explicitly (not viaspinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/module_singleton_self_accessor.rbaddedView file
Loading diff…
test/module_singleton_self_accessor.rb.expectedaddedView file
Loading diff…