Commit e304b98e
e304b98e78d5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Declare rescue-bound locals inside Fiber bodies
rescue => e inside a Fiber.new body binds e as a local of the
generated _fiber_body_N function, but the binding was attributed to
the enclosing scope rather than the fiber body's own local set, so the
body referenced an undeclared lv_e and failed to compile.
Collect the names bound by rescue => e clauses lexically inside the
fiber body (without descending into nested closures, which declare
their own) and force them to true fiber-body locals typed exception,
dropping them from the capture set so a same-named outer variable is
not captured in their place. compile_rescue_chain already emits
lv_e = sp_exc_new(...); it just needed the local declared and typed.
The lambda/proc variant of the same construct compiles after this but still mis-types the local in infer_type (analyze attributes the binding to the enclosing scope, which infer_type reads from its cache); that deeper layer is tracked separately.
Refs #1363 Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- e304b98e78d5
- Parent
- 20239660ca1b
spinel_codegen.rbmodifiedView file
scan_fiber_free_vars(body, all_plist, all_names, free_vars, free_var_types) end # `rescue => e` inside the fiber body binds `e` as a fiber-body local, # not a capture of a same-named outer var. scan_fiber_free_vars would # otherwise capture it while compile_rescue_chain emits a bare `lv_e`, # leaving it undeclared in `_fiber_body_N`. Force such names to true # locals (typed `exception`) and drop them from the capture set. rb_names_f = "".split(",", -1) if body >= 0 collect_rescue_bound_names(body, rb_names_f) end rbi = 0 while rbi < rb_names_f.length rbn = rb_names_f[rbi] new_fv = "".split(",", -1) new_fvt = "".split(",", -1) fvj = 0 while fvj < free_vars.length if free_vars[fvj] != rbn new_fv.push(free_vars[fvj]) new_fvt.push(free_var_types[fvj]) end fvj = fvj + 1 end free_vars = new_fv free_var_types = new_fvt if not_in(rbn, local_names) == 1 && not_in(rbn, all_plist) == 1 local_names.push(rbn) local_types.push("exception") end rbi = rbi + 1 end # Capture `self` when the body references the enclosing instance # (explicit `self`, ivar reads/writes, or no-receiver method calls # that resolve to `self.<m>`). Without this, `_fiber_body_N`'s end end # Collect the names bound by `rescue => e` clauses lexically inside a # proc/lambda/fiber body, without descending into nested closure bodies # (those declare their own). compile_rescue_chain emits `lv_<name> = ...` # assuming the binding is a declared local of the function it lowers # into; but a rescue-bound var is attributed to the *enclosing* scope, # so a closure body never declares it and the body function references # an undeclared `lv_<name>`. Callers declare these as `exception` locals. def collect_rescue_bound_names(nid, acc) if nid < 0 return end t = @nd_type[nid] if t == "LambdaNode" return end if t == "CallNode" && @nd_name[nid] == "new" rv = @nd_receiver[nid] if rv >= 0 && constructor_class_name(rv) == "Fiber" return end end rc = @nd_rescue_clause[nid] while rc >= 0 ref = @nd_reference[rc] if ref >= 0 en = @nd_name[ref] if not_in(en, acc) == 1 acc.push(en) end end rc = @nd_subsequent[rc] end cs_rb = [] push_child_ids(nid, cs_rb) k = 0 while k < cs_rb.length collect_rescue_bound_names(cs_rb[k], acc) k = k + 1 end end def scan_lambda_ret_types(stmts) stmts.each { |sid| scan_lambda_ret_types_node(sid)test/rescue_bind_in_fiber_body.rbaddedView file
Loading diff…
test/rescue_bind_in_fiber_body.rb.expectedaddedView file
Loading diff…