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

@@ -11632,6 +11632,20 @@
1163211632 if vt == "string"
1163311633 return "int_str_hash"
1163411634 end
11635+ # `kt == "int"` with `vt == "int"` is ambiguous: it could be a
11636+ # real int-keyed-int-valued hash (no native variant, would go
11637+ # to poly_poly_hash), OR it could be the analyzer's fallback
11638+ # when the key expression's type couldn't be resolved on the
11639+ # first pass (e.g. `parts[0]` where `parts` isn't declared
11640+ # yet). Promoting to poly_poly_hash here is destructive: a
11641+ # later pass that correctly resolves the key as "string" can't
11642+ # downgrade poly_poly_hash back to str_int_hash. Defer to the
11643+ # next pass instead. Non-int vt (array, obj, etc.) still
11644+ # routes through the catch-all below since those are
11645+ # unambiguous "real concrete write" signals.
11646+ if vt == "int" || vt == "bool" || vt == "nil"
11647+ return ""
11648+ end
1163511649 end
1163611650 # Non-string / non-symbol / non-int key types: Method, IntArray,
1163711651 # generic obj_X, or already-poly. Use poly_poly_hash so the runtime