Commit f2820e72

f2820e72b5b5Merged

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

Description

codegen(promote): box a nullable-int sentinel as nil, not a boxed INT_MIN

When an int? value (a hash miss, String#rindex, Integer#nonzero?, ...) flows into a poly slot, emit_boxed wrapped it with sp_box_int, producing a boxed integer holding SP_INT_NIL (INT_MIN) instead of Ruby nil. The value then printed/inspected as -9223372036854775808, and .nil? / equality against nil failed.

SP_INT_NIL is the reserved nullable-int sentinel and is never a legitimate integer, so boxing an int into poly must map it to nil. Add sp_box_int_or_nil (sentinel -> sp_box_nil, otherwise sp_box_int) and use it for the TY_INT case in emit_boxed.

Fixes promote-mode int? -> poly sentinel leak: int_opt_p_inspect, int_int_hash_missing_key_nil, str_int_hash_missing_key_nil, str_rindex_position, nil_guard_narrows_int_or_nil.

Default test 992/0/0 and optcarrot checksum 59662 unchanged.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
f2820e72b5b5
Parent
522af46cbfed

lib/sp_runtime.hmodifiedView file

@@ -2989,6 +2989,11 @@
29892989 static sp_RbVal sp_box_float(mrb_float v) { sp_RbVal r; r.tag = SP_TAG_FLT; r.cls_id = 0; r.v.f = v; return r; }
29902990 static sp_RbVal sp_box_bool(mrb_bool v) { sp_RbVal r; r.tag = SP_TAG_BOOL; r.cls_id = 0; r.v.b = v; return r; }
29912991 static sp_RbVal sp_box_nil(void) { sp_RbVal r; r.tag = SP_TAG_NIL; r.cls_id = 0; r.v.i = 0; return r; }
2992+/* Boxing a nullable-int value (int?): SP_INT_NIL is the reserved nil sentinel
2993+ and never a legitimate integer, so a sentinel must surface as Ruby nil rather
2994+ than a boxed INT_MIN. Used when an int? value (hash miss, rindex, nonzero?,
2995+ ...) flows into a poly slot. */
2996+static sp_RbVal sp_box_int_or_nil(mrb_int v) { return v == SP_INT_NIL ? sp_box_nil() : sp_box_int(v); }
29922997 static sp_RbVal sp_box_obj(void *p, int cls_id) { sp_RbVal r; r.tag = SP_TAG_OBJ; r.cls_id = cls_id; r.v.p = p; return r; }
29932998 static sp_RbVal sp_box_sym(sp_sym v) { sp_RbVal r; r.tag = SP_TAG_SYM; r.cls_id = 0; r.v.i = (mrb_int)v; return r; }
29942999 /* box a sp_Class into a poly slot. */