Commit a0152639

a015263931daMerged

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

Description

fix(codegen): reset in_gc_scope at void initialize wrapper entry

The synthesized sp_<C>_initialize super-chain wrapper (emitted alongside sp_<C>_new for every heap class) inherits @in_gc_scope from whatever the prior emit left it at — typically the heap-path constructor's tail value of 1. When the void wrapper's body has both:

  • a String pointer local (forcing declare_method_locals' has_gc_locals branch to want to emit SP_GC_SAVE()), and
  • an early bare return (which compile_return_stmt lowers to SP_GC_RESTORE() because @in_gc_scope == 1 is inherited),

declare_method_locals' guard @needs_gc == 1 && @in_gc_scope == 0 is false on the inherited 1 and skips the SP_GC_SAVE emit. The emitted body then references _gc_saved (declared by SAVE) from RESTORE — gcc errors with _gc_saved undeclared.

Fix: reset @in_gc_scope = 0 at the start of the void wrapper body so declare_method_locals decides cleanly, and restore the saved value when leaving.

Surfaced via Roundhouse's ActiveSupport::HashWithIndifferentAccess#initialize shape: poly-typed other = nil param + return if other.nil? early exit + a String pointer local that needs GC rooting. matz/spinel#314 follow-up.

Refs #314

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

Details

Commit
a015263931da
Parent
5fef0b63ea90

spinel_codegen.rbmodifiedView file

@@ -17778,6 +17778,17 @@
1777817778 @current_method_has_self = 1
1777917779 @current_method_return = "void"
1778017780 @current_class_idx = ci
17781+ # Issue #314 follow-up: if the body contains an early `return`,
17782+ # compile_return_stmt emits `SP_GC_RESTORE()` (which references
17783+ # `_gc_saved`). The matching `SP_GC_SAVE()` at the top of the
17784+ # function must therefore exist. emit_constructor restored
17785+ # `@in_gc_scope` to its pre-constructor value just above; if
17786+ # that was 1 from a stale prior emit, declare_method_locals'
17787+ # `@in_gc_scope == 0` guard skips the SAVE emit and `_gc_saved`
17788+ # ends up undeclared. Reset to 0 here so declare_method_locals
17789+ # can decide cleanly.
17790+ saved_in_gc_scope2 = @in_gc_scope
17791+ @in_gc_scope = 0
1778117792 all_params = @cls_meth_params[ci].split("|")
1778217793 all_ptypes = @cls_meth_ptypes[ci].split("|")
1778317794 pnames = "".split(",")
@@ -17816,6 +17827,7 @@
1781617827 @current_class_idx = -1
1781717828 @current_method_return = saved_method_return
1781817829 @current_method_has_self = saved_has_self2
17830+ @in_gc_scope = saved_in_gc_scope2
1781917831 end
1782017832 emit_raw("}")
1782117833 emit_raw("")