Commit 1f054fd8

1f054fd8169cMerged

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

Description

Promote an empty-{} hash with an integer-literal key to int_int_hash

h = {}; h[1] = 5 segfaulted. The empty {} stays at the str_int_hash default, so the integer key reaches sp_StrIntHash_set / sp_StrIntHash_get as a const char * and is dereferenced. promote_empty_hash_for deferred the int-key/int-value case, and codegen's local empty-{} construction had no int_int_hash arm, so even when the slot decl promoted, the {} construction stayed str_int_hash and mismatched it.

Promote (int, int) to int_int_hash, gated on the key being a genuine Integer literal (IntegerNode). kt == "int" is also the analyzer's first-pass fallback for an unresolved key expression (e.g. h[parts[0]] = 1, whose key only resolves to a String on a later pass), so promoting on the bare type string would store a String key into an int_int_hash. A literal int key is unambiguous. Thread the key node through promote_empty_hash_for's call sites, and add the int_int_hash arm to the local empty-{} construction emit so the {} and the slot decl agree (the read/write sites then follow the declared type).

Variable / expression integer keys (h[k] = 5) stay deferred: their "int" type can be a first-pass fallback, and promoting them broke real code (bm_csv_process, bm_wordfreq, string_scanner_nullable) where a key resolves to a String after the empty-hash scan. They keep the pre-existing str_int_hash behavior.

gate green: 844 tests, bench 57, optcarrot 59662, self-host fixpoint OK. (int_int_hash #each with a 2-arg block is a separate pre-existing gap, not touched here.)

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
1f054fd8169c
Parent
42c29318732a

spinel_analyze.rbmodifiedView file

@@ -20561,7 +20561,7 @@
2056120561 # `@h[k] = v`. Returns "" when the (key, value) pair has no
2056220562 # matching concrete container — the caller leaves the ivar type
2056320563 # alone in that case.
20564- def promote_empty_hash_for(kt, vt)
20564+ def promote_empty_hash_for(kt, vt, key_nid = -1)
2056520565 if kt == "string"
2056620566 if vt == "string"
2056720567 return "str_str_hash"
@@ -20584,16 +20584,19 @@
2058420584 if vt == "string"
2058520585 return "int_str_hash"
2058620586 end
20587- # `kt == "int"` with `vt == "int"` would map to int_int_hash, but
20588- # promoting an empty-`{}` local/ivar here is incomplete: the slot's
20589- # decl is updated while the `{}` construction and `[]=` write sites
20590- # still emit the str_int_hash default (the promotion doesn't
20591- # propagate to the RHS), yielding a type-mismatched build. It is
20592- # also ambiguous with the analyzer's first-pass "int" fallback for
20593- # an unresolved key expression. Defer; int-keyed-int-valued hash
20594- # *literals* (`{1 => 2}`) are inferred directly by
20595- # infer_hash_val_type and don't rely on this promotion.
20587+ # `kt == "int"` with `vt == "int"`: promote to int_int_hash only
20588+ # when the key is a genuine Integer *literal* (`h[1] = 5`). `kt ==
20589+ # "int"` is also the analyzer's first-pass fallback for an
20590+ # unresolved key expression (e.g. `h[parts[0]] = 1`, whose key only
20591+ # resolves to a String on a later pass), so promoting on the bare
20592+ # type string would store a String key into an int_int_hash. An
20593+ # IntegerNode literal key is unambiguous. Without this the empty-{}
20594+ # local stays str_int_hash and the int key reaches sp_StrIntHash_set
20595+ # as a `const char *`, dereferenced -> segfault.
2059620596 if vt == "int" || vt == "bool" || vt == "nil"
20597+ if key_nid >= 0 && @nd_type[key_nid] == "IntegerNode"
20598+ return "int_int_hash"
20599+ end
2059720600 return ""
2059820601 end
2059920602 end
@@ -21349,7 +21352,7 @@
2134921352 iow_iname = @nd_name[iow_recv]
2135021353 iow_cur = cls_ivar_type(@current_class_idx, iow_iname)
2135121354 if iow_cur == "str_int_hash"
21352- iow_promoted = promote_empty_hash_for(iow_kt, iow_vt)
21355+ iow_promoted = promote_empty_hash_for(iow_kt, iow_vt, iow_args[0])
2135321356 if iow_promoted != "" && iow_promoted != iow_cur
2135421357 replace_ivar_type(@current_class_idx, iow_iname, iow_promoted)
2135521358 if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"
@@ -21365,7 +21368,7 @@
2136521368 iow_lname = @nd_name[iow_recv]
2136621369 iow_cur = find_var_type(iow_lname)
2136721370 if iow_cur == "str_int_hash"
21368- iow_promoted = promote_empty_hash_for(iow_kt, iow_vt)
21371+ iow_promoted = promote_empty_hash_for(iow_kt, iow_vt, iow_args[0])
2136921372 if iow_promoted != "" && iow_promoted != iow_cur
2137021373 set_var_type(iow_lname, iow_promoted)
2137121374 if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"
@@ -21496,7 +21499,7 @@
2149621499 if ai.length >= 2
2149721500 kt = infer_type(ai[0])
2149821501 vt = infer_type(ai[ai.length - 1])
21499- promoted = promote_empty_hash_for(kt, vt)
21502+ promoted = promote_empty_hash_for(kt, vt, ai[0])
2150021503 if promoted != "" && promoted != cur_t
2150121504 # Direct assign: update_ivar_type would widen the
2150221505 # existing-vs-new mismatch to `poly`, but we know this
@@ -31566,7 +31569,7 @@
3156631569 if iow_promotable && iow_ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[iow_ki] == "1"
3156731570 iow_kt = scan_locals_arg_type(iow_aargs[0], names, types, params)
3156831571 iow_vt = scan_locals_arg_type(iow_val, names, types, params)
31569- iow_promoted = promote_empty_hash_for(iow_kt, iow_vt)
31572+ iow_promoted = promote_empty_hash_for(iow_kt, iow_vt, iow_aargs[0])
3157031573 if iow_promoted != "" && iow_promoted != iow_cur
3157131574 types[iow_ki] = iow_promoted
3157231575 if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"
@@ -31636,7 +31639,7 @@
3163631639 # `types[]`. Prefer that local types array when
3163731640 # the argument is a LocalVariableReadNode we
3163831641 # recorded; fall back to infer_type otherwise.
31639- promoted = promote_empty_hash_for(key_type, val_type)
31642+ promoted = promote_empty_hash_for(key_type, val_type, aargs[0])
3164031643 else
3164131644 # Non-empty literal init followed by a `[]=` write whose
3164231645 # value (or key) type doesn't fit the current variant's