Commit 6363ab43

6363ab430bfeMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

include: sync copy params to source + register transplanted ivars

After the fixpoint, propagate include-copy param types back to the transplanted source scope so the final infer_type scan (which uses comp_scope_of mapping body nodes to the original scope) sees the correct types for LocalVariableReadNodes.

Also scan source body for ivar accesses in process_include_body and register them in the destination class, so the emitted struct layout includes ivars written by late-added module methods.

Move is_transplanted_source flag outside the nparams>0 guard so zero-param module methods are also skipped during codegen emission.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
6363ab430bfe
Parent
af4cd1a4b612

src/analyze.cmodifiedView file

@@ -2491,6 +2491,7 @@
24912491 dst->nrequired = src->nrequired;
24922492 dst->rest_idx = src->rest_idx;
24932493 if (src->blk_param) dst->blk_param = strdup(src->blk_param);
2494+ src->is_transplanted_source = 1;
24942495 /* Copy parameter names and defaults. */
24952496 dst->nparams = src->nparams;
24962497 if (src->nparams > 0) {
@@ -2507,8 +2508,21 @@
25072508 lv->is_param = 1;
25082509 }
25092510 }
2510- src->is_transplanted_source = 1;
25112511 }
2512+ /* Scan source body for ivar accesses and register them in the
2513+ destination class so codegen's struct layout includes them. */
2514+ for (int id2 = 0; id2 < nt->count; id2++) {
2515+ if (c->nscope[id2] != ms) continue;
2516+ const char *bty = nt_type(nt, id2);
2517+ if (!bty) continue;
2518+ if (!strcmp(bty, "InstanceVariableWriteNode") ||
2519+ !strcmp(bty, "InstanceVariableReadNode") ||
2520+ !strcmp(bty, "InstanceVariableOperatorWriteNode") ||
2521+ !strcmp(bty, "InstanceVariableOrWriteNode")) {
2522+ const char *ivnm = nt_str(nt, id2, "name");
2523+ if (ivnm) comp_ivar_intern(&c->classes[ci], ivnm);
2524+ }
2525+ }
25122526 }
25132527 }
25142528 }
@@ -5086,30 +5100,35 @@
50865100 (e.g. a never-called method with an uninferrable param). */
50875101 compute_reachable(c);
50885102
5089- /* Post-fixpoint: scope copies created by register_includes have pnames but no
5090- locals (since nscope still maps body nodes to the original scope). Find each
5091- such copy and transplant the finalized locals + return type from the source. */
5103+ /* Post-fixpoint: propagate include-copy param types back to the source
5104+ scope so the final infer_type scan (which uses comp_scope_of, mapping
5105+ body nodes to the ORIGINAL scope) sees the correctly-typed params.
5106+ Without this, LocalVariableReadNodes inside the body get TY_UNKNOWN
5107+ because the source scope's params were never updated (no direct calls
5108+ go through it). */
50925109 for (int ci = 0; ci < c->nscopes; ci++) {
5093- Scope *s = &c->scopes[ci];
5094- if (!s->name || s->nparams == 0 || s->nlocals > 0) continue;
5095- /* Has params but no locals: a copy whose params were never registered. */
5096- for (int si = 0; si < c->nscopes; si++) {
5097- if (si == ci) continue;
5098- Scope *src = &c->scopes[si];
5099- if (!src->name || strcmp(src->name, s->name)) continue;
5100- if (src->body != s->body || src->nlocals == 0) continue;
5101- if (src->nparams != s->nparams) continue;
5102- if (!scope_local(src, s->pnames[0])) continue;
5103- s->nlocals = src->nlocals;
5104- s->clocals = src->nlocals;
5105- s->locals = malloc(sizeof(LocalVar) * (size_t)src->nlocals);
5106- for (int l = 0; l < src->nlocals; l++) {
5107- s->locals[l] = src->locals[l];
5108- s->locals[l].name = src->locals[l].name ? strdup(src->locals[l].name) : NULL;
5110+ Scope *copy = &c->scopes[ci];
5111+ if (!copy->name || !copy->is_transplanted_source || copy->nparams == 0) continue;
5112+ /* This is a transplanted SOURCE: find copies (same body, different class_id,
5113+ params registered and typed) and unify their param types back here. */
5114+ for (int k = 0; k < c->nscopes; k++) {
5115+ if (k == ci) continue;
5116+ Scope *dst = &c->scopes[k];
5117+ if (!dst->name || strcmp(dst->name, copy->name)) continue;
5118+ if (dst->body != copy->body || dst->nparams != copy->nparams) continue;
5119+ if (!dst->is_transplanted_source) {
5120+ /* dst is a copy: unify its param types into the source */
5121+ for (int p = 0; p < copy->nparams; p++) {
5122+ if (!copy->pnames[p]) continue;
5123+ LocalVar *slv = scope_local(copy, copy->pnames[p]);
5124+ LocalVar *dlv = scope_local(dst, dst->pnames[p]);
5125+ if (!slv || !dlv || dlv->type == TY_UNKNOWN) continue;
5126+ TyKind mg = ty_unify(slv->type, dlv->type);
5127+ if (mg != slv->type) slv->type = mg;
5128+ }
5129+ if (dst->ret != TY_UNKNOWN && copy->ret == TY_UNKNOWN)
5130+ copy->ret = dst->ret;
51095131 }
5110- s->ret = src->ret;
5111- s->ret_proc_ret = src->ret_proc_ret;
5112- break;
51135132 }
51145133 }
51155134