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

@@ -40966,6 +40966,19 @@
4096640966 if @nd_collection[nid] >= 0
4096740967 scan_lambda_free_vars(@nd_collection[nid], params, locals, free_vars)
4096840968 end
40969+ # `case`/`when` arms hang off @nd_conditions (the WhenNode list,
40970+ # whose own match values are again @nd_conditions). Without this
40971+ # recursion a local assigned only inside a `when` branch is never
40972+ # seen, so it's neither captured nor declared in the proc fn —
40973+ # the lambda's C function then references an undeclared `lv_<name>`.
40974+ # The `if`/`while` branches already flow through the @nd_body /
40975+ # @nd_subsequent / @nd_else_clause links above.
40976+ conds_lf = parse_id_list(@nd_conditions[nid])
40977+ k = 0
40978+ while k < conds_lf.length
40979+ scan_lambda_free_vars(conds_lf[k], params, locals, free_vars)
40980+ k = k + 1
40981+ end
4096940982 end
4097040983
4097140984 def scan_lambda_ret_types(stmts)