Commit c7c2884b

c7c2884b7f53Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Capture self into procs forwarded into an explicit &block param

A literal block forwarded into a method's explicit &block parameter is lowered to a standalone proc function (the d419896 path that replaced yield-inlining for &block + block_given? methods). compile_proc_literal captured only free locals, never self, so a block body that calls a method on the implicit receiver (e.g. section do label("h2") end) emitted a bare self into a proc fn with no self parameter -- "use of undeclared identifier 'self'", an uncompilable unit. One of the two failures on roundhouse's toolchain-spinel CI (self-using assert_select blocks).

Mirror the fiber-body self capture in compile_proc_literal: when the block body references self (fiber_body_uses_self) inside a non-value-type instance method, add a "self" capture of type obj_, set @self_override to (*_cap->self) while compiling the body, and populate a dedicated self cell at the proc-creation site (bypassing the persistent heap-promotion map, whose cells live in another C scope). The call-site cap population is refactored to a local_cells map so the self cell is threaded without polluting @heap_promoted_names. Gate green (test/bench/optcarrot 59662/self-host fixpoint).

The reported compile failure -- and the real CI breakage, which uses self-using side-effect blocks -- is resolved. A separate pre-existing bug remains: a value-returning yield-method drops the block's return type (prints 0 instead of "h2"), reproducible even with a bare def m = yield and unrelated to the &block path; left for a follow-up.

Refs #1343.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
c7c2884b7f53
Parent
4d4bf30fe841

spinel_codegen.rbmodifiedView file

@@ -42894,6 +42894,22 @@
4289442894 end
4289542895 fk = fk + 1
4289642896 end
42897+ # Capture `self` when the block body references the enclosing instance
42898+ # (explicit `self`, ivar access, or a no-receiver call resolving to
42899+ # `self.<m>`). A literal block forwarded into an explicit `&block`
42900+ # param is lowered here to a standalone proc fn that takes no `self`,
42901+ # so without this its body would emit a bare `self` that doesn't
42902+ # compile. Mirrors the fiber-body self capture; same value-type
42903+ # exclusion (those pass `self` by value, needing a different shape).
42904+ # Issue #1343.
42905+ self_captured_proc = 0
42906+ if @current_class_idx >= 0 && @cls_is_value_type[@current_class_idx] != 1
42907+ if bbody >= 0 && fiber_body_uses_self(bbody) == 1
42908+ captures.push("self")
42909+ capture_types.push("obj_" + @cls_names[@current_class_idx])
42910+ self_captured_proc = 1
42911+ end
42912+ end
4289742913 has_captures = 0
4289842914 if captures.length > 0
4289942915 has_captures = 1
@@ -42911,6 +42927,7 @@
4291142927 saved_proc_capture_types = @proc_capture_types
4291242928 saved_heap_hp_names_len = @heap_promoted_names.length
4291342929 saved_heap_hp_cells_len = @heap_promoted_cells.length
42930+ saved_self_override_proc = @self_override
4291442931 if has_captures == 1
4291542932 @in_proc_body = 1
4291642933 @proc_captures = captures
@@ -42920,6 +42937,9 @@
4292042937 @proc_captures = "".split(",", -1)
4292142938 @proc_capture_types = "".split(",", -1)
4292242939 end
42940+ if self_captured_proc == 1
42941+ @self_override = "(*_cap->self)"
42942+ end
4292342943 push_scope
4292442944 pts = "".split("|", -1)
4292542945 if blk_param_types != ""
@@ -43018,6 +43038,7 @@
4301843038 @in_proc_body = saved_in_proc_body
4301943039 @proc_captures = saved_proc_captures
4302043040 @proc_capture_types = saved_proc_capture_types
43041+ @self_override = saved_self_override_proc
4302143042 while @heap_promoted_names.length > saved_heap_hp_names_len
4302243043 @heap_promoted_names.pop
4302343044 end
@@ -43079,45 +43100,49 @@
4307943100 # a cell, copy current value into it, register so subsequent
4308043101 # references in the enclosing scope go through the cell), then
4308143102 # allocate the cap struct and populate its pointers.
43103+ local_cells = "".split(",", -1)
4308243104 k = 0
4308343105 while k < captures.length
4308443106 vn = captures[k]
4308543107 ct = c_type(capture_types[k])
43086- already_promoted = 0
43087- ci = 0
43088- while ci < @heap_promoted_names.length
43089- if @heap_promoted_names[ci] == vn
43090- already_promoted = 1
43108+ cell = ""
43109+ if vn == "self"
43110+ # `self` resolves to the enclosing C function's parameter, distinct
43111+ # per method; allocate a dedicated cell and copy it in, bypassing the
43112+ # persistent heap-promotion map whose cells live in another scope.
43113+ cell = "_hcell_self_p" + pid.to_s
43114+ emit(" " + ct + " *" + cell + " = (" + ct + " *)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);")
43115+ emit(" *" + cell + " = " + self_expr + ";")
43116+ else
43117+ already_promoted = 0
43118+ ci = 0
43119+ while ci < @heap_promoted_names.length
43120+ if @heap_promoted_names[ci] == vn
43121+ already_promoted = 1
43122+ cell = @heap_promoted_cells[ci]
43123+ end
43124+ ci = ci + 1
4309143125 end
43092- ci = ci + 1
43093- end
43094- if already_promoted == 0
43095- if @in_proc_body == 1 && proc_capture_index(vn) >= 0
43096- cell = "_cap->" + vn
43097- else
43098- cell = "_hcell_" + vn + "_p" + pid.to_s
43099- emit(" " + ct + " *" + cell + " = (" + ct + " *)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);")
43100- emit(" *" + cell + " = " + fiber_var_ref(vn) + ";")
43126+ if already_promoted == 0
43127+ if @in_proc_body == 1 && proc_capture_index(vn) >= 0
43128+ cell = "_cap->" + vn
43129+ else
43130+ cell = "_hcell_" + vn + "_p" + pid.to_s
43131+ emit(" " + ct + " *" + cell + " = (" + ct + " *)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);")
43132+ emit(" *" + cell + " = " + fiber_var_ref(vn) + ";")
43133+ end
43134+ @heap_promoted_names.push(vn)
43135+ @heap_promoted_cells.push(cell)
4310143136 end
43102- @heap_promoted_names.push(vn)
43103- @heap_promoted_cells.push(cell)
4310443137 end
43138+ local_cells.push(cell)
4310543139 k = k + 1
4310643140 end
4310743141 cap_ptr = "_cap_ptr_p" + pid.to_s
4310843142 emit(" " + cap_name + " *" + cap_ptr + " = (" + cap_name + " *)sp_gc_alloc(sizeof(" + cap_name + "), NULL, " + cap_scan_name + ");")
4310943143 k = 0
4311043144 while k < captures.length
43111- vn = captures[k]
43112- ci = 0
43113- cell = ""
43114- while ci < @heap_promoted_names.length
43115- if @heap_promoted_names[ci] == vn
43116- cell = @heap_promoted_cells[ci]
43117- end
43118- ci = ci + 1
43119- end
43120- emit(" " + cap_ptr + "->" + vn + " = " + cell + ";")
43145+ emit(" " + cap_ptr + "->" + captures[k] + " = " + local_cells[k] + ";")
4312143146 k = k + 1
4312243147 end
4312343148 return "sp_proc_new_meta(" + fname + ", " + cap_ptr + ", " + cap_scan_name + ", " + meta_args + ")"