Commit 043ef65b

043ef65b49bbMerged

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

Description

Merge pull request #495 from ydah/fix/nil-numeric-ivar

fix(analyze): preserve nil state for scalar ivars

Details

Commit
043ef65b49bb
Parent
0600ee510ca6
Merge parent
f7f424fd11a3

spinel_analyze.rbmodifiedView file

@@ -7727,6 +7727,15 @@
77277727 end
77287728 end
77297729
7730+ def scalar_ivar_type?(t)
7731+ t == "int" || t == "float" || t == "bool" || t == "symbol"
7732+ end
7733+
7734+ def nil_scalar_ivar_mix?(old_type, new_type)
7735+ (old_type == "nil" && scalar_ivar_type?(new_type)) ||
7736+ (new_type == "nil" && scalar_ivar_type?(old_type))
7737+ end
7738+
77307739 def update_ivar_type(ci, iname, new_type)
77317740 names = @cls_ivar_names[ci].split(";")
77327741 types = @cls_ivar_types[ci].split(";")
@@ -7779,7 +7788,8 @@
77797788 # `@m = method(:foo)` initially scans as int and a
77807789 # refinement promotes it to `obj_Method` — no heterogeneity
77817790 # to widen for).
7782- if (old == "int" || old == "nil") && is_obj_type(new_type) == 1 && cls_ivar_definite_flag(ci, iname) == 1
7791+ if nil_scalar_ivar_mix?(old, new_type) ||
7792+ ((old == "int" || old == "nil") && is_obj_type(new_type) == 1 && cls_ivar_definite_flag(ci, iname) == 1)
77837793 types[k] = "poly"
77847794 @needs_rb_value = 1
77857795 @cls_ivar_types[ci] = types.join(";")
@@ -8071,7 +8081,7 @@
80718081 if new_def == 1 && cur_def == 1 && cur != vtype && cur != "poly"
80728082 replace_ivar_type(ci, iname, "poly")
80738083 @needs_rb_value = 1
8074- elsif vtype != "int"
8084+ elsif vtype != "int" || cur == "nil"
80758085 update_ivar_type(ci, iname, vtype)
80768086 end
80778087 end
@@ -10996,7 +11006,14 @@
1099611006 if ij < ivar_types.length
1099711007 if ivar_names[ij] == iname
1099811008 if ptypes[pi] != ""
10999- ivar_types[ij] = unify_call_types(ivar_types[ij], ptypes[pi], -1)
11009+ old_t = ivar_types[ij]
11010+ new_t = ptypes[pi]
11011+ if nil_scalar_ivar_mix?(old_t, new_t)
11012+ ivar_types[ij] = "poly"
11013+ @needs_rb_value = 1
11014+ else
11015+ ivar_types[ij] = unify_call_types(old_t, new_t, -1)
11016+ end
1100011017 end
1100111018 end
1100211019 end
@@ -13763,23 +13780,21 @@
1376313780 # Chain participants bypass the `int` guard so that
1376413781 # `@string_slot = @int_slot = expr_returning_int` widens
1376513782 # the head to poly instead of leaving it stuck at
13766- # `string`. The `nil` guard stays in place even for
13767- # chains: `@a = @b = ... = nil` is handled at emit time
13768- # by `compile_chained_ivar_writes`'s per-slot recurse
13769- # path (NilNode lowers to a literal `0`, a null pointer
13770- # constant valid for any slot type), and forcing nil
13771- # into the slot type interacts badly with parent-cascade
13772- # update_ivar_type — a subclass write that pins the same
13773- # slot to a concrete obj type ping-pongs between obj_X
13774- # and obj_X? across iter rounds and lands on poly,
13775- # breaking the typed-pointer store at emit time.
13783+ # `string`. Plain `nil` writes are routed through
13784+ # update_ivar_type when the current slot is scalar; object
13785+ # nil writes stay on the existing nullable/finalize paths to
13786+ # avoid parent-cascade ping-pong across passes.
1377613787 if chain_inames.length > 1 && at == "int"
1377713788 ci_idx = 0
1377813789 while ci_idx < chain_inames.length
1377913790 update_ivar_type(@current_class_idx, chain_inames[ci_idx], at)
1378013791 ci_idx = ci_idx + 1
1378113792 end
13782- elsif at != "int" && at != "nil"
13793+ elsif at == "nil"
13794+ if scalar_ivar_type?(cls_ivar_type(@current_class_idx, iname))
13795+ update_ivar_type(@current_class_idx, iname, at)
13796+ end
13797+ elsif at != "int" || cls_ivar_type(@current_class_idx, iname) == "nil"
1378313798 update_ivar_type(@current_class_idx, iname, at)
1378413799 end
1378513800 end