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_poly and sp_StrArray_index_poly / _rindex_poly wrappers that box nil for not-found, int for found. Sibling to the existing sp_str_index_poly for String#index (#532). The raw _index helpers 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 _poly wrappers for int_array and str_array recvs.
  • analyze: infer_method_name_type for 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

@@ -1374,6 +1374,22 @@
13741374 static sp_RbVal sp_box_sym_array(void *p) { return sp_box_obj(p, SP_BUILTIN_SYM_ARRAY); }
13751375 static sp_RbVal sp_box_ptr_array(void *p) { return sp_box_obj(p, SP_BUILTIN_PTR_ARRAY); }
13761376 static sp_RbVal sp_box_proc(void *p) { return sp_box_obj(p, SP_BUILTIN_PROC); }
1377+
1378+/* CRuby-compatible Array#index / #rindex / #find_index: returns
1379+ sp_RbVal (nil tag for not-found, int tag with the position when
1380+ found). spinel's raw `_index` helpers return the -1 sentinel,
1381+ which diverges from CRuby's nil. Codegen routes
1382+ `arr.index(x)` / `arr.find_index(x)` / `arr.rindex(x)` through
1383+ these `_poly` wrappers so `.nil?` / `== nil` / `inspect` etc.
1384+ on the result behave the CRuby way. Sibling to
1385+ `sp_str_index_poly` above; same widening rationale.
1386+ Issue raised during #585 follow-up: spinel positions itself
1387+ as a Ruby SUBSET, so documented Ruby APIs must match CRuby
1388+ behavior. */
1389+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); }
1390+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); }
1391+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); }
1392+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); }
13771393 /* sp_Range is a 16-byte value type that doesn't fit in sp_RbVal's union
13781394 (max 8 bytes). When a Range crosses into a poly slot (heterogeneous
13791395 hash / array / param / ivar), copy it onto the GC heap and box the