Commit 991d5fe0

991d5fe0dc3eMerged

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

Description

fix(codegen): scan const-init expressions for block-param locals

emit_main scans top-level statements for locals so they get declared at main()'s frame before any code runs. Constant initializer expressions are then compiled into main() right before the user statements, but they were not part of the local scan. Block params introduced inside a const RHS — e.g. `FRAME_CLOCKS = [29830, ...].map { |n| RP2A03_CC * n }`'s `n` — never got a corresponding `mrb_int lv_n;` declaration, and the emitted assignment `lv_n = sp_IntArray_get(...);` fell through with "undeclared identifier".

Walk @const_expr_ids alongside the existing top-level statement scan so any local introduced inside a const's RHS gets the same treatment.

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

Details

Commit
991d5fe0dc3e
Parent
bee7e89d5b64

spinel_codegen.rbmodifiedView file

@@ -13446,6 +13446,18 @@
1344613446 end
1344713447 end
1344813448 }
13449+ # Constant initializer expressions get compiled into main() right
13450+ # before the user statements run, but the block params they
13451+ # introduce (e.g. \`FRAME = [...].map { |n| ... }\`) need
13452+ # declarations in the same scope. Scan each const's RHS so the
13453+ # block param names land in lnames alongside the user-stmt locals.
13454+ ci_lc = 0
13455+ while ci_lc < @const_expr_ids.length
13456+ if @const_expr_ids[ci_lc] >= 0
13457+ scan_locals(@const_expr_ids[ci_lc], lnames, ltypes, empty_params)
13458+ end
13459+ ci_lc = ci_lc + 1
13460+ end
1344913461
1345013462 # Declare vars for second pass to resolve dependent types
1345113463 j = 0