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
end77287728
end77297729
7730+
def scalar_ivar_type?(t)7731+
t == "int" || t == "float" || t == "bool" || t == "symbol"7732+
end7733+
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+
end7738+
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 a77807789
# refinement promotes it to `obj_Method` — no heterogeneity77817790
# to widen for).7782-
if (old == "int" || old == "nil") && is_obj_type(new_type) == 1 && cls_ivar_definite_flag(ci, iname) == 17791+
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 = 177857795
@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 = 18074-
elsif vtype != "int"8084+
elsif vtype != "int" || cur == "nil"80758085
update_ivar_type(ci, iname, vtype)80768086
end80778087
end@@ -10996,7 +11006,14 @@
1099611006
if ij < ivar_types.length1099711007
if ivar_names[ij] == iname1099811008
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 = 111014+
else11015+
ivar_types[ij] = unify_call_types(old_t, new_t, -1)11016+
end1100011017
end1100111018
end1100211019
end@@ -13763,23 +13780,21 @@
1376313780
# Chain participants bypass the `int` guard so that1376413781
# `@string_slot = @int_slot = expr_returning_int` widens1376513782
# the head to poly instead of leaving it stuck at13766-
# `string`. The `nil` guard stays in place even for13767-
# chains: `@a = @b = ... = nil` is handled at emit time13768-
# by `compile_chained_ivar_writes`'s per-slot recurse13769-
# path (NilNode lowers to a literal `0`, a null pointer13770-
# constant valid for any slot type), and forcing nil13771-
# into the slot type interacts badly with parent-cascade13772-
# update_ivar_type — a subclass write that pins the same13773-
# slot to a concrete obj type ping-pongs between obj_X13774-
# 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 through13784+
# update_ivar_type when the current slot is scalar; object13785+
# nil writes stay on the existing nullable/finalize paths to13786+
# avoid parent-cascade ping-pong across passes.1377613787
if chain_inames.length > 1 && at == "int"1377713788
ci_idx = 01377813789
while ci_idx < chain_inames.length1377913790
update_ivar_type(@current_class_idx, chain_inames[ci_idx], at)1378013791
ci_idx = ci_idx + 11378113792
end13782-
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+
end13797+
elsif at != "int" || cls_ivar_type(@current_class_idx, iname) == "nil"1378313798
update_ivar_type(@current_class_idx, iname, at)1378413799
end1378513800
endtest/nil_scalar_ivar_widen.rbaddedView file
Loading diff…
test/nil_scalar_ivar_widen.rb.expectedaddedView file
Loading diff…