Commit 33a04d66

33a04d660349Merged

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

Description

fix(analyze): hash-param body-usage widening runs post-fixpoint

Ori Pekelman's #556: tep's tep_mustache_m_ivars got sp_RbVal lv_ivars even though the single caller passed a typed sp_StrStrHash *. The widening originated from infer_hash_param_from_body running inside the iterative inference loop: iter 0 found ivars["k"] in the body with an int-defaulted param, widened to str_poly_hash; then unify_call_types(str_poly_hash, str_str_hash) at the caller widened further to poly because the two variants don't unify.

Mirror the post-fixpoint pattern already established by #545's infer_array_param_from_body (the comment at the array call site explicitly explains the reasoning that applies here too): move infer_hash_param_from_body out of the iterative loop. By the time the fixpoint converges, any param with a typed caller has been pinned to a concrete variant; only genuinely-untyped (int / nil default) params reach the widening, and unify_call_types is past.

To avoid losing precision for untyped-caller-only patterns where body writes (h["k"] = 1) used to pin a specific variant via the in-loop narrow_param_hash_types_from_body_writes, re-run narrow_param_hash_types_from_body_writes immediately after the post-loop widen so the freshly-claimed str_poly_hash / sym_poly_hash refines down to str_int_hash / sym_int_hash when the body's value writes are uniform.

Verified against tep@9ca09fd: pre-fix sp_tep_mustache_m_ivars(sp_StrStrHash *, sp_RbVal) with the caller doing sp_box_obj(iv_ivars, SP_BUILTIN_STR_STR_HASH); post-fix (sp_StrStrHash *, sp_StrStrHash *) and the caller passes lv_req->iv_ivars directly. 526 tests pass, optcarrot checksum 59662 preserved.

Closes #556

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

Details

Commit
33a04d660349
Parent
3c63156f2478

spinel_analyze.rbmodifiedView file

@@ -18455,13 +18455,14 @@
1845518455 infer_param_array_type_from_body
1845618456 narrow_param_types_from_body_method_calls
1845718457 infer_string_param_from_body
18458- # Body-usage hash inference (#542). Lifts int-defaulted params
18459- # to str_poly_hash / sym_poly_hash when the body keyed-accesses
18460- # the param. Runs after the string-param lift so the
18461- # is_string_only_method check fires first for plain-string
18462- # consumers and the hash-shape inference only claims the param
18463- # when the body actually treats it as a hash.
18464- infer_hash_param_from_body
18458+ # Body-usage hash inference (#542) used to run here; moved to
18459+ # post-fixpoint below so call-site inference can pin a typed
18460+ # caller's hash variant (str_str_hash, sym_int_hash, etc.)
18461+ # first. Issue #556: when this fired inside the iterative
18462+ # loop, an int-defaulted param would widen to str_poly_hash
18463+ # in iter 0, then unify_call_types(str_poly_hash, str_str_hash)
18464+ # at the caller widens further to poly -- even though the
18465+ # caller was always typed.
1846518466 widen_nil_default_params_used_as_hash
1846618467 widen_params_from_ivar_hash_aset
1846718468 infer_param_type_from_callee_slot
@@ -18504,6 +18505,27 @@
1850418505 infer_function_body_call_types
1850518506 infer_class_body_call_types
1850618507 infer_all_returns
18508+ # Body-usage hash inference (#542) -- moved out of the
18509+ # iterative loop per #556. Same reasoning as the array
18510+ # pass below: by the time the fixpoint converges, any
18511+ # param with a typed caller has been pinned to a concrete
18512+ # variant (str_str_hash / sym_int_hash / etc.); only
18513+ # genuinely-untyped params (int / nil default) reach the
18514+ # widening here, so unify_call_types doesn't get a chance
18515+ # to widen the freshly-claimed str_poly_hash back to poly
18516+ # against a more-specific typed caller. Followup
18517+ # narrow_param_hash_types_from_body_writes refines the
18518+ # freshly-widened str_poly_hash / sym_poly_hash slot down
18519+ # to a more specific variant (str_int_hash / sym_int_hash
18520+ # / etc.) when body writes pin the value type; without
18521+ # this call, untyped-caller-only patterns lose the
18522+ # precision the in-loop narrow pass used to give them.
18523+ infer_hash_param_from_body
18524+ narrow_param_hash_types_from_body_writes
18525+ infer_all_returns
18526+ infer_function_body_call_types
18527+ infer_class_body_call_types
18528+ infer_all_returns
1850718529 # Body-usage array inference (#545). Runs ONCE post-fixpoint:
1850818530 # by this point any param that has a typed call site has been
1850918531 # widened to a concrete narrow variant (int_array, str_array,