Commit 17300e8c
17300e8cefc7Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix(codegen): detect proc captures and fail loudly instead of silently
Commit 4593581 hoisted compile_proc_literal output to file scope so
Apple clang could parse it (no GCC nested function extension). For
non-capturing procs that's fine, but procs whose body references outer
locals broke silently: the body either compiled to return 0; (locals
absent from file scope) or hit a lv_x undeclared cc error far from
the source.
Phase 1 of the proc-closure recovery: scan the proc body via scan_lambda_free_vars, intersect with find_var_type to identify true outer-scope captures, and for those:
- Emit a comment naming the captured variables
- Emit a function body that prints a clear stderr message and abort()s
- Print the same message to stderr at codegen time
This trades silent miscompilation for a loud runtime error and a clear
codegen-time warning, so users hit the limitation immediately rather
than chasing a 0 return from a closure. Lambda (-> { ... } /
lambda { ... }) keeps its existing capture support unchanged.
Phase 2 will replace the abort with real capture-struct support modeled on the existing Fiber capture mechanism.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 17300e8cefc7
- Parent
- 9513c217109b
spinel_codegen.rbmodifiedView file
end pop_scope @out_lines = save_out # Detect free variables that reference outer-scope locals (captures). # Until proc closure support lands (Phase 2), surface this loudly: # silent file-scope hoist would either drop the body or fail to compile. free_vars = "".split(",") if bbody >= 0 proc_params = "".split(",") proc_params.push(bp) proc_locals = "".split(",") scan_lambda_free_vars(bbody, proc_params, proc_locals, free_vars) end captures = "".split(",") fk = 0 while fk < free_vars.length fv = free_vars[fk] if find_var_type(fv) != "" captures.push(fv) end fk = fk + 1 end if captures.length > 0 cap_list = captures.join(", ") STDERR.puts("spinel: proc body captures outer local(s) [" + cap_list + "] which is not yet implemented; use lambda { ... } / -> { ... } for closures (proc: " + fname + ")") @lambda_funcs << "/* proc captures outer locals (not yet supported): " @lambda_funcs << cap_list @lambda_funcs << " */\n" @lambda_funcs << "static mrb_int " @lambda_funcs << fname @lambda_funcs << "(mrb_int lv_" @lambda_funcs << bp @lambda_funcs << ") {\n" @lambda_funcs << " fprintf(stderr, \"spinel: proc with captured variables [" @lambda_funcs << cap_list @lambda_funcs << "] is not implemented; use lambda for closures" @lambda_funcs << bsl_n @lambda_funcs << "\");\n" @lambda_funcs << " abort();\n" @lambda_funcs << " return 0;\n" @lambda_funcs << "}\n" return "sp_proc_new(" + fname + ")" end # Hoist the proc body to file scope. Previously this was emitted as a # GCC nested-function inside a statement-expression, which Apple clang # does not implement. File-scope works for non-capturing procs (the # only kind compile_proc_literal currently produces). # does not implement. File-scope works for non-capturing procs. @lambda_funcs << "static mrb_int " @lambda_funcs << fname @lambda_funcs << "(mrb_int lv_"