Commit 1814b4f1
1814b4f17933Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): empty hash with int/int key+value defers promotion (#437)
h = {} followed by h[parts[0]] = v where parts comes from a
split result widened h to poly_poly_hash. Root cause:
promote_empty_hash_for("int", "int") returned "poly_poly_hash"
via the catch-all if kt != "" arm — but kt="int" is also the
analyzer's fallback when the key expression's type couldn't be
resolved (parts[0] in a first-pass scan where parts isn't declared
yet). Widening to poly_poly_hash is destructive: a later pass that
correctly resolves parts as str_array (and hence parts[0] as
string) can't downgrade poly_poly_hash back to str_int_hash.
Defer instead: kt="int" with vt in {int,bool,nil} returns "" (no promotion). The next pass with the key correctly resolved as "string" then promotes to str_int_hash. The kt="int" vt="string" case (legitimate int-keyed-string-valued hash) is unchanged. Other non-int/non-string/non-symbol key types (Method, IntArray, generic obj_X) still hit the poly_poly_hash catch-all.
Side effect: bm_wordfreq benchmark goes from FAIL to PASS now that
counts is correctly typed as str_int_hash. bm_csv_process and
bm_io_wordcount still ERR for an unrelated reason (`puts k + ": "
- v.to_s` chained-concat doesn't lower to nested sp_str_concat).
Closes #437
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 1814b4f17933
- Parent
- c2ec3e02b08a
spinel_analyze.rbmodifiedView file
if vt == "string" return "int_str_hash" end # `kt == "int"` with `vt == "int"` is ambiguous: it could be a # real int-keyed-int-valued hash (no native variant, would go # to poly_poly_hash), OR it could be the analyzer's fallback # when the key expression's type couldn't be resolved on the # first pass (e.g. `parts[0]` where `parts` isn't declared # yet). Promoting to poly_poly_hash here is destructive: a # later pass that correctly resolves the key as "string" can't # downgrade poly_poly_hash back to str_int_hash. Defer to the # next pass instead. Non-int vt (array, obj, etc.) still # routes through the catch-all below since those are # unambiguous "real concrete write" signals. if vt == "int" || vt == "bool" || vt == "nil" return "" end end # Non-string / non-symbol / non-int key types: Method, IntArray, # generic obj_X, or already-poly. Use poly_poly_hash so the runtimetest/empty_hash_int_int_no_widen.rbaddedView file
Loading diff…
test/empty_hash_int_int_no_widen.rb.expectedaddedView file
Loading diff…