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 end
75617561 end
75627562
7563+ def scalar_ivar_type?(t)
7564+ t == "int" || t == "float" || t == "bool" || t == "symbol"
7565+ end
7566+
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+ end
7571+
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 a
76137622 # refinement promotes it to `obj_Method` — no heterogeneity
76147623 # to widen for).
7615- nil_scalar_mix = 0
7616- if old == "nil" && (new_type == "int" || new_type == "float" || new_type == "bool" || new_type == "symbol")
7617- nil_scalar_mix = 1
7618- elsif new_type == "nil" && (old == "int" || old == "float" || old == "bool" || old == "symbol")
7619- nil_scalar_mix = 1
7620- end
7621- if nil_scalar_mix == 1
7622- types[k] = "poly"
7623- @needs_rb_value = 1
7624- @cls_ivar_types[ci] = types.join(";")
7625- @cls_ivar_types_version = @cls_ivar_types_version + 1
7626- elsif (old == "int" || old == "nil") && is_obj_type(new_type) == 1 && cls_ivar_definite_flag(ci, iname) == 1
7624+ 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 = 1
76297628 @cls_ivar_types[ci] = types.join(";")
@@ -10840,11 +10839,13 @@
1084010839 if ij < ivar_types.length
1084110840 if ivar_names[ij] == iname
1084210841 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 = 1
1084610847 else
10847- 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 end
1084910850 end
1085010851 end
@@ -13435,23 +13436,21 @@
1343513436 # Chain participants bypass the `int` guard so that
1343613437 # `@string_slot = @int_slot = expr_returning_int` widens
1343713438 # the head to poly instead of leaving it stuck at
13438- # `string`. The `nil` guard stays in place even for
13439- # chains: `@a = @b = ... = nil` is handled at emit time
13440- # by `compile_chained_ivar_writes`'s per-slot recurse
13441- # path (NilNode lowers to a literal `0`, a null pointer
13442- # constant valid for any slot type), and forcing nil
13443- # into the slot type interacts badly with parent-cascade
13444- # update_ivar_type — a subclass write that pins the same
13445- # slot to a concrete obj type ping-pongs between obj_X
13446- # 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 through
13440+ # update_ivar_type when the current slot is scalar; object
13441+ # nil writes stay on the existing nullable/finalize paths to
13442+ # avoid parent-cascade ping-pong across passes.
1344813443 if chain_inames.length > 1 && at == "int"
1344913444 ci_idx = 0
1345013445 while ci_idx < chain_inames.length
1345113446 update_ivar_type(@current_class_idx, chain_inames[ci_idx], at)
1345213447 ci_idx = ci_idx + 1
1345313448 end
13454- 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+ end
13453+ elsif at != "int" || cls_ivar_type(@current_class_idx, iname) == "nil"
1345513454 update_ivar_type(@current_class_idx, iname, at)
1345613455 end
1345713456 end