Commit cd9733e5
cd9733e51230Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
fix(analyze,codegen,runtime): Array#index returns nil for not-found
spinel positions itself as a Ruby SUBSET, so documented Ruby
APIs must match CRuby behavior. Array#index(x) /
#find_index(x) / #rindex(x) in CRuby return Integer | nil,
with nil for not-found. spinel previously returned the raw -1
sentinel from the underlying sp_*Array_index helpers, which
broke arr.index(x).nil? / arr.index(x) == nil checks --
both always false in spinel, regardless of whether the item
was actually present.
- runtime: add
sp_IntArray_index_poly/_rindex_polyandsp_StrArray_index_poly/_rindex_polywrappers that box nil for not-found, int for found. Sibling to the existingsp_str_index_polyfor String#index (#532). The raw_indexhelpers keep the -1 return for internal callers that need it (none exist in spinel-internal today, but the helpers stay available for future emit paths). - codegen: route Array#index / #find_index / #rindex through
the
_polywrappers for int_array and str_array recvs. - analyze:
infer_method_name_typefor these methods now returns "poly" for array recvs (was "int" / -1-sentinel).
Side-effect cleanup: the @unified_imeth_returns.index(tag) != nil consumer at infer_all_returns line ~16763 was relying on
spinel's pre-fix .index returns -1 behavior to always-fire
(preserving all concrete return types across re-derivation).
With Array#index now CRuby-correct, that predicate becomes
genuine "is in the marker list" -- which is the original
intent of #563's preservation. Switched to .include? for
explicit bool semantics. No test regression and self-host
bootstrap stays clean.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- cd9733e51230
- Parent
- 517121c02621
lib/sp_runtime.hmodifiedView file
static sp_RbVal sp_box_sym_array(void *p) { return sp_box_obj(p, SP_BUILTIN_SYM_ARRAY); }static sp_RbVal sp_box_ptr_array(void *p) { return sp_box_obj(p, SP_BUILTIN_PTR_ARRAY); }static sp_RbVal sp_box_proc(void *p) { return sp_box_obj(p, SP_BUILTIN_PROC); }/* CRuby-compatible Array#index / #rindex / #find_index: returns sp_RbVal (nil tag for not-found, int tag with the position when found). spinel's raw `_index` helpers return the -1 sentinel, which diverges from CRuby's nil. Codegen routes `arr.index(x)` / `arr.find_index(x)` / `arr.rindex(x)` through these `_poly` wrappers so `.nil?` / `== nil` / `inspect` etc. on the result behave the CRuby way. Sibling to `sp_str_index_poly` above; same widening rationale. Issue raised during #585 follow-up: spinel positions itself as a Ruby SUBSET, so documented Ruby APIs must match CRuby behavior. */static sp_RbVal sp_IntArray_index_poly(sp_IntArray *a, mrb_int v) { mrb_int n = sp_IntArray_index(a, v); return n < 0 ? sp_box_nil() : sp_box_int(n); }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); }/* 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…
test/array_index_nil_for_not_found.rbaddedView file
Loading diff…
test/array_index_nil_for_not_found.rb.expectedaddedView file
Loading diff…