Commit 8710b3e8
8710b3e87fbdMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): module-level @ivar inside def self.X resolves to module slot (#432)
The InstanceVariableReadNode arm already detected "current emit
scope is a module class method" and rewrote @x to the
module's cst_<Mod>_<x> static slot. The write paths
(InstanceVariableWriteNode, InstanceVariableOperatorWriteNode,
InstanceVariableOrWriteNode, InstanceVariableAndWriteNode) all
went through ivar_lhs, which only handled the toplevel and
the instance-method cases -- module class methods got the
default self->iv_X expansion, but a module class method has
no self parameter, so the C compile failed with use of undeclared identifier 'self'.
Fix: hoist the module-class-method dispatch into a new
module_ivar_const_lhs helper and call it from ivar_lhs.
The InstanceVariableReadNode arm now also routes through
ivar_lhs instead of duplicating the logic inline. The
operator-write / or-write / and-write arms automatically pick
up the correct slot because they were already calling
ivar_lhs.
Test/module_ivar_op_write.rb covers @x += 1, @x = N,
and @x ||= v shapes plus a read-only @x access, all on a
module-level @ivar accessed from def self.X. Repro from #432
prints "2" / "3" correctly.
Bootstrap 4-way fixpoint OK; optcarrot checksum 59662 unchanged; 387 tests pass.
Closes #432 Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 8710b3e87fbd
- Parent
- eab5b038c08b
spinel_codegen.rbmodifiedView file
"" end # `iv_<name>` at toplevel (no self), `self->iv_<name>` inside a class. # Resolves an ivar lhs / rhs C expression. Three cases: # - inside a module class method (def self.X on a module), # `@x` lowers to the module's `cst_<Mod>_<x>` static -- # module class methods have no instance, so `self->iv_x` # would dangle. # - at script-toplevel script scope where the ivar was # registered as a toplevel slot, `iv_<name>` (no self). # - otherwise, `self->iv_<name>` (or `self.iv_<name>` for # value-typed self). def ivar_lhs(name) mod_slot = module_ivar_const_lhs(name) if mod_slot != "" return mod_slot end if @current_class_idx < 0 && toplevel_ivar_type(name) != "" return sanitize_ivar(name) end self_arrow + sanitize_ivar(name) end # When the current emit scope is a module class method # (`def self.X` on a module), the bare `@count` ivar maps to # the module's `cst_<Mod>_<name>` static slot rather than a # nonexistent `self->iv_count`. Returns the C expression or "" # when the current scope isn't a module class method or no # matching const is registered. def module_ivar_const_lhs(name) if @current_method_name == "" return "" end mi = 0 while mi < @module_names.length mmod = @module_names[mi] if mmod != "" && @current_method_name.start_with?(mmod + "_cls_") cname = mmod + "_" + name[1, name.length - 1] if find_const_idx(cname) >= 0 return "cst_" + cname end end mi = mi + 1 end "" end # Walk the program for ivar nodes at script scope. Class/Module bodies # are skipped — their ivars belong to the enclosing class. Top-level return fiber_var_ref(@nd_name[nid]) end if t == "InstanceVariableReadNode" # Check if we're in a module class method mi3 = 0 while mi3 < @module_names.length mmod = @module_names[mi3] if mmod != "" if @current_method_name.start_with?(mmod + "_cls_") iname = @nd_name[nid] cname3 = mmod + "_" + iname[1, iname.length - 1] ci3 = find_const_idx(cname3) if ci3 >= 0 return "cst_" + cname3 end end end mi3 = mi3 + 1 end return ivar_lhs(@nd_name[nid]) end if t == "ClassVariableReadNode"test/module_ivar_op_write.rbaddedView file
Loading diff…
test/module_ivar_op_write.rb.expectedaddedView file
Loading diff…