Commit 1eb79fd8
1eb79fd83e74Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
codegen: declare a transplant clone's body locals from its source scope
include M into a user class clones M's instance methods into a scope
owned by the class (process_include_body). For a user-class target that
clone shares the source method's body AST and registers only the
params; the body locals stay on the source scope, which codegen then
skips (is_transplanted_source). Since codegen emits the clone, its
prologue declared only the params, so every body local
(matched/controller/merged/block params ...) was a bare assignment
to an undeclared C identifier -- un-compilable output (a concrete case of
the ill-typed-emit class in #1338).
Before emitting a method, copy onto the emitted scope any local the matching transplant-source scope registered that the clone lacks, reusing the source's already-inferred types (so no re-inference or type guessing). Gated on a shared body (src->body == s->body) so the builtin-target clone -- which re-walks its own body copy and registers its own locals -- is left untouched, and so only locals actually referenced in the shared body are added (no spurious unused-variable declarations).
Fixes #1435
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 1eb79fd83e74
- Parent
- cc869197cab3
src/codegen.cmodifiedView file
(void)cn;}/* An `include M` into a user class clones M's instance methods into a scope owned by the class, but that clone *shares* the source's body AST and only registers the params -- the body locals stay on the source scope, which codegen skips (is_transplanted_source). Since codegen emits the clone, its declarations would be missing and every `lv_<name>` body reference would be undeclared (#1435). Copy the source's body locals onto the clone, reusing their already-inferred types. Gated on a shared body so the builtin-target clone (which re-walks its own body copy and registers its own locals) is left untouched. */static void inherit_transplant_locals(Compiler *c, Scope *s) { if (s->def_node < 0 || s->is_transplanted_source) return; for (int i = 0; i < c->nscopes; i++) { Scope *src = &c->scopes[i]; if (src == s || !src->is_transplanted_source || src->def_node != s->def_node || src->body != s->body) continue; for (int k = 0; k < src->nlocals; k++) { LocalVar sl = src->locals[k]; /* copy: intern may realloc src->locals */ if (!sl.name || scope_local(s, sl.name)) continue; LocalVar *dl = scope_local_intern(s, sl.name); dl->type = sl.type; dl->is_param = sl.is_param; dl->is_block_param = sl.is_block_param; dl->proc_ret = sl.proc_ret; dl->is_cell = sl.is_cell; } break; }}void emit_method(Compiler *c, Scope *s, Buf *b) { if (s->cs_synth) { emit_compiler_state_method(c, s, b); return; } /* instance_eval/exec trampolines are inlined at every call site; the } return; } inherit_transplant_locals(c, s); emit_method_signature(c, s, b); buf_puts(b, " {\n"); buf_puts(b, " SP_GC_SAVE();\n");test/include_method_body_locals.rbaddedView file
Loading diff…
test/include_method_body_locals.rb.expectedaddedView file
Loading diff…