Commit 2de162f2
2de162f2b49aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Document the 32-bit int? / SP_INT_NIL collision limitation
Per the marker audit: the int? nil sentinel is a single reserved bit pattern (INTPTR_MIN), so a genuine integer equal to it is indistinguishable from nil. Harmless on 64-bit (INT64_MIN unreachable in practice) but on 32-bit a real -2147483648 in an int? slot reads as nil. Document the scope (int? slots only; plain ints unaffected), why the overflow helpers don't reserve the value (hot-path cost vs the embedded-build goal), and the workaround (box it as poly). No code change.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 2de162f2b49a
- Parent
- f4783256b339
lib/sp_types.hmodifiedView file
@@ -37,12 +37,24 @@
3737
/* Sentinel value reserved by the int? (scalar-nullable int) type. An3838
int? slot is bit-compatible with mrb_int; SP_INT_NIL marks the3939
"nil" inhabitant. The pattern is INTPTR_MIN -- INT64_MIN on 64-bit40-
(unchanged), INT32_MIN on 32-bit. On 64-bit this value would auto-41-
promote to Bignum in CRuby so the reservation is safe; on 32-bit42-
INT32_MIN is a representable Integer, so the sentinel can collide43-
with a genuine -2147483648 (an accepted 32-bit-build limitation).40+
(unchanged), INT32_MIN on 32-bit.4441
`sp_int_is_nil(v)` is the canonical predicate; treat any int? value45-
produced by runtime helpers as opaque outside this macro. */42+
produced by runtime helpers as opaque outside this macro.43+
44+
KNOWN LIMITATION (32-bit builds only). The reservation is a single45+
bit pattern, so a *genuine* integer equal to the sentinel is46+
indistinguishable from nil. On 64-bit, INT64_MIN is effectively47+
unreachable in practice (CRuby would have promoted it to Bignum), so48+
this never bites. On 32-bit, INT32_MIN (-2147483648) is an ordinary49+
reachable Integer, so a real -2147483648 flowing into an int? slot50+
reads back as nil -- e.g. `[-2147483648].pop` yields nil instead of51+
the value. This affects ONLY int? (nullable-int) slots; a plain52+
(non-nullable) int holding -2147483648 is fine, since it never53+
consults sp_int_is_nil. The integer-overflow helpers deliberately do54+
NOT reserve this value (checking every +/-/* result against it would55+
cost the hot path the embedded build is trying to save). Code that56+
must store -2147483648 nullably on 32-bit should box it (poly) rather57+
than use a flat int? slot. */4658
#define SP_INT_NIL ((mrb_int)INTPTR_MIN)4759
#define sp_int_is_nil(v) ((v) == SP_INT_NIL)4860