Commit cd60c2bc
cd60c2bc2d17Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 5 files · ~5 modified
Description
Fix var_table scoping: add var_scope_floor to prevent cross-function pollution
var_lookup now searches current scope first, then global scope. var_declare only updates entries within current scope, preventing method params from widening top-level variables to POLY.
All 7 temporary scope expansions in resolve_class_types now properly save/restore var_scope_floor and clear stale entries.
Fixes: obj = TheClass.new + def method(obj) no longer conflict. 57/57 tests, 39/39 benchmarks pass.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- cd60c2bc2d17
- Parent
- 99bd2a3861e8
src/codegen.cmodifiedView file
@@ -239,14 +239,27 @@
239239
/* ------------------------------------------------------------------ */240240
241241
var_entry_t *var_lookup(codegen_ctx_t *ctx, const char *name) {242-
for (int i = 0; i < ctx->var_count; i++)242+
/* Search current scope first (var_scope_floor..var_count) */243+
for (int i = ctx->var_scope_floor; i < ctx->var_count; i++)244+
if (ctx->vars[i].name[0] && strcmp(ctx->vars[i].name, name) == 0) return &ctx->vars[i];245+
/* Then search global/outer scope (0..var_scope_floor) */246+
for (int i = 0; i < ctx->var_scope_floor; i++)243247
if (ctx->vars[i].name[0] && strcmp(ctx->vars[i].name, name) == 0) return &ctx->vars[i];244248
return NULL;245249
}246250
247251
var_entry_t *var_declare(codegen_ctx_t *ctx, const char *name,248252
vtype_t type, bool is_constant) {249-
var_entry_t *v = var_lookup(ctx, name);253+
/* When inside a scoped region (var_scope_floor > 0), only look up within254+
* the current scope to avoid polluting outer scope variables with widening.255+
* For reads, var_lookup still searches both scopes. */256+
var_entry_t *v = NULL;257+
if (ctx->var_scope_floor > 0) {258+
for (int i = ctx->var_scope_floor; i < ctx->var_count; i++)259+
if (ctx->vars[i].name[0] && strcmp(ctx->vars[i].name, name) == 0) { v = &ctx->vars[i]; break; }260+
} else {261+
v = var_lookup(ctx, name);262+
}250263
if (v) {251264
/* Widen if types conflict */252265
if (v->type.kind != type.kind && v->type.kind != SPINEL_TYPE_UNKNOWN) {@@ -2755,6 +2768,8 @@
27552768
/* Also infer function body variables to detect array/hash usage */27562769
if (!ctx->needs_gc && f->body_node) {27572770
int saved_vc = ctx->var_count;2771+
int saved_sf = ctx->var_scope_floor;2772+
ctx->var_scope_floor = saved_vc;27582773
for (int j = 0; j < f->param_count; j++)27592774
var_declare(ctx, f->params[j].name, f->params[j].type, false);27602775
infer_pass(ctx, f->body_node);@@ -2767,6 +2782,7 @@
27672782
}27682783
}27692784
ctx->var_count = saved_vc;2785+
ctx->var_scope_floor = saved_sf;27702786
}27712787
}27722788
/* Also check class method bodies for array/hash usage */@@ -2781,6 +2797,8 @@
27812797
m->return_type.kind == SPINEL_TYPE_HASH) { ctx->needs_gc = true; break; }27822798
if (m->body_node) {27832799
int saved_vc = ctx->var_count;2800+
int saved_sf = ctx->var_scope_floor;2801+
ctx->var_scope_floor = saved_vc;27842802
for (int j = 0; j < m->param_count; j++)27852803
var_declare(ctx, m->params[j].name, m->params[j].type, false);27862804
infer_pass(ctx, m->body_node);@@ -2793,6 +2811,7 @@
27932811
}27942812
}27952813
ctx->var_count = saved_vc;2814+
ctx->var_scope_floor = saved_sf;27962815
}27972816
}27982817
ctx->parser = saved_gc;src/codegen.hmodifiedView file
Loading diff…
src/emit.cmodifiedView file
Loading diff…
src/expr.cmodifiedView file
Loading diff…
src/type.cmodifiedView file
Loading diff…