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?> + 1without 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
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); }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); }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); }/* int? siblings of the *_index_poly wrappers above. Same not-found semantics, but return the int? sentinel (SP_INT_NIL) instead of boxing into sp_RbVal. Used when the call site's static type tracking carries the result as int? rather than poly — eliminates the box/unbox round-trip for the common `i = arr.index(x); i.nil? ? ... : <use i as int>` idiom. */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; }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; }/* Inspect/to_s for an int? value. CRuby renders the nil case as "nil" and the integer case via Integer#to_s. sp_int_to_s already handles the latter; the wrapper only adds the nil short-circuit. */static const char *sp_int_opt_inspect(mrb_int v) { return sp_int_is_nil(v) ? "nil" : sp_int_to_s(v); }/* sp_Range is a 16-byte value type that doesn't fit in sp_RbVal's union (max 8 bytes). When a Range crosses into a poly slot (heterogeneous hash / array / param / ivar), copy it onto the GC heap and box thespinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…