Commit fa74f721

fa74f7217fb3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

refactor(analyze): unify refine_* pass-2 merge rules into one helper

Three sites (refine_method_body_locals pass 2, refine_method_body_locals pass 3, refine_locals_multi_pass_full's pass loop) each carried a hand-written if-cascade of pair-wise refinement rules: int -> non-int, nil -> nullable pointer with ? wrap, str_poly_hash widening, int_array -> typed array, int_str_hash -> resolved variant, str_int_hash -> str_str_hash, tuple non-tuple -> tuple, tuple X -> Y, and (newest) concrete -> poly. Each site duplicated some rules and silently omitted others — refine_method_body_locals pass 3 only mirrored 2 of pass 2's 8 rules despite a "Mirror the merge rules from pass 2" comment, and refine_locals_multi_pass_full's pass loop took non-nullable pointers from cur=nil without the ? suffix the per-method counterpart adds.

Extract merge_refined_local_type(cur_t, new_t) as the single decision table. The clauses match every existing rule verbatim (the nil -> nullable rule keeps the ? wrap that refine_method_body_locals always did) and apply uniformly at all three sites. Pass 3 now refines on the full rule set; main-scope nil -> string now gets the ? it always needed. No tests regress on the broadened pass-3 behaviour because each refinement only strictly improves the type — there is no "concrete refined to default" path through the helper.

This is the first half of the type-inference plan after #463: a single source of truth for refinement transitions so future discoveries (concrete -> poly being one example) ship as a one-line addition rather than a triplicated cascade. The poly arm is the rule that closed #463; that path still fires through the helper.

Validation:

  • bootstrap 4-way fixpoint OK
  • 434 tests pass, 0 fail
  • #463 AR-pattern repro still compiles under -Werror=int-conversion
  • diff: 113 lines deleted, 100 added (net -13)

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

Details

Commit
fa74f7217fb3
Parent
e8af2af804a6

spinel_analyze.rbmodifiedView file

@@ -14549,6 +14549,87 @@
1454914549 result
1455014550 end
1455114551
14552+ # Two-pass scan_locals merge: given the older type stored in
14553+ # scope (cur_t) and a freshly-observed type from a re-scan with
14554+ # more locals declared (new_t), return the type that should
14555+ # replace cur_t, or "" to keep cur_t unchanged. Centralises the
14556+ # rule table that previously lived inline at every refine_* /
14557+ # multi-pass merge site. Each clause encodes a known transition
14558+ # where the later pass is strictly better informed; everything
14559+ # else (incl. regressions like T -> int) is rejected.
14560+ def merge_refined_local_type(cur_t, new_t)
14561+ if new_t == cur_t || new_t == ""
14562+ return ""
14563+ end
14564+ # Never regress to default placeholders. A pass that produced
14565+ # "int" / "void" usually means it walked into a scope where the
14566+ # local wasn't visible — keep the earlier pass's verdict.
14567+ if new_t == "int" || new_t == "void"
14568+ return ""
14569+ end
14570+ # int default → anything specific (incl. nil for nullable vars).
14571+ if cur_t == "int"
14572+ if new_t == "nil"
14573+ return ""
14574+ end
14575+ return new_t
14576+ end
14577+ # nil starting type → nullable pointer. Add "?" so the C slot
14578+ # gets NULL-init and tag handling. is_nullable_type already
14579+ # checks for trailing "?".
14580+ if cur_t == "nil"
14581+ if is_nullable_pointer_type(new_t) == 1
14582+ if is_nullable_type(new_t) == 1
14583+ return new_t
14584+ end
14585+ return new_t + "?"
14586+ end
14587+ return ""
14588+ end
14589+ # Concrete → poly: genuine polymorphism detected after an
14590+ # upstream local resolved. Fixes the #463 cascade where
14591+ # `sub = if raw_sub.is_a?(Hash) then raw_sub else {} end`
14592+ # got stuck at str_int_hash from pass 1.
14593+ if new_t == "poly"
14594+ return "poly"
14595+ end
14596+ # str_poly_hash / sym_poly_hash widen any hash variant (poly
14597+ # value side covers the disagreement).
14598+ if new_t == "str_poly_hash" || new_t == "sym_poly_hash"
14599+ return new_t
14600+ end
14601+ # int_array pass-1 default → typed / poly array once the push
14602+ # rhs resolves.
14603+ if cur_t == "int_array"
14604+ if new_t == "str_array" || new_t == "float_array" || new_t == "sym_array" || is_ptr_array_type(new_t) == 1 || new_t == "poly_array"
14605+ return new_t
14606+ end
14607+ end
14608+ # ptr_array lateral move (sp_FooPtrArray <-> sp_BarPtrArray) —
14609+ # pass 2 typically picks the right element class.
14610+ if is_ptr_array_type(cur_t) == 1 && is_ptr_array_type(new_t) == 1
14611+ return new_t
14612+ end
14613+ # int_str_hash pass-1 default → resolved key/value combination.
14614+ if cur_t == "int_str_hash"
14615+ if new_t == "str_str_hash" || new_t == "sym_str_hash" || new_t == "str_int_hash" || new_t == "sym_int_hash"
14616+ return new_t
14617+ end
14618+ end
14619+ # str_int_hash → str_str_hash (value refinement once rhs local
14620+ # is declared as string).
14621+ if cur_t == "str_int_hash" && new_t == "str_str_hash"
14622+ return new_t
14623+ end
14624+ # Tuple refinements: non-tuple → tuple, and tuple X → tuple Y.
14625+ if is_tuple_type(new_t) == 1
14626+ if is_tuple_type(cur_t) == 0 || cur_t != new_t
14627+ return new_t
14628+ end
14629+ end
14630+ ""
14631+ end
14632+
1455214633 def fix_lambda_return_types
1455314634 # For methods that return "lambda", check if they are called from
1455414635 # contexts that expect primitive types. If so, downgrade the return type.
@@ -21619,55 +21700,14 @@
2161921700 k = 0
2162021701 while k < lnames.length
2162121702 if lnames[k] == ln[j]
21622- if ltypes[k] == "int" || ltypes[k] == "nil"
21623- if lt[j] != "int" && lt[j] != "nil"
21624- ltypes[k] = lt[j]
21625- set_var_type(lnames[k], lt[j])
21703+ merged_p = merge_refined_local_type(ltypes[k], lt[j])
21704+ if merged_p != ""
21705+ ltypes[k] = merged_p
21706+ set_var_type(lnames[k], merged_p)
21707+ if merged_p == "poly"
21708+ @needs_rb_value = 1
2162621709 end
2162721710 end
21628- if ltypes[k] == "int_array" && lt[j] != "int_array" && lt[j] != "int"
21629- ltypes[k] = lt[j]
21630- set_var_type(lnames[k], lt[j])
21631- end
21632- if is_ptr_array_type(ltypes[k]) == 1 && lt[j] != ltypes[k] && is_ptr_array_type(lt[j]) == 1
21633- ltypes[k] = lt[j]
21634- set_var_type(lnames[k], lt[j])
21635- end
21636- if ltypes[k] == "str_int_hash" && lt[j] == "str_str_hash"
21637- ltypes[k] = lt[j]
21638- set_var_type(lnames[k], lt[j])
21639- end
21640- # When pass 0 saw an unresolved key expression (e.g. `parts[0]`
21641- # where parts wasn't declared yet) and produced `int_str_hash`
21642- # from `kt=int + vt=string`, but pass 1 with parts declared
21643- # correctly resolves the key as string (str_str_hash) or
21644- # symbol (sym_str_hash), promote the local to the better
21645- # variant. Symmetric to the str_int_hash → str_str_hash rule
21646- # above; without it the hash stays pinned at int_str_hash and
21647- # the call site emits `sp_IntStrHash_set(h, const char *, ...)`
21648- # with a -Wint-conversion error.
21649- if ltypes[k] == "int_str_hash" && (lt[j] == "str_str_hash" || lt[j] == "sym_str_hash" || lt[j] == "str_int_hash" || lt[j] == "sym_int_hash")
21650- ltypes[k] = lt[j]
21651- set_var_type(lnames[k], lt[j])
21652- end
21653- if is_tuple_type(lt[j]) == 1 && is_tuple_type(ltypes[k]) == 0
21654- ltypes[k] = lt[j]
21655- set_var_type(lnames[k], lt[j])
21656- end
21657- # tuple:X → tuple:Y refinement (matches emit_main pass 3).
21658- if is_tuple_type(ltypes[k]) == 1 && is_tuple_type(lt[j]) == 1 && ltypes[k] != lt[j]
21659- ltypes[k] = lt[j]
21660- set_var_type(lnames[k], lt[j])
21661- 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
2167121711 end
2167221712 k = k + 1
2167321713 end
@@ -21930,67 +21970,13 @@
2193021970 k = 0
2193121971 while k < lnames.length
2193221972 if lnames[k] == lnames2[j]
21933- if ltypes[k] == "int"
21934- if ltypes2[j] != "int"
21935- ltypes[k] = ltypes2[j]
21936- set_var_type(lnames[k], ltypes2[j])
21937- end
21938- elsif ltypes[k] == "nil" && is_nullable_pointer_type(ltypes2[j]) == 1
21939- if is_nullable_type(ltypes2[j]) == 1
21940- ltypes[k] = ltypes2[j]
21941- else
21942- ltypes[k] = ltypes2[j] + "?"
21943- end
21944- set_var_type(lnames[k], ltypes[k])
21945- elsif (ltypes2[j] == "str_poly_hash" || ltypes2[j] == "sym_poly_hash") && ltypes[k] != ltypes2[j]
21946- ltypes[k] = ltypes2[j]
21947- set_var_type(lnames[k], ltypes2[j])
21948- # pass 1 typed `results` as int_array (from
21949- # the empty `[]` literal). Pass 2's `<<` push observation,
21950- # with the rhs local now in scope, refined the type to
21951- # str_array / float_array / sym_array / *_ptr_array. Without
21952- # this branch the refined type is dropped and codegen emits
21953- # `sp_IntArray *` against a body that pushes typed pointers,
21954- # tripping the cc step at the push site.
21955- elsif ltypes[k] == "int_array" && ltypes2[j] != "int_array" &&
21956- (ltypes2[j] == "str_array" || ltypes2[j] == "float_array" ||
21957- ltypes2[j] == "sym_array" || is_ptr_array_type(ltypes2[j]) == 1 ||
21958- ltypes2[j] == "poly_array")
21959- ltypes[k] = ltypes2[j]
21960- set_var_type(lnames[k], ltypes2[j])
21961- # Pass 1 picked int_str_hash from an unresolved key
21962- # expression (`kt=int + vt=string` via the analyzer
21963- # fallback for an undeclared local). Pass 2 with the key
21964- # resolved as string (or symbol) corrects to str_str_hash /
21965- # sym_str_hash. Mirrors the str_int_hash → str_str_hash
21966- # widening rule in refine_locals_multi_pass_full.
21967- elsif ltypes[k] == "int_str_hash" && (ltypes2[j] == "str_str_hash" || ltypes2[j] == "sym_str_hash" || ltypes2[j] == "str_int_hash" || ltypes2[j] == "sym_int_hash")
21968- ltypes[k] = ltypes2[j]
21969- set_var_type(lnames[k], ltypes2[j])
21970- # Symmetric: pass 1 saw `vt=int` because the value's source
21971- # local (e.g. `pb` from `pb = b.split("/")`) wasn't declared
21972- # yet, leaving the hash at str_int_hash. Pass 2 with `pb`
21973- # declared as str_array correctly resolves pb[i] as string and
21974- # picks str_str_hash. Mirrors the rule in
21975- # refine_locals_multi_pass_full at line 20142.
21976- elsif ltypes[k] == "str_int_hash" && ltypes2[j] == "str_str_hash"
21977- ltypes[k] = ltypes2[j]
21978- 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
21973+ merged = merge_refined_local_type(ltypes[k], ltypes2[j])
21974+ if merged != ""
21975+ ltypes[k] = merged
21976+ set_var_type(lnames[k], merged)
21977+ if merged == "poly"
21978+ @needs_rb_value = 1
21979+ end
2199421980 end
2199521981 end
2199621982 k = k + 1
@@ -22016,12 +22002,13 @@
2201622002 k = 0
2201722003 while k < lnames.length
2201822004 if lnames[k] == lnames3[j]
22019- if ltypes[k] == "str_int_hash" && ltypes3[j] == "str_str_hash"
22020- ltypes[k] = ltypes3[j]
22021- set_var_type(lnames[k], ltypes3[j])
22022- elsif ltypes[k] == "int_str_hash" && (ltypes3[j] == "str_str_hash" || ltypes3[j] == "sym_str_hash" || ltypes3[j] == "str_int_hash" || ltypes3[j] == "sym_int_hash")
22023- ltypes[k] = ltypes3[j]
22024- set_var_type(lnames[k], ltypes3[j])
22005+ merged3 = merge_refined_local_type(ltypes[k], ltypes3[j])
22006+ if merged3 != ""
22007+ ltypes[k] = merged3
22008+ set_var_type(lnames[k], merged3)
22009+ if merged3 == "poly"
22010+ @needs_rb_value = 1
22011+ end
2202522012 end
2202622013 end
2202722014 k = k + 1