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
# `@h[k] = v`. Returns "" when the (key, value) pair has no # matching concrete container — the caller leaves the ivar type # alone in that case. def promote_empty_hash_for(kt, vt) def promote_empty_hash_for(kt, vt, key_nid = -1) if kt == "string" if vt == "string" return "str_str_hash" if vt == "string" return "int_str_hash" end # `kt == "int"` with `vt == "int"` would map to int_int_hash, but # promoting an empty-`{}` local/ivar here is incomplete: the slot's # decl is updated while the `{}` construction and `[]=` write sites # still emit the str_int_hash default (the promotion doesn't # propagate to the RHS), yielding a type-mismatched build. It is # also ambiguous with the analyzer's first-pass "int" fallback for # an unresolved key expression. Defer; int-keyed-int-valued hash # *literals* (`{1 => 2}`) are inferred directly by # infer_hash_val_type and don't rely on this promotion. # `kt == "int"` with `vt == "int"`: promote to int_int_hash only # when the key is a genuine Integer *literal* (`h[1] = 5`). `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. An # IntegerNode literal key is unambiguous. Without this the empty-{} # local stays str_int_hash and the int key reaches sp_StrIntHash_set # as a `const char *`, dereferenced -> segfault. if vt == "int" || vt == "bool" || vt == "nil" if key_nid >= 0 && @nd_type[key_nid] == "IntegerNode" return "int_int_hash" end return "" end end iow_iname = @nd_name[iow_recv] iow_cur = cls_ivar_type(@current_class_idx, iow_iname) if iow_cur == "str_int_hash" iow_promoted = promote_empty_hash_for(iow_kt, iow_vt) iow_promoted = promote_empty_hash_for(iow_kt, iow_vt, iow_args[0]) if iow_promoted != "" && iow_promoted != iow_cur replace_ivar_type(@current_class_idx, iow_iname, iow_promoted) if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash" iow_lname = @nd_name[iow_recv] iow_cur = find_var_type(iow_lname) if iow_cur == "str_int_hash" iow_promoted = promote_empty_hash_for(iow_kt, iow_vt) iow_promoted = promote_empty_hash_for(iow_kt, iow_vt, iow_args[0]) if iow_promoted != "" && iow_promoted != iow_cur set_var_type(iow_lname, iow_promoted) if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash" if ai.length >= 2 kt = infer_type(ai[0]) vt = infer_type(ai[ai.length - 1]) promoted = promote_empty_hash_for(kt, vt) promoted = promote_empty_hash_for(kt, vt, ai[0]) if promoted != "" && promoted != cur_t # Direct assign: update_ivar_type would widen the # existing-vs-new mismatch to `poly`, but we know this if iow_promotable && iow_ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[iow_ki] == "1" iow_kt = scan_locals_arg_type(iow_aargs[0], names, types, params) iow_vt = scan_locals_arg_type(iow_val, names, types, params) iow_promoted = promote_empty_hash_for(iow_kt, iow_vt) iow_promoted = promote_empty_hash_for(iow_kt, iow_vt, iow_aargs[0]) if iow_promoted != "" && iow_promoted != iow_cur types[iow_ki] = iow_promoted if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash" # `types[]`. Prefer that local types array when # the argument is a LocalVariableReadNode we # recorded; fall back to infer_type otherwise. promoted = promote_empty_hash_for(key_type, val_type) promoted = promote_empty_hash_for(key_type, val_type, aargs[0]) else # Non-empty literal init followed by a `[]=` write whose # value (or key) type doesn't fit the current variant'sspinel_codegen.rbmodifiedView file
Loading diff…
test/empty_hash_int_int_promote.rbaddedView file
Loading diff…
test/empty_hash_int_int_promote.rb.expectedaddedView file
Loading diff…