Commit 220ad5c6

220ad5c6a894Merged

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

Description

fix(codegen): heap-promoted name list reset at method boundary

Ori Pekelman's #564. Fiber.new { ... } bodies that capture a local from the enclosing method push a _hcell_<name>_<n> entry onto @heap_promoted_names so fiber_var_ref(<name>) emits (*_hcell_<name>_<n>) in the capturing function's C body. The list was never reset at method boundaries: when a sibling method later in the same compilation unit referenced a local of the same name (e.g. def do_work(client) called from inside the fiber body), its emit ALSO picked up the heap cell ref and the link failed because the cell was declared only in the spawner's C scope.

Save / restore @heap_promoted_names and @heap_promoted_cells length around the top-level method body emit (the @meth_body_ids walker at line ~9670). The inner compile_fiber_new already has its own save/restore for nested fiber emits; this outer wrapper ensures cells don't leak past a single method's generated C function.

Closes #564

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

Details

Commit
220ad5c6a894
Parent
2a78c2da2d2f

spinel_codegen.rbmodifiedView file

@@ -9661,6 +9661,13 @@
96619661 yp = yield_params_suffix(mi)
96629662 end
96639663
9664+ # Reset @heap_promoted_names at method boundary. The list
9665+ # tracks fiber-capture heap cells that live in the EMITTING
9666+ # function's local C scope; leaking it across methods makes
9667+ # sibling methods see `(*_hcell_<name>_<n>)` references to
9668+ # cells that exist only in the original method. Issue #564.
9669+ saved_hp_names_len_mb = @heap_promoted_names.length
9670+ saved_hp_cells_len_mb = @heap_promoted_cells.length
96649671 if oc_type != ""
96659672 # Open class method: self is primitive type
96669673 rt = @meth_return_types[mi]
@@ -9715,6 +9722,15 @@
97159722
97169723 rt = @meth_return_types[mi]
97179724 pop_scope
9725+ # Restore @heap_promoted_names to the pre-method baseline so
9726+ # the next method doesn't see this method's fiber-capture
9727+ # cells. Issue #564.
9728+ while @heap_promoted_names.length > saved_hp_names_len_mb
9729+ @heap_promoted_names.pop
9730+ end
9731+ while @heap_promoted_cells.length > saved_hp_cells_len_mb
9732+ @heap_promoted_cells.pop
9733+ end
97189734 @current_method_name = ""
97199735 @current_method_block_param = ""
97209736 @in_yield_method = 0