Commit e8af2af8
e8af2af804a6Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 6 files · +4 added · ~2 modified
Description
fix(analyze,codegen): Hash#fetch with hash default on int-leaf hash
#463. Sequel to #454 (string-default case closed via sp_int_to_s).
The hash-default case (fetch "k", {}) cannot unify the two
ternary arms to a single primitive: get returns int, default is
a hash pointer. Box both arms to sp_RbVal and surface the call's
result type as poly.
The same approach landed in 9f38cf1 and was reverted in be09bfa
because the downstream sub = raw_sub.is_a?(Hash) ? raw_sub : {}
shape in real-blog cascaded into 10 new C compile errors. The
revert noted that the underlying blocker is scan_locals'
narrowing-vs-poly interaction.
Root cause located: refine_method_body_locals and refine_locals_multi_pass_full both run a multi-pass scan to re-resolve LV types once intermediate locals are in scope. The pass-2 merge rules update concrete-to-concrete refinements (int_array -> str_array, str_int_hash -> str_str_hash, etc.) but had no rule for concrete-to-poly. For
raw_sub = params.fetch "article", {} # poly sub = if raw_sub.is_a?(Hash) then raw_sub else {} end
pass 1 typed sub's then-arm as int (raw_sub not yet declared,
LocalVariableReadNode -> int default) and unified with {}
(str_int_hash) to get str_int_hash. Pass 2 with raw_sub now
declared as poly correctly unifies to poly via
unify_return_type's "genuinely different types" branch -- but
the merge rejected the update and sub stayed str_int_hash.
Codegen then emitted sp_StrIntHash * for a slot that's
sp_RbVal at runtime: incompatible-pointer assignment.
Adding concrete -> poly to both pass-2 merge tables closes the
cascade. The rule is direction-strict: only fires when pass N
specifically returned poly (which happens only when the LV is
written from sites of genuinely incompatible types), so a
pass-1 concrete type that's still correct cannot be wrongly
widened.
Validation: bootstrap 4-way fixpoint OK, 434 tests pass
(includes the hash_fetch_hash_default test from 9f38cf1 with
its missing .expected restored, plus a new narrowing-cascade
regression test mirroring the AR from_raw shape).
Closes #463.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- e8af2af804a6
- Parent
- 649ac632110c
spinel_analyze.rbmodifiedView file
if def_at_f == "string" || def_at_f == "mutable_str" return "string" end # Hash-typed default (`fetch "k", {}`). The two ternary arms # are int (from the get) and a hash pointer (from the default # literal) with no shared primitive type. Box both to # sp_RbVal and surface the result as poly. The receiving LV # slot widens to sp_RbVal via the refine pass's # concrete-to-poly merge rule (below in this file). if is_hash_type(def_at_f) == 1 @needs_rb_value = 1 return "poly" end end end end ltypes[k] = lt[j] set_var_type(lnames[k], lt[j]) end # Pass N detected genuine polymorphism (writes from sites with # incompatible types) that an earlier pass missed because an # upstream local was still defaulted to int. Mirrors the rule # in refine_method_body_locals; same reasoning. if lt[j] == "poly" && ltypes[k] != "poly" ltypes[k] = "poly" set_var_type(lnames[k], "poly") @needs_rb_value = 1 end end k = k + 1 end elsif ltypes[k] == "str_int_hash" && ltypes2[j] == "str_str_hash" ltypes[k] = ltypes2[j] set_var_type(lnames[k], ltypes2[j]) # Pass 2 detected genuine polymorphism (the local is written # from sites of incompatible types) that pass 1 missed because # an upstream local was still defaulted to int. Example: # `sub = if raw_sub.is_a?(Hash) then raw_sub else {} end` where # raw_sub is poly (from `params.fetch "k", {}`): pass 1 typed # the then-arm as int (raw_sub not yet in scope) so unify gave # str_int_hash; pass 2 with raw_sub declared poly correctly # unifies to poly. Without this rule the LV slot stays # str_int_hash and codegen emits sp_StrIntHash * for a value # that's actually sp_RbVal at runtime — incompatible-pointer # assignment at the write site. elsif ltypes2[j] == "poly" && ltypes[k] != "poly" ltypes[k] = "poly" set_var_type(lnames[k], "poly") @needs_rb_value = 1 end end k = k + 1spinel_codegen.rbmodifiedView file
Loading diff…
test/hash_fetch_hash_default.rbaddedView file
Loading diff…
test/hash_fetch_hash_default.rb.expectedaddedView file
Loading diff…
test/hash_fetch_hash_default_narrowing.rbaddedView file
Loading diff…
test/hash_fetch_hash_default_narrowing.rb.expectedaddedView file
Loading diff…