Commit 5cdf3e62

5cdf3e62ee5fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): set_var_type keeps *_poly_hash slot wider than narrower assigns

Half of #613. After the analyzer widened a local's C slot to <key>_poly_hash (to accommodate later poly-value writes), the initial merged = StrStrHash.dup assignment ran set_var_type with rhs_t = "str_str_hash". set_var_type then narrowed the tracked slot type back to str_str_hash. compile_bracket_assign's infer_type lookup at the next merged[k] = v site picked the narrowed type and emitted sp_StrStrHash_set against an sp_StrPolyHash * slot — hard cc error.

Fix: set_var_type now refuses to narrow when the current slot is a *_poly_hash variant and the incoming type is its scalar-value sibling (str_poly_hash <- str_str_hash / str_int_hash; same for sym_poly_hash and poly_poly_hash). The C slot's storage type is fixed at the declared widened variant, so the tracked type must stay aligned with it.

Belt-and-suspenders: compile_bracket_assign also consults find_var_declared_type when the recv is a LocalVariableReadNode and the declared type is a hash variant that differs from infer_type's answer — picks the declared type. That guard catches the same shape independently of the set_var_type fix.

The other half of #613 (RBS-declared Hash[untyped, untyped] param emits as mrb_int instead of sp_RbVal) is a separate analyzer-side issue and stays open. Refs #613 — not Closes — pending that.

Test: test/str_poly_hash_widen_keeps_slot_type.rb covers the dispatch shape without the RBS-param surface that the original 26-LOC repro pulls in. 588 tests pass / bootstrap fixpoint clean.

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

Details

Commit
5cdf3e62ee5f
Parent
cad6d50d8a16

spinel_codegen.rbmodifiedView file

@@ -1782,6 +1782,25 @@
17821782 i = @scope_names.length - 1
17831783 while i >= 0
17841784 if @scope_names[i] == name
1785+ # When the declared slot is a *_poly_hash variant, a subsequent
1786+ # narrower-shape assignment (`<str_poly_hash slot> = StrStrHash.dup`
1787+ # — the analyzer widened the slot's C type at declaration time to
1788+ # accommodate later poly-value writes, but the initial RHS is the
1789+ # narrower StrStrHash) must not collapse the tracked type back to
1790+ # the narrower variant. The C slot's storage type is fixed at
1791+ # declaration; downstream emit sites (bracket_assign, etc.) read
1792+ # the tracked type to pick the right runtime helper, and dispatching
1793+ # on the narrower variant against a wider slot tries to call
1794+ # sp_StrStrHash_set against an sp_StrPolyHash * — hard cc error.
1795+ # Issue #613.
1796+ cur = @scope_types[i]
1797+ if (cur == "str_poly_hash" && vtype == "str_str_hash") ||
1798+ (cur == "str_poly_hash" && vtype == "str_int_hash") ||
1799+ (cur == "sym_poly_hash" && vtype == "sym_str_hash") ||
1800+ (cur == "sym_poly_hash" && vtype == "sym_int_hash") ||
1801+ (cur == "poly_poly_hash" && (vtype == "str_str_hash" || vtype == "str_int_hash" || vtype == "sym_str_hash" || vtype == "sym_int_hash" || vtype == "int_str_hash" || vtype == "str_poly_hash" || vtype == "sym_poly_hash"))
1802+ return
1803+ end
17851804 @scope_types[i] = vtype
17861805 return
17871806 end
@@ -29229,6 +29248,23 @@
2922929248 def compile_bracket_assign(nid)
2923029249 recv = @nd_receiver[nid]
2923129250 rt = infer_type(recv)
29251+ # When the recv is a local whose declared C slot type is a
29252+ # different (wider) hash variant than the analyzer's per-call-
29253+ # site infer_type, prefer the declared type. The analyzer's
29254+ # refine_method_body_locals pass-2 widens locals to str_poly_hash
29255+ # when cross-type writes flow in (the post-#610 follow-on shape
29256+ # from #613); the matching scope decl already lays the local
29257+ # down as sp_StrPolyHash *, but infer_type may still report
29258+ # the un-widened str_str_hash from the initial `.dup`-from-
29259+ # str_str_hash writer. Dispatching on the un-widened type emits
29260+ # sp_StrStrHash_set against an sp_StrPolyHash * slot — hard cc
29261+ # error.
29262+ if @nd_type[recv] == "LocalVariableReadNode"
29263+ decl_rt = find_var_declared_type(@nd_name[recv])
29264+ if decl_rt != "" && decl_rt != rt && is_hash_type(decl_rt) == 1
29265+ rt = decl_rt
29266+ end
29267+ end
2923229268 # Nullable hash receivers (`str_str_hash?` etc.) should dispatch
2923329269 # identically to the base hash; NULL-check semantics live at the
2923429270 # caller, mirroring compile_call_expr's strip-? handling. Without