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

@@ -3739,14 +3739,50 @@
37393739 ""
37403740 end
37413741
3742- # `iv_<name>` at toplevel (no self), `self->iv_<name>` inside a class.
3742+ # Resolves an ivar lhs / rhs C expression. Three cases:
3743+ # - inside a module class method (def self.X on a module),
3744+ # `@x` lowers to the module's `cst_<Mod>_<x>` static --
3745+ # module class methods have no instance, so `self->iv_x`
3746+ # would dangle.
3747+ # - at script-toplevel script scope where the ivar was
3748+ # registered as a toplevel slot, `iv_<name>` (no self).
3749+ # - otherwise, `self->iv_<name>` (or `self.iv_<name>` for
3750+ # value-typed self).
37433751 def ivar_lhs(name)
3752+ mod_slot = module_ivar_const_lhs(name)
3753+ if mod_slot != ""
3754+ return mod_slot
3755+ end
37443756 if @current_class_idx < 0 && toplevel_ivar_type(name) != ""
37453757 return sanitize_ivar(name)
37463758 end
37473759 self_arrow + sanitize_ivar(name)
37483760 end
37493761
3762+ # When the current emit scope is a module class method
3763+ # (`def self.X` on a module), the bare `@count` ivar maps to
3764+ # the module's `cst_<Mod>_<name>` static slot rather than a
3765+ # nonexistent `self->iv_count`. Returns the C expression or ""
3766+ # when the current scope isn't a module class method or no
3767+ # matching const is registered.
3768+ def module_ivar_const_lhs(name)
3769+ if @current_method_name == ""
3770+ return ""
3771+ end
3772+ mi = 0
3773+ while mi < @module_names.length
3774+ mmod = @module_names[mi]
3775+ if mmod != "" && @current_method_name.start_with?(mmod + "_cls_")
3776+ cname = mmod + "_" + name[1, name.length - 1]
3777+ if find_const_idx(cname) >= 0
3778+ return "cst_" + cname
3779+ end
3780+ end
3781+ mi = mi + 1
3782+ end
3783+ ""
3784+ end
3785+
37503786
37513787 # Walk the program for ivar nodes at script scope. Class/Module bodies
37523788 # are skipped — their ivars belong to the enclosing class. Top-level
@@ -9920,22 +9956,6 @@
99209956 return fiber_var_ref(@nd_name[nid])
99219957 end
99229958 if t == "InstanceVariableReadNode"
9923- # Check if we're in a module class method
9924- mi3 = 0
9925- while mi3 < @module_names.length
9926- mmod = @module_names[mi3]
9927- if mmod != ""
9928- if @current_method_name.start_with?(mmod + "_cls_")
9929- iname = @nd_name[nid]
9930- cname3 = mmod + "_" + iname[1, iname.length - 1]
9931- ci3 = find_const_idx(cname3)
9932- if ci3 >= 0
9933- return "cst_" + cname3
9934- end
9935- end
9936- end
9937- mi3 = mi3 + 1
9938- end
99399959 return ivar_lhs(@nd_name[nid])
99409960 end
99419961 if t == "ClassVariableReadNode"