Commit c0419c9a

c0419c9a977fMerged

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

Description

Build int-keyed, int-valued hash literals as native IntIntHash

A hash literal with all-integer keys and integer values ({1 => 10}) was misinferred as str_int_hash: the analyzer's literal-type inference and the codegen construction both lacked an int-key/int-value arm and fell through to the str_int default. The generated C then passed the integer keys to sp_StrIntHash_set/_get as const char *, hashing a small integer as a pointer and crashing.

sp_IntIntHash already exists in the runtime and codegen had partial support, so this completes the analyze + codegen plumbing for the literal path:

  • infer_hash_val_type (analyze and codegen): all-int keys + int/bool/ nil/bigint values -> int_int_hash;
  • compile_hash_literal: emit sp_IntIntHash_new()/_set() (bigint values unbox via sp_bigint_to_int, mirroring sym_int_hash);
  • compile_bracket_assign / store: h[k] = v and h.store(k, v);
  • hash_leaf_type, hash_fetch_block_info, is_hash_type: recognize the variant and report its int leaf.

int-keyed-int-valued hashes built from an empty {} plus []= writes are deliberately left to the existing poly fallback: promoting the empty-hash default would update the slot's decl without the {} construction and write sites following it, yielding a type-mismatched build. That promotion path is entangled with the same type-promotion instability tracked for #801 and is left for separate work.

Refs #801

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

Details

Commit
c0419c9a977f
Parent
155ec59a2269

spinel_analyze.rbmodifiedView file

@@ -3064,6 +3064,11 @@
30643064 if all_sym_keys == 1 && (first_vt == "int" || first_vt == "bool" || first_vt == "nil")
30653065 return "sym_int_hash"
30663066 end
3067+ # Int keys with int values -> int_int_hash (mirrors the sym_int_hash
3068+ # case above; bigint values unbox to mrb_int the same way).
3069+ if all_int_keys == 1 && (first_vt == "int" || first_vt == "bool" || first_vt == "nil" || first_vt == "bigint")
3070+ return "int_int_hash"
3071+ end
30673072 # promote-mode bigint values map onto sym_int_hash storage by
30683073 # unboxing each bigint via sp_bigint_to_int at the codegen
30693074 # set/get sites (mirrors the codegen-side infer_hash_val_type).
@@ -7763,7 +7768,7 @@
77637768 if t == "str_int_hash" || t == "str_str_hash" || t == "int_str_hash"
77647769 return 1
77657770 end
7766- if t == "sym_int_hash" || t == "sym_str_hash"
7771+ if t == "sym_int_hash" || t == "sym_str_hash" || t == "int_int_hash"
77677772 return 1
77687773 end
77697774 if t == "str_poly_hash" || t == "sym_poly_hash"
@@ -7777,7 +7782,7 @@
77777782
77787783
77797784 def hash_leaf_type(recv_type)
7780- if recv_type == "str_int_hash" || recv_type == "sym_int_hash"
7785+ if recv_type == "str_int_hash" || recv_type == "sym_int_hash" || recv_type == "int_int_hash"
77817786 return "int"
77827787 end
77837788 if recv_type == "str_str_hash" || recv_type == "sym_str_hash" || recv_type == "int_str_hash"
@@ -12710,6 +12715,11 @@
1271012715 if int_keys && str_vals
1271112716 return "int_str_hash"
1271212717 end
12718+ # int_keys && int_vals: an empty-`{}` slot refined from observed
12719+ # writes would promote the decl to int_int_hash without the
12720+ # construction / write sites following, so leave it to the poly
12721+ # fallback. int-keyed-int-valued hash *literals* are handled
12722+ # directly by infer_hash_val_type.
1271312723 # Fall back to poly when the observed pair doesn't fit a typed hash.
1271412724 if sym_keys
1271512725 return "sym_poly_hash"
@@ -18467,17 +18477,15 @@
1846718477 if vt == "string"
1846818478 return "int_str_hash"
1846918479 end
18470- # `kt == "int"` with `vt == "int"` is ambiguous: it could be a
18471- # real int-keyed-int-valued hash (no native variant, would go
18472- # to poly_poly_hash), OR it could be the analyzer's fallback
18473- # when the key expression's type couldn't be resolved on the
18474- # first pass (e.g. `parts[0]` where `parts` isn't declared
18475- # yet). Promoting to poly_poly_hash here is destructive: a
18476- # later pass that correctly resolves the key as "string" can't
18477- # downgrade poly_poly_hash back to str_int_hash. Defer to the
18478- # next pass instead. Non-int vt (array, obj, etc.) still
18479- # routes through the catch-all below since those are
18480- # unambiguous "real concrete write" signals.
18480+ # `kt == "int"` with `vt == "int"` would map to int_int_hash, but
18481+ # promoting an empty-`{}` local/ivar here is incomplete: the slot's
18482+ # decl is updated while the `{}` construction and `[]=` write sites
18483+ # still emit the str_int_hash default (the promotion doesn't
18484+ # propagate to the RHS), yielding a type-mismatched build. It is
18485+ # also ambiguous with the analyzer's first-pass "int" fallback for
18486+ # an unresolved key expression. Defer; int-keyed-int-valued hash
18487+ # *literals* (`{1 => 2}`) are inferred directly by
18488+ # infer_hash_val_type and don't rely on this promotion.
1848118489 if vt == "int" || vt == "bool" || vt == "nil"
1848218490 return ""
1848318491 end