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
i = @scope_names.length - 1 while i >= 0 if @scope_names[i] == name # When the declared slot is a *_poly_hash variant, a subsequent # narrower-shape assignment (`<str_poly_hash slot> = StrStrHash.dup` # — the analyzer widened the slot's C type at declaration time to # accommodate later poly-value writes, but the initial RHS is the # narrower StrStrHash) must not collapse the tracked type back to # the narrower variant. The C slot's storage type is fixed at # declaration; downstream emit sites (bracket_assign, etc.) read # the tracked type to pick the right runtime helper, and dispatching # on the narrower variant against a wider slot tries to call # sp_StrStrHash_set against an sp_StrPolyHash * — hard cc error. # Issue #613. cur = @scope_types[i] if (cur == "str_poly_hash" && vtype == "str_str_hash") || (cur == "str_poly_hash" && vtype == "str_int_hash") || (cur == "sym_poly_hash" && vtype == "sym_str_hash") || (cur == "sym_poly_hash" && vtype == "sym_int_hash") || (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")) return end @scope_types[i] = vtype return end def compile_bracket_assign(nid) recv = @nd_receiver[nid] rt = infer_type(recv) # When the recv is a local whose declared C slot type is a # different (wider) hash variant than the analyzer's per-call- # site infer_type, prefer the declared type. The analyzer's # refine_method_body_locals pass-2 widens locals to str_poly_hash # when cross-type writes flow in (the post-#610 follow-on shape # from #613); the matching scope decl already lays the local # down as sp_StrPolyHash *, but infer_type may still report # the un-widened str_str_hash from the initial `.dup`-from- # str_str_hash writer. Dispatching on the un-widened type emits # sp_StrStrHash_set against an sp_StrPolyHash * slot — hard cc # error. if @nd_type[recv] == "LocalVariableReadNode" decl_rt = find_var_declared_type(@nd_name[recv]) if decl_rt != "" && decl_rt != rt && is_hash_type(decl_rt) == 1 rt = decl_rt end end # Nullable hash receivers (`str_str_hash?` etc.) should dispatch # identically to the base hash; NULL-check semantics live at the # caller, mirroring compile_call_expr's strip-? handling. Withouttest/str_poly_hash_widen_keeps_slot_type.rbaddedView file
Loading diff…
test/str_poly_hash_widen_keeps_slot_type.rb.expectedaddedView file
Loading diff…