Commit 007f6c7f

007f6c7ff3f3Merged

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

Description

feat(runtime,analyze,codegen): scalar-nullable scaffolding for int?

Adds the foundation for sentinel-encoded nullable scalar types. The existing T? notation already drives nullable pointer types via NULL (is_nullable_pointer_type); scalars need a different encoding because NULL has no analogue on a bit pattern that's also a valid value.

  • Runtime: SP_INT_NIL = INT64_MIN, with sp_int_is_nil(v) macro. INT64_MIN was chosen because Ruby auto-promotes 2**63 to Bignum (the #597 limitation), so the reserved value falls outside the range spinel already declines to handle on the fast path. Future siblings: SP_FLOAT_NIL (likely struct-based, NaN-tagging deferred), SP_SYM_NIL (sym_id == 0), bool? (3-valued enum). Documented at the typedef site.

  • Analyze + codegen: is_scalar_nullable_type(t) returns 1 for int? (today) and is the call-site test that separates "this nullable uses NULL" from "this nullable uses a sentinel." Keeps is_nullable_pointer_type's existing list untouched so already- landed callers that branch on it continue to mean "NULL-based."

No call sites consume int? yet. This is the scaffolding the next commit (Array#index returning int? instead of poly-widened sp_RbVal) builds on. 582 tests still pass.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
007f6c7ff3f3
Parent
8bcde2a736cf

lib/sp_runtime.hmodifiedView file

@@ -63,6 +63,16 @@
6363 typedef int64_t mrb_int;
6464 typedef double mrb_float;
6565 typedef bool mrb_bool;
66+
67+/* Sentinel value reserved by the int? (scalar-nullable int) type. An
68+ int? slot is bit-compatible with mrb_int; SP_INT_NIL marks the
69+ "nil" inhabitant. The chosen pattern is INT64_MIN, which Ruby's
70+ Integer would auto-promote to Bignum (#597 limitation), so the
71+ reservation lines up with spinel's existing fast-path-only spec.
72+ `sp_int_is_nil(v)` is the canonical predicate; treat any int? value
73+ produced by runtime helpers as opaque outside this macro. */
74+#define SP_INT_NIL ((mrb_int)INT64_MIN)
75+#define sp_int_is_nil(v) ((v) == SP_INT_NIL)
6676 /* sp_sym is defined per-program in emit_sym_runtime, but poly helpers
6777 below need to reference it by forward declaration. */
6878 typedef mrb_int sp_sym;