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
infer_param_array_type_from_body narrow_param_types_from_body_method_calls infer_string_param_from_body # Body-usage hash inference (#542). Lifts int-defaulted params # to str_poly_hash / sym_poly_hash when the body keyed-accesses # the param. Runs after the string-param lift so the # is_string_only_method check fires first for plain-string # consumers and the hash-shape inference only claims the param # when the body actually treats it as a hash. infer_hash_param_from_body # Body-usage hash inference (#542) used to run here; moved to # post-fixpoint below so call-site inference can pin a typed # caller's hash variant (str_str_hash, sym_int_hash, etc.) # first. Issue #556: when this fired inside the iterative # loop, an int-defaulted param would widen to str_poly_hash # in iter 0, then unify_call_types(str_poly_hash, str_str_hash) # at the caller widens further to poly -- even though the # caller was always typed. widen_nil_default_params_used_as_hash widen_params_from_ivar_hash_aset infer_param_type_from_callee_slot infer_function_body_call_types infer_class_body_call_types infer_all_returns # Body-usage hash inference (#542) -- moved out of the # iterative loop per #556. Same reasoning as the array # pass below: by the time the fixpoint converges, any # param with a typed caller has been pinned to a concrete # variant (str_str_hash / sym_int_hash / etc.); only # genuinely-untyped params (int / nil default) reach the # widening here, so unify_call_types doesn't get a chance # to widen the freshly-claimed str_poly_hash back to poly # against a more-specific typed caller. Followup # narrow_param_hash_types_from_body_writes refines the # freshly-widened str_poly_hash / sym_poly_hash slot down # to a more specific variant (str_int_hash / sym_int_hash # / etc.) when body writes pin the value type; without # this call, untyped-caller-only patterns lose the # precision the in-loop narrow pass used to give them. infer_hash_param_from_body narrow_param_hash_types_from_body_writes infer_all_returns infer_function_body_call_types infer_class_body_call_types infer_all_returns # Body-usage array inference (#545). Runs ONCE post-fixpoint: # by this point any param that has a typed call site has been # widened to a concrete narrow variant (int_array, str_array,test/typed_hash_caller_no_body_widen.rbaddedView file
Loading diff…
test/typed_hash_caller_no_body_widen.rb.expectedaddedView file
Loading diff…