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

@@ -359,6 +359,35 @@
359359 (void)cn;
360360 }
361361
362+/* An `include M` into a user class clones M's instance methods into a scope
363+ owned by the class, but that clone *shares* the source's body AST and only
364+ registers the params -- the body locals stay on the source scope, which
365+ codegen skips (is_transplanted_source). Since codegen emits the clone, its
366+ declarations would be missing and every `lv_<name>` body reference would be
367+ undeclared (#1435). Copy the source's body locals onto the clone, reusing
368+ their already-inferred types. Gated on a shared body so the builtin-target
369+ clone (which re-walks its own body copy and registers its own locals) is
370+ left untouched. */
371+static void inherit_transplant_locals(Compiler *c, Scope *s) {
372+ if (s->def_node < 0 || s->is_transplanted_source) return;
373+ for (int i = 0; i < c->nscopes; i++) {
374+ Scope *src = &c->scopes[i];
375+ if (src == s || !src->is_transplanted_source ||
376+ src->def_node != s->def_node || src->body != s->body) continue;
377+ for (int k = 0; k < src->nlocals; k++) {
378+ LocalVar sl = src->locals[k]; /* copy: intern may realloc src->locals */
379+ if (!sl.name || scope_local(s, sl.name)) continue;
380+ LocalVar *dl = scope_local_intern(s, sl.name);
381+ dl->type = sl.type;
382+ dl->is_param = sl.is_param;
383+ dl->is_block_param = sl.is_block_param;
384+ dl->proc_ret = sl.proc_ret;
385+ dl->is_cell = sl.is_cell;
386+ }
387+ break;
388+ }
389+}
390+
362391 void emit_method(Compiler *c, Scope *s, Buf *b) {
363392 if (s->cs_synth) { emit_compiler_state_method(c, s, b); return; }
364393 /* instance_eval/exec trampolines are inlined at every call site; the
@@ -380,6 +409,7 @@
380409 }
381410 return;
382411 }
412+ inherit_transplant_locals(c, s);
383413 emit_method_signature(c, s, b);
384414 buf_puts(b, " {\n");
385415 buf_puts(b, " SP_GC_SAVE();\n");