Commit 92a2b892

92a2b8922e7eMerged

Yusuke Endoh <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified

Description

fix(codegen): capture self and ivars in Fiber.new { ... } body

Three independent gaps in the fiber pipeline kept any non-trivial Fiber.new { ... } body from compiling:

  1. infer_ivar_init_type returned obj_Fiber for Fiber.new {...} but infer_call_type returned fiber. The slot was registered as obj_Fiber, the writer-scan disagreed with fiber, and update_ivar_type widened to poly. @fiber.resume then fell through the unresolved-int dispatch. Add Fiber → "fiber" next to the existing Array / Hash / StringIO arms.

  2. The Fiber.new block scan only captured local variables, not the enclosing instance's self. Bodies like Fiber.new { main_loop } referenced an undeclared self in the emitted C function. Add fiber_body_uses_self(nid) (counts SelfNode, ivar reads/writes, and no-receiver method calls inside class context) and synthesise a self capture entry of type obj_<CurrentClass>. Source the cell's initial value from the C self parameter directly, and track per-fiber cells in a local map (the persistent @heap_promoted_names reuse table can't be used because each enclosing method's self parameter is a different C variable). Inside the body, @self_override = "(*_cap->self)" redirects SelfNode, ivar_lhs, and no-receiver method-call emit so the body's sp_X_main_loop(self) becomes sp_X_main_loop(*_cap->self). SelfNode → "self" was hardcoded; route through self_expr.

  3. The fiber body's last-stmt branch only handled LocalVariableWriteNode / LocalVariableOperatorWriteNode. Add the ivar write/op-write/and/or-write forms so an ivar mutation as the block's tail value (e.g. @n += 1000 at the end of a counter fiber) actually fires compile_stmt for its side effect instead of being silently dropped via compile_expr.

Tests: fiber_yield_across_method_call, fiber_ivar_persists_across_yield.

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

Details

Commit
92a2b8922e7e
Parent
4a8ca9dea493

spinel_codegen.rbmodifiedView file

@@ -8343,6 +8343,9 @@
83438343 if rname == "StringIO"
83448344 return "stringio"
83458345 end
8346+ if rname == "Fiber"
8347+ return "fiber"
8348+ end
83468349 return "obj_" + rname
83478350 end
83488351 end
@@ -20743,7 +20746,7 @@
2074320746 return "0"
2074420747 end
2074520748 if t == "SelfNode"
20746- return "self"
20749+ return self_expr
2074720750 end
2074820751 if t == "LocalVariableReadNode"
2074920752 return fiber_var_ref(@nd_name[nid])
@@ -21546,6 +21549,135 @@
2154621549 end
2154721550
2154821551
21552+ # Detect whether a Fiber.new block body references `self` — directly
21553+ # (`SelfNode`), via `@ivar` reads/writes, or via a no-receiver method
21554+ # call inside a class body. Used by `compile_fiber_new` to decide
21555+ # whether to thread an explicit `self` capture through the fiber's
21556+ # `_cap` struct so the body's emitted C can resolve `self->iv_X` and
21557+ # `sp_<Class>_<method>(self)` call sites without the surrounding
21558+ # method's `self` parameter.
21559+ def fiber_body_uses_self(nid)
21560+ if nid < 0
21561+ return 0
21562+ end
21563+ t = @nd_type[nid]
21564+ if t == "SelfNode"
21565+ return 1
21566+ end
21567+ if t == "InstanceVariableReadNode" || t == "InstanceVariableWriteNode"
21568+ return 1
21569+ end
21570+ if t == "InstanceVariableOperatorWriteNode" || t == "InstanceVariableTargetNode"
21571+ return 1
21572+ end
21573+ if t == "InstanceVariableAndWriteNode" || t == "InstanceVariableOrWriteNode"
21574+ return 1
21575+ end
21576+ # Stop at nested lambda/fiber boundaries (their own body captures self separately)
21577+ if t == "LambdaNode"
21578+ return 0
21579+ end
21580+ if t == "CallNode"
21581+ mn = @nd_name[nid]
21582+ if mn == "new"
21583+ rv = @nd_receiver[nid]
21584+ if rv >= 0 && constructor_class_name(rv) == "Fiber"
21585+ return 0
21586+ end
21587+ end
21588+ # Bare (no receiver) call inside class context implicitly uses self
21589+ if @nd_receiver[nid] < 0 && @current_class_idx >= 0
21590+ return 1
21591+ end
21592+ end
21593+ if @nd_body[nid] >= 0
21594+ if fiber_body_uses_self(@nd_body[nid]) == 1
21595+ return 1
21596+ end
21597+ end
21598+ if @nd_receiver[nid] >= 0
21599+ if fiber_body_uses_self(@nd_receiver[nid]) == 1
21600+ return 1
21601+ end
21602+ end
21603+ if @nd_expression[nid] >= 0
21604+ if fiber_body_uses_self(@nd_expression[nid]) == 1
21605+ return 1
21606+ end
21607+ end
21608+ if @nd_predicate[nid] >= 0
21609+ if fiber_body_uses_self(@nd_predicate[nid]) == 1
21610+ return 1
21611+ end
21612+ end
21613+ if @nd_subsequent[nid] >= 0
21614+ if fiber_body_uses_self(@nd_subsequent[nid]) == 1
21615+ return 1
21616+ end
21617+ end
21618+ if @nd_else_clause[nid] >= 0
21619+ if fiber_body_uses_self(@nd_else_clause[nid]) == 1
21620+ return 1
21621+ end
21622+ end
21623+ if @nd_arguments[nid] >= 0
21624+ if fiber_body_uses_self(@nd_arguments[nid]) == 1
21625+ return 1
21626+ end
21627+ end
21628+ if @nd_rescue_clause[nid] >= 0
21629+ if fiber_body_uses_self(@nd_rescue_clause[nid]) == 1
21630+ return 1
21631+ end
21632+ end
21633+ if @nd_ensure_clause[nid] >= 0
21634+ if fiber_body_uses_self(@nd_ensure_clause[nid]) == 1
21635+ return 1
21636+ end
21637+ end
21638+ stmts_list = parse_id_list(@nd_stmts[nid])
21639+ k = 0
21640+ while k < stmts_list.length
21641+ if fiber_body_uses_self(stmts_list[k]) == 1
21642+ return 1
21643+ end
21644+ k = k + 1
21645+ end
21646+ args_list = parse_id_list(@nd_args[nid])
21647+ k = 0
21648+ while k < args_list.length
21649+ if fiber_body_uses_self(args_list[k]) == 1
21650+ return 1
21651+ end
21652+ k = k + 1
21653+ end
21654+ conds_list = parse_id_list(@nd_conditions[nid])
21655+ k = 0
21656+ while k < conds_list.length
21657+ if fiber_body_uses_self(conds_list[k]) == 1
21658+ return 1
21659+ end
21660+ k = k + 1
21661+ end
21662+ elems_list = parse_id_list(@nd_elements[nid])
21663+ k = 0
21664+ while k < elems_list.length
21665+ if fiber_body_uses_self(elems_list[k]) == 1
21666+ return 1
21667+ end
21668+ k = k + 1
21669+ end
21670+ blk = @nd_block[nid]
21671+ if blk >= 0
21672+ if @nd_body[blk] >= 0
21673+ if fiber_body_uses_self(@nd_body[blk]) == 1
21674+ return 1
21675+ end
21676+ end
21677+ end
21678+ 0
21679+ end
21680+
2154921681 def scan_fiber_free_vars(nid, params, locals, free_vars, free_var_types)
2155021682 if nid < 0
2155121683 return
@@ -21730,6 +21862,23 @@
2173021862 scan_fiber_free_vars(body, all_plist, all_names, free_vars, free_var_types)
2173121863 end
2173221864
21865+ # Capture `self` when the body references the enclosing instance
21866+ # (explicit `self`, ivar reads/writes, or no-receiver method calls
21867+ # that resolve to `self.<m>`). Without this, `_fiber_body_N`'s
21868+ # emitted body references a `self` that isn't a parameter and won't
21869+ # compile. Only add the capture inside an instance-method context;
21870+ # value-type classes pass `self` by value, and that path needs a
21871+ # different capture shape we don't generate yet.
21872+ self_captured = 0
21873+ if @current_class_idx >= 0 && @cls_is_value_type[@current_class_idx] != 1
21874+ if body >= 0 && fiber_body_uses_self(body) == 1
21875+ self_type = "obj_" + @cls_names[@current_class_idx]
21876+ free_vars.push("self")
21877+ free_var_types.push(self_type)
21878+ self_captured = 1
21879+ end
21880+ end
21881+
2173321882 fid = @fiber_counter
2173421883 @fiber_counter = @fiber_counter + 1
2173521884 fname = "_fiber_body_" + fid.to_s
@@ -21764,11 +21913,15 @@
2176421913 saved_fiber_capture_types = @fiber_capture_types
2176521914 saved_hp_names_len = @heap_promoted_names.length
2176621915 saved_hp_cells_len = @heap_promoted_cells.length
21916+ saved_self_override = @self_override
2176721917 @out_lines = "".split(",")
2176821918 @indent = 1
2176921919 @in_fiber_body = 1
2177021920 @fiber_captures = free_vars
2177121921 @fiber_capture_types = free_var_types
21922+ if self_captured == 1
21923+ @self_override = "(*_cap->self)"
21924+ end
2177221925
2177321926 push_scope
2177421927 if bp != ""
@@ -21798,11 +21951,16 @@
2179821951 compile_stmt(last)
2179921952 emit(" _fb->yielded_value = sp_box_nil();")
2180021953 else
21801- if @nd_type[last] == "LocalVariableWriteNode" || @nd_type[last] == "LocalVariableOperatorWriteNode"
21954+ last_t_check = @nd_type[last]
21955+ if last_t_check == "LocalVariableWriteNode" || last_t_check == "LocalVariableOperatorWriteNode" ||
21956+ last_t_check == "InstanceVariableWriteNode" || last_t_check == "InstanceVariableOperatorWriteNode" ||
21957+ last_t_check == "InstanceVariableOrWriteNode" || last_t_check == "InstanceVariableAndWriteNode"
2180221958 compile_stmt(last)
21959+ emit(" _fb->yielded_value = sp_box_nil();")
21960+ else
21961+ last_val = compile_expr(last)
21962+ emit(" _fb->yielded_value = " + box_val_to_poly(last_val, last_type) + ";")
2180321963 end
21804- last_val = compile_expr(last)
21805- emit(" _fb->yielded_value = " + box_val_to_poly(last_val, last_type) + ";")
2180621964 end
2180721965 end
2180821966 end
@@ -21818,6 +21976,7 @@
2181821976 @in_fiber_body = saved_in_fiber_body
2181921977 @fiber_captures = saved_fiber_captures
2182021978 @fiber_capture_types = saved_fiber_capture_types
21979+ @self_override = saved_self_override
2182121980 # Restore heap promoted lists to saved length
2182221981 while @heap_promoted_names.length > saved_hp_names_len
2182321982 @heap_promoted_names.pop
@@ -21831,35 +21990,54 @@
2183121990 return "sp_Fiber_new(" + fname + ")"
2183221991 end
2183321992
21834- # Heap-promote captured variables (allocate cells if not already promoted)
21993+ # Heap-promote captured variables (allocate cells if not already promoted).
21994+ # Track this fiber's cell-per-capture in a local map so a cross-method
21995+ # `self` capture (different C function each time) doesn't accidentally
21996+ # reuse a stale `_hcell_self_<n>` from a sibling fiber site that lives
21997+ # in a different C scope.
21998+ local_cells = "".split(",")
2183521999 k = 0
2183622000 while k < free_vars.length
2183722001 vn = free_vars[k]
21838- already_promoted = 0
21839- # Check outer scope heap promotions
21840- ci = 0
21841- while ci < @heap_promoted_names.length
21842- if @heap_promoted_names[ci] == vn
21843- already_promoted = 1
21844- end
21845- ci = ci + 1
21846- end
21847- # Check if variable is already a heap pointer from enclosing fiber capture
21848- if already_promoted == 0 && @in_fiber_body == 1 && fiber_capture_index(vn) >= 0
21849- # Reuse the enclosing fiber's capture pointer (_cap->vn is already a heap cell)
21850- cell = "_cap->" + vn
21851- @heap_promoted_names.push(vn)
21852- @heap_promoted_cells.push(cell)
21853- already_promoted = 1
21854- end
21855- if already_promoted == 0
21856- cell = "_hcell_" + vn + "_" + fid.to_s
22002+ cell = ""
22003+ if vn == "self"
22004+ # `self` resolves to the enclosing C function's parameter, which
22005+ # differs per method (e.g. `sp_X_new`'s `self` and `sp_X_initialize`'s
22006+ # `self` are distinct C variables). Skip the persistent
22007+ # @heap_promoted_names lookup — its cells live in another scope.
22008+ cell = "_hcell_self_" + fid.to_s
2185722009 ct = c_type(free_var_types[k])
2185822010 emit(" " + ct + " *" + cell + " = (" + ct + "*)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);")
21859- emit(" *" + cell + " = " + fiber_var_ref(vn) + ";")
21860- @heap_promoted_names.push(vn)
21861- @heap_promoted_cells.push(cell)
22011+ emit(" *" + cell + " = self;")
22012+ else
22013+ already_promoted = 0
22014+ # Check outer scope heap promotions
22015+ ci = 0
22016+ while ci < @heap_promoted_names.length
22017+ if @heap_promoted_names[ci] == vn
22018+ already_promoted = 1
22019+ cell = @heap_promoted_cells[ci]
22020+ end
22021+ ci = ci + 1
22022+ end
22023+ # Check if variable is already a heap pointer from enclosing fiber capture
22024+ if already_promoted == 0 && @in_fiber_body == 1 && fiber_capture_index(vn) >= 0
22025+ # Reuse the enclosing fiber's capture pointer (_cap->vn is already a heap cell)
22026+ cell = "_cap->" + vn
22027+ @heap_promoted_names.push(vn)
22028+ @heap_promoted_cells.push(cell)
22029+ already_promoted = 1
22030+ end
22031+ if already_promoted == 0
22032+ cell = "_hcell_" + vn + "_" + fid.to_s
22033+ ct = c_type(free_var_types[k])
22034+ emit(" " + ct + " *" + cell + " = (" + ct + "*)sp_gc_alloc(sizeof(" + ct + "), NULL, NULL);")
22035+ emit(" *" + cell + " = " + fiber_var_ref(vn) + ";")
22036+ @heap_promoted_names.push(vn)
22037+ @heap_promoted_cells.push(cell)
22038+ end
2186222039 end
22040+ local_cells.push(cell)
2186322041 k = k + 1
2186422042 end
2186522043
@@ -21870,15 +22048,7 @@
2187022048 k = 0
2187122049 while k < free_vars.length
2187222050 vn = free_vars[k]
21873- # Find the cell for this variable
21874- ci = 0
21875- cell = ""
21876- while ci < @heap_promoted_names.length
21877- if @heap_promoted_names[ci] == vn
21878- cell = @heap_promoted_cells[ci]
21879- end
21880- ci = ci + 1
21881- end
22051+ cell = local_cells[k]
2188222052 emit(" " + cap_ptr + "->" + vn + " = " + cell + ";")
2188322053 k = k + 1
2188422054 end