Commit 9d4d4a02

9d4d4a02bc95Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · ~3 modified

Description

Fix GC bug: local IntArray vars not tracked as roots when param names collide

When a function (e.g., sd_solve) has local variables with the same names as parameters in a previously-emitted function (e.g., sd_update_forward's sr/sc params), var_declare would reuse existing entries at indices below the param boundary. The local variable declaration loop used saved_var_count + param_count as its start index, which assumed all params created new entries -- but params sharing names with top-level vars reuse existing slots, so the actual var_count after params could be lower than expected. This caused local IntArray variables to be declared without = NULL initialization and without SP_GC_ROOT registration.

Three fixes applied:

  1. Track var_count_after_params instead of computing saved_var_count + param_count, so the local var loop correctly covers all new entries.
  2. Clear stale var table entries beyond saved_var_count when entering a new function/method scope, preventing cross-function name collisions.
  3. Add name[0] guard in var_lookup to skip cleared entries.
  4. Fix infer_type to return SPINEL_TYPE_ARRAY for all Array.new calls, not just zero-argument ones.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
9d4d4a02bc95
Parent
90768cc2d0e2

src/codegen.cmodifiedView file

@@ -240,7 +240,7 @@
240240
241241 var_entry_t *var_lookup(codegen_ctx_t *ctx, const char *name) {
242242 for (int i = 0; i < ctx->var_count; i++)
243- if (strcmp(ctx->vars[i].name, name) == 0) return &ctx->vars[i];
243+ if (ctx->vars[i].name[0] && strcmp(ctx->vars[i].name, name) == 0) return &ctx->vars[i];
244244 return NULL;
245245 }
246246