Commit 6f920fb2

6f920fb2551cMerged

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

Description

feat(runtime,analyze,codegen): Array#index on int_array returns int?

First vertical slice on the scalar-nullable design from the previous scaffolding commit. Switches Array#index / find_index / rindex on a int_array receiver from poly-widening (sp_RbVal with boxed nil or boxed int) to int?-sentinel (mrb_int with SP_INT_NIL for not-found). Same Ruby-level semantics — arr.index(x).nil? still works, puts arr.index(x).inspect still prints "nil" — but the static type survives as int? so a follow-up nil-guard narrow lets r + 1 after return -1 if r.nil? compile to direct integer arithmetic without the poly round-trip the boxed shape currently forces.

Pieces that landed:

  • Runtime: sp_IntArray_index_opt / sp_IntArray_rindex_opt return mrb_int with SP_INT_NIL on miss; sp_int_opt_inspect prints "nil" or sp_int_to_s(v). The existing _poly wrappers stay for callers on the poly path (notably String#index and str_array#index, both of which keep the poly return until their respective sentinel encodings are settled).

  • Analyze: Array#index on int_array returns "int?". str_array stays "poly" — splitting the previous shared branch.

  • Codegen: the is_nullable_type(recv_type) strip-? pass at the top of compile_call_expr now skips scalar-nullable types so the int? dispatch arms (added immediately after the int arm) actually see "int?". The new arm handles inspect / to_s via sp_int_opt_inspect and falls through to the int dispatch for arithmetic / queries (sentinel-leak on <int?> + 1 without an enclosing nil-guard is the documented spec until the narrow path lands).

  • Codegen: c_default_val("int?") returns SP_INT_NIL instead of NULL (which would have produced mrb_int lv_r = NULL;, a hard type warning).

  • .nil? on a scalar-nullable receiver now compiles to sp_int_is_nil before falling through to the NULL-pointer branch (which would have meant "== 0" — silently wrong for the sentinel).

Tests: 582 pass / 0 fail / 0 error. Bootstrap fixpoint clean. Existing array_index_nil_for_not_found.rb and nil_guard_narrows_int_or_nil.rb stay byte-identical to their .expected — the latter even exercises the chain s.index(needle); return -1 if h.nil?; h + 1 which still works because String#index keeps the poly return for now (narrow happens through the existing poly→int path). The Array#index narrow path on int? is the next commit.

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

Details

Commit
6f920fb2551c
Parent
007f6c7ff3f3

lib/sp_runtime.hmodifiedView file

@@ -1547,6 +1547,18 @@
15471547 static sp_RbVal sp_IntArray_rindex_poly(sp_IntArray *a, mrb_int v) { mrb_int n = sp_IntArray_rindex(a, v); return n < 0 ? sp_box_nil() : sp_box_int(n); }
15481548 static sp_RbVal sp_StrArray_index_poly(sp_StrArray *a, const char *v) { mrb_int n = sp_StrArray_index(a, v); return n < 0 ? sp_box_nil() : sp_box_int(n); }
15491549 static sp_RbVal sp_StrArray_rindex_poly(sp_StrArray *a, const char *v) { mrb_int n = sp_StrArray_rindex(a, v); return n < 0 ? sp_box_nil() : sp_box_int(n); }
1550+/* int? siblings of the *_index_poly wrappers above. Same not-found
1551+ semantics, but return the int? sentinel (SP_INT_NIL) instead of
1552+ boxing into sp_RbVal. Used when the call site's static type
1553+ tracking carries the result as int? rather than poly — eliminates
1554+ the box/unbox round-trip for the common `i = arr.index(x);
1555+ i.nil? ? ... : <use i as int>` idiom. */
1556+static mrb_int sp_IntArray_index_opt(sp_IntArray *a, mrb_int v) { mrb_int n = sp_IntArray_index(a, v); return n < 0 ? SP_INT_NIL : n; }
1557+static mrb_int sp_IntArray_rindex_opt(sp_IntArray *a, mrb_int v) { mrb_int n = sp_IntArray_rindex(a, v); return n < 0 ? SP_INT_NIL : n; }
1558+/* Inspect/to_s for an int? value. CRuby renders the nil case as
1559+ "nil" and the integer case via Integer#to_s. sp_int_to_s already
1560+ handles the latter; the wrapper only adds the nil short-circuit. */
1561+static const char *sp_int_opt_inspect(mrb_int v) { return sp_int_is_nil(v) ? "nil" : sp_int_to_s(v); }
15501562 /* sp_Range is a 16-byte value type that doesn't fit in sp_RbVal's union
15511563 (max 8 bytes). When a Range crosses into a poly slot (heterogeneous
15521564 hash / array / param / ivar), copy it onto the GC heap and box the