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

@@ -18705,10 +18705,49 @@
1870518705 end
1870618706 pop_scope
1870718707 @out_lines = save_out
18708+ # Detect free variables that reference outer-scope locals (captures).
18709+ # Until proc closure support lands (Phase 2), surface this loudly:
18710+ # silent file-scope hoist would either drop the body or fail to compile.
18711+ free_vars = "".split(",")
18712+ if bbody >= 0
18713+ proc_params = "".split(",")
18714+ proc_params.push(bp)
18715+ proc_locals = "".split(",")
18716+ scan_lambda_free_vars(bbody, proc_params, proc_locals, free_vars)
18717+ end
18718+ captures = "".split(",")
18719+ fk = 0
18720+ while fk < free_vars.length
18721+ fv = free_vars[fk]
18722+ if find_var_type(fv) != ""
18723+ captures.push(fv)
18724+ end
18725+ fk = fk + 1
18726+ end
18727+ if captures.length > 0
18728+ cap_list = captures.join(", ")
18729+ STDERR.puts("spinel: proc body captures outer local(s) [" + cap_list + "] which is not yet implemented; use lambda { ... } / -> { ... } for closures (proc: " + fname + ")")
18730+ @lambda_funcs << "/* proc captures outer locals (not yet supported): "
18731+ @lambda_funcs << cap_list
18732+ @lambda_funcs << " */\n"
18733+ @lambda_funcs << "static mrb_int "
18734+ @lambda_funcs << fname
18735+ @lambda_funcs << "(mrb_int lv_"
18736+ @lambda_funcs << bp
18737+ @lambda_funcs << ") {\n"
18738+ @lambda_funcs << " fprintf(stderr, \"spinel: proc with captured variables ["
18739+ @lambda_funcs << cap_list
18740+ @lambda_funcs << "] is not implemented; use lambda for closures"
18741+ @lambda_funcs << bsl_n
18742+ @lambda_funcs << "\");\n"
18743+ @lambda_funcs << " abort();\n"
18744+ @lambda_funcs << " return 0;\n"
18745+ @lambda_funcs << "}\n"
18746+ return "sp_proc_new(" + fname + ")"
18747+ end
1870818748 # Hoist the proc body to file scope. Previously this was emitted as a
1870918749 # GCC nested-function inside a statement-expression, which Apple clang
18710- # does not implement. File-scope works for non-capturing procs (the
18711- # only kind compile_proc_literal currently produces).
18750+ # does not implement. File-scope works for non-capturing procs.
1871218751 @lambda_funcs << "static mrb_int "
1871318752 @lambda_funcs << fname
1871418753 @lambda_funcs << "(mrb_int lv_"