Commit cc4a3f07

cc4a3f07b5f6Merged

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

Description

Don't let int-key [] indexing promote arrays to int-keyed hashes

x[i] (read) and x[i] = v (element assignment) with an integer key are ambiguous with array indexing, so they must not promote a slot to an int-keyed hash: a genuine int-keyed hash is typed by its {...} literal or empty-{}-then-[]= construction instead. Two narrowings:

  • Drop the int-key case from the []/fetch read-promotion: a bare x[i] read is not hash evidence (arrays index by int), and an array-returning method assigned to x may not have settled its element type yet, so promoting there locked the slot to a hash before the array write was seen.
  • Extend the local-variable array-construction guard to cover x[i] = v index-writes (it previously applied only to reads), matching the ivar guard.

Surfaced while compiling the legacy compiler with spinelc: locals fed by split(...)/array-returning methods and then indexed by position were mis-typed as poly_poly_hash/int_str_hash, breaking .join.

Gates: 845 test, 57 bench, optcarrot checksum 59662 -- all green.

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

Details

Commit
cc4a3f07b5f6
Parent
3bccb3ba20c0

src/analyze_pass.cmodifiedView file

@@ -685,7 +685,11 @@
685685 kt = infer_type(c, argv[0]);
686686 if (kt == TY_SYMBOL) { vt = TY_INT; /* dummy: sym hash val is always poly */ }
687687 else if (kt == TY_STRING) { vt = TY_POLY; /* will map to STR_POLY */ }
688- else if (kt == TY_INT) { vt = TY_POLY; /* will map to POLY_POLY */ }
688+ /* An int-key bare read (`x[i]`) is NOT strong hash evidence: arrays
689+ index by int too, and an array-returning method assigned to `x` may
690+ not have settled its element type yet, so promoting here would lock
691+ the slot to a hash before the array write is recognized. A genuine
692+ int-keyed hash is typed by its `[]=` writes or literal instead. */
689693 else continue;
690694 }
691695 else continue;
@@ -716,10 +720,11 @@
716720 Scope *lsc = rnm ? comp_scope_of(c, recv) : NULL;
717721 LocalVar *lv = lsc ? scope_local(lsc, rnm) : NULL;
718722 if (!lv || lv->is_param || lv->is_block_param) continue;
719- /* A bare `x[i]` read must not promote `x` to a hash if `x` elsewhere gets
720- an array-typed write (`x = arr.sort_by{}` etc.): it is an array, and the
721- hash type would otherwise collide with it. Mirrors the ivar guard. */
722- if (!is_push && !is_idx_write && lv->type == TY_UNKNOWN) {
723+ /* A bare `x[i]` read OR an `x[i] = v` element assignment must not promote
724+ `x` to a hash if `x` elsewhere gets an array-typed write (`x = a.split`
725+ etc.): it is an array indexed/assigned by position, and the hash type
726+ would otherwise collide with it. Mirrors the ivar guard. */
727+ if (!is_push && lv->type == TY_UNKNOWN) {
723728 int has_array_write = 0;
724729 for (int w = 0; w < nt->count && !has_array_write; w++) {
725730 const char *wty = nt_type(nt, w);