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

@@ -3811,6 +3811,16 @@
38113811 if def_at_f == "string" || def_at_f == "mutable_str"
38123812 return "string"
38133813 end
3814+ # Hash-typed default (`fetch "k", {}`). The two ternary arms
3815+ # are int (from the get) and a hash pointer (from the default
3816+ # literal) with no shared primitive type. Box both to
3817+ # sp_RbVal and surface the result as poly. The receiving LV
3818+ # slot widens to sp_RbVal via the refine pass's
3819+ # concrete-to-poly merge rule (below in this file).
3820+ if is_hash_type(def_at_f) == 1
3821+ @needs_rb_value = 1
3822+ return "poly"
3823+ end
38143824 end
38153825 end
38163826 end
@@ -21649,6 +21659,15 @@
2164921659 ltypes[k] = lt[j]
2165021660 set_var_type(lnames[k], lt[j])
2165121661 end
21662+ # Pass N detected genuine polymorphism (writes from sites with
21663+ # incompatible types) that an earlier pass missed because an
21664+ # upstream local was still defaulted to int. Mirrors the rule
21665+ # in refine_method_body_locals; same reasoning.
21666+ if lt[j] == "poly" && ltypes[k] != "poly"
21667+ ltypes[k] = "poly"
21668+ set_var_type(lnames[k], "poly")
21669+ @needs_rb_value = 1
21670+ end
2165221671 end
2165321672 k = k + 1
2165421673 end
@@ -21957,6 +21976,21 @@
2195721976 elsif ltypes[k] == "str_int_hash" && ltypes2[j] == "str_str_hash"
2195821977 ltypes[k] = ltypes2[j]
2195921978 set_var_type(lnames[k], ltypes2[j])
21979+ # Pass 2 detected genuine polymorphism (the local is written
21980+ # from sites of incompatible types) that pass 1 missed because
21981+ # an upstream local was still defaulted to int. Example:
21982+ # `sub = if raw_sub.is_a?(Hash) then raw_sub else {} end` where
21983+ # raw_sub is poly (from `params.fetch "k", {}`): pass 1 typed
21984+ # the then-arm as int (raw_sub not yet in scope) so unify gave
21985+ # str_int_hash; pass 2 with raw_sub declared poly correctly
21986+ # unifies to poly. Without this rule the LV slot stays
21987+ # str_int_hash and codegen emits sp_StrIntHash * for a value
21988+ # that's actually sp_RbVal at runtime — incompatible-pointer
21989+ # assignment at the write site.
21990+ elsif ltypes2[j] == "poly" && ltypes[k] != "poly"
21991+ ltypes[k] = "poly"
21992+ set_var_type(lnames[k], "poly")
21993+ @needs_rb_value = 1
2196021994 end
2196121995 end
2196221996 k = k + 1