Commit 9b184d39
9b184d3945d3Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Capture case-branch locals in a lambda/proc body
A local assigned only inside a case/when arm of a lambda or proc
body was never declared in the emitted proc function, so the C build
failed with 'lv_<name>' undeclared. The capture scanner walked
if/while branches (via the @nd_body / @nd_subsequent / @nd_else_clause
links) but not case arms, which hang off @nd_conditions, so a
branch-only local was neither captured nor declared.
Recurse scan_lambda_free_vars into @nd_conditions so a when-branch
local is classified like any other body local: a closure over an outer
variable is captured (and mutations write through the shared cell),
matching the working if-branch path. Plain method and .each {}
bodies were already unaffected.
Fixes #1315
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 9b184d3945d3
- Parent
- e9d77ad83523
spinel_codegen.rbmodifiedView file
if @nd_collection[nid] >= 0 scan_lambda_free_vars(@nd_collection[nid], params, locals, free_vars) end # `case`/`when` arms hang off @nd_conditions (the WhenNode list, # whose own match values are again @nd_conditions). Without this # recursion a local assigned only inside a `when` branch is never # seen, so it's neither captured nor declared in the proc fn — # the lambda's C function then references an undeclared `lv_<name>`. # The `if`/`while` branches already flow through the @nd_body / # @nd_subsequent / @nd_else_clause links above. conds_lf = parse_id_list(@nd_conditions[nid]) k = 0 while k < conds_lf.length scan_lambda_free_vars(conds_lf[k], params, locals, free_vars) k = k + 1 end end def scan_lambda_ret_types(stmts)test/case_local_in_lambda.rbaddedView file
Loading diff…
test/case_local_in_lambda.rb.expectedaddedView file
Loading diff…