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 barex[i]read is not hash evidence (arrays index by int), and an array-returning method assigned toxmay 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] = vindex-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
kt = infer_type(c, argv[0]); if (kt == TY_SYMBOL) { vt = TY_INT; /* dummy: sym hash val is always poly */ } else if (kt == TY_STRING) { vt = TY_POLY; /* will map to STR_POLY */ } else if (kt == TY_INT) { vt = TY_POLY; /* will map to POLY_POLY */ } /* An int-key bare read (`x[i]`) is NOT strong hash evidence: arrays index by int too, and an array-returning method assigned to `x` may not have settled its element type yet, so promoting here would lock the slot to a hash before the array write is recognized. A genuine int-keyed hash is typed by its `[]=` writes or literal instead. */ else continue; } else continue; Scope *lsc = rnm ? comp_scope_of(c, recv) : NULL; LocalVar *lv = lsc ? scope_local(lsc, rnm) : NULL; if (!lv || lv->is_param || lv->is_block_param) continue; /* A bare `x[i]` read must not promote `x` to a hash if `x` elsewhere gets an array-typed write (`x = arr.sort_by{}` etc.): it is an array, and the hash type would otherwise collide with it. Mirrors the ivar guard. */ if (!is_push && !is_idx_write && lv->type == TY_UNKNOWN) { /* A bare `x[i]` read OR an `x[i] = v` element assignment must not promote `x` to a hash if `x` elsewhere gets an array-typed write (`x = a.split` etc.): it is an array indexed/assigned by position, and the hash type would otherwise collide with it. Mirrors the ivar guard. */ if (!is_push && lv->type == TY_UNKNOWN) { int has_array_write = 0; for (int w = 0; w < nt->count && !has_array_write; w++) { const char *wty = nt_type(nt, w);src/codegen_util.cmodifiedView file
Loading diff…