Commit 75a05343

75a053431aa3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Merge pull request #1254 from fc-anjos/feature/spinel-struct-member-typing

Keep nullable int struct members unboxed

Details

Commit
75a053431aa3
Parent
19317e2af8e8
Merge parent
ab07bd11e85d

spinel_analyze.rbmodifiedView file

@@ -10888,8 +10888,9 @@
1088810888 # Same base, one nullable: keep / adopt the nullable form
1088910889 # rather than collapsing to poly. `@x = "hi"` (string) +
1089010890 # `@x = some_string_or_nil` (string?) is one storage shape
10891- # — `const char *` with NULL as the nil sentinel.
10892- elsif base_type(old) == base_type(new_type) && is_nullable_pointer_type(old) == 1
10891+ # — `const char *` with NULL as the nil sentinel. Scalar int?
10892+ # follows the same merge rule with SP_INT_NIL as its sentinel.
10893+ elsif base_type(old) == base_type(new_type) && (is_nullable_pointer_type(old) == 1 || is_scalar_nullable_type(old) == 1)
1089310894 if is_nullable_type(new_type) == 1 && is_nullable_type(old) == 0
1089410895 types[k] = new_type
1089510896 @cls_ivar_types[ci] = types.join(";")
@@ -13813,6 +13814,11 @@
1381313814 end
1381413815 end
1381513816 if at == "int"
13817+ # A literal int call-site is compatible with an existing int?
13818+ # slot; keep the sentinel-capable storage instead of widening.
13819+ if is_scalar_nullable_type(old_pt) == 1
13820+ return old_pt
13821+ end
1381613822 # Numeric compat: int + float is safe in both directions.
1381713823 if old_pt == "float"
1381813824 return "float"
@@ -16847,6 +16853,22 @@
1684716853 end
1684816854 end
1684916855
16856+ def synthetic_struct_class?(ci)
16857+ if ci < 0 || ci >= @cls_meth_names.length
16858+ return 0
16859+ end
16860+ names = @cls_meth_names[ci].split(";", -1)
16861+ bodies = @cls_meth_bodies[ci].split(";", -1)
16862+ k = 0
16863+ while k < names.length && k < bodies.length
16864+ if names[k] == "initialize" && bodies[k] == "-2"
16865+ return 1
16866+ end
16867+ k = k + 1
16868+ end
16869+ 0
16870+ end
16871+
1685016872 # Post-pass for #634 shape A. An optional param with an explicit
1685116873 # `= nil` default whose call sites then widen its type to a
1685216874 # Post-pass for #634 shape B. An ivar exposed by attr_reader /
@@ -16906,8 +16928,12 @@
1690616928 assigned = ivar_assigned_in_init_chain?(ci, iname)
1690716929 obs = cls_ivar_observed_types_for(ci, iname)
1690816930 if assigned == 0 && obs == ""
16909- types[k] = "poly"
16910- @needs_rb_value = 1
16931+ if pt == "int" && synthetic_struct_class?(ci) == 1
16932+ types[k] = "int?"
16933+ else
16934+ types[k] = "poly"
16935+ @needs_rb_value = 1
16936+ end
1691116937 changed_c = 1
1691216938 end
1691316939 end