Commit f7f424fd
f7f424fd11a3Merged
ydah <[email protected]> · 6 months ago · 3 files · ~3 modified
Description
fix(analyze): widen scalar ivars assigned nil
Details
- Commit
- f7f424fd11a3
- Parent
- e6c85b7fbbee
spinel_analyze.rbmodifiedView file
@@ -7560,6 +7560,15 @@
75607560
end75617561
end75627562
7563+
def scalar_ivar_type?(t)7564+
t == "int" || t == "float" || t == "bool" || t == "symbol"7565+
end7566+
7567+
def nil_scalar_ivar_mix?(old_type, new_type)7568+
(old_type == "nil" && scalar_ivar_type?(new_type)) ||7569+
(new_type == "nil" && scalar_ivar_type?(old_type))7570+
end7571+
75637572
def update_ivar_type(ci, iname, new_type)75647573
names = @cls_ivar_names[ci].split(";")75657574
types = @cls_ivar_types[ci].split(";")@@ -7612,18 +7621,8 @@
76127621
# `@m = method(:foo)` initially scans as int and a76137622
# refinement promotes it to `obj_Method` — no heterogeneity76147623
# to widen for).7615-
nil_scalar_mix = 07616-
if old == "nil" && (new_type == "int" || new_type == "float" || new_type == "bool" || new_type == "symbol")7617-
nil_scalar_mix = 17618-
elsif new_type == "nil" && (old == "int" || old == "float" || old == "bool" || old == "symbol")7619-
nil_scalar_mix = 17620-
end7621-
if nil_scalar_mix == 17622-
types[k] = "poly"7623-
@needs_rb_value = 17624-
@cls_ivar_types[ci] = types.join(";")7625-
@cls_ivar_types_version = @cls_ivar_types_version + 17626-
elsif (old == "int" || old == "nil") && is_obj_type(new_type) == 1 && cls_ivar_definite_flag(ci, iname) == 17624+
if nil_scalar_ivar_mix?(old, new_type) ||7625+
((old == "int" || old == "nil") && is_obj_type(new_type) == 1 && cls_ivar_definite_flag(ci, iname) == 1)76277626
types[k] = "poly"76287627
@needs_rb_value = 176297628
@cls_ivar_types[ci] = types.join(";")@@ -10840,11 +10839,13 @@
1084010839
if ij < ivar_types.length1084110840
if ivar_names[ij] == iname1084210841
if ptypes[pi] != ""10843-
if ivar_types[ij] == "nil" && (ptypes[pi] == "int" || ptypes[pi] == "float" || ptypes[pi] == "bool" || ptypes[pi] == "symbol")10842+
old_t = ivar_types[ij]10843+
new_t = ptypes[pi]10844+
if nil_scalar_ivar_mix?(old_t, new_t)1084410845
ivar_types[ij] = "poly"1084510846
@needs_rb_value = 11084610847
else10847-
ivar_types[ij] = unify_call_types(ivar_types[ij], ptypes[pi], -1)10848+
ivar_types[ij] = unify_call_types(old_t, new_t, -1)1084810849
end1084910850
end1085010851
end@@ -13435,23 +13436,21 @@
1343513436
# Chain participants bypass the `int` guard so that1343613437
# `@string_slot = @int_slot = expr_returning_int` widens1343713438
# the head to poly instead of leaving it stuck at13438-
# `string`. The `nil` guard stays in place even for13439-
# chains: `@a = @b = ... = nil` is handled at emit time13440-
# by `compile_chained_ivar_writes`'s per-slot recurse13441-
# path (NilNode lowers to a literal `0`, a null pointer13442-
# constant valid for any slot type), and forcing nil13443-
# into the slot type interacts badly with parent-cascade13444-
# update_ivar_type — a subclass write that pins the same13445-
# slot to a concrete obj type ping-pongs between obj_X13446-
# and obj_X? across iter rounds and lands on poly,13447-
# breaking the typed-pointer store at emit time.13439+
# `string`. Plain `nil` writes are routed through13440+
# update_ivar_type when the current slot is scalar; object13441+
# nil writes stay on the existing nullable/finalize paths to13442+
# avoid parent-cascade ping-pong across passes.1344813443
if chain_inames.length > 1 && at == "int"1344913444
ci_idx = 01345013445
while ci_idx < chain_inames.length1345113446
update_ivar_type(@current_class_idx, chain_inames[ci_idx], at)1345213447
ci_idx = ci_idx + 11345313448
end13454-
elsif at != "nil" && (at != "int" || cls_ivar_type(@current_class_idx, iname) == "nil")13449+
elsif at == "nil"13450+
if scalar_ivar_type?(cls_ivar_type(@current_class_idx, iname))13451+
update_ivar_type(@current_class_idx, iname, at)13452+
end13453+
elsif at != "int" || cls_ivar_type(@current_class_idx, iname) == "nil"1345513454
update_ivar_type(@current_class_idx, iname, at)1345613455
end1345713456
endtest/nil_scalar_ivar_widen.rbmodifiedView file
Loading diff…
test/nil_scalar_ivar_widen.rb.expectedmodifiedView file
Loading diff…