Commit 880d7270

880d7270f2c0Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 9 files · +2 added · ~7 modified

Description

fix(analyze,codegen,runtime): String#index returns nil for not-found

String#index / String#rindex previously returned mrb_int with -1 as the not-found sentinel. Per #521's strict int == nil -> FALSE, the CRuby idiom

pos = body.index('"content"', i)
break if pos.nil?

never exited the loop because pos.nil? on a plain-int local is always false. Ori Pekelman hit this writing a GPT-2 BPE token scanner that walks JSON content fields; the result was an infinite loop wandering past the buffer and Bus-erroring.

This change widens String#index / String#rindex's return to sp_RbVal -- boxed nil for the -1 sentinel, boxed int for found. CRuby semantics now work end-to-end:

  • pos.nil? / pos == nil / pos != nil via the standard poly-tag dispatch.
  • pos.inspect via sp_poly_inspect (prints "nil" or the int).
  • while pos = s.index(...) truthiness (boxed nil is falsy).

Implementation:

  • lib/sp_runtime.h: new sp_str_index_poly / sp_str_index_from_poly / sp_str_rindex_poly / sp_re_rindex_poly wrappers over the existing mrb_int-returning helpers, boxing the result.
  • spinel_codegen.rb: compile_str_method_expr's index / rindex arms emit the _poly variants; compile_poly_method_call gains an inspect arm via sp_poly_inspect; new helper compile_expr_as_int(nid) unboxes via .v.i when the static type is poly, used at the int-expecting C helper boundaries (sp_str_sub_range, the string [] two-arg form, sp_str_index_from's start arg).
  • spinel_analyze.rb: String#index / #rindex infer as "poly" when the receiver is string / mutable_str. Array#index / #find_index intentionally keep the int return for now -- the same structural fix applies but #532's reproduction surface is strings, and widening Array#index cascades through more call sites that consume the result as a raw index.

Test fixture updates:

  • test/string_index_from.rb: rewritten to the new idiom -- the walk-all-dots loop now uses break if pos.nil? instead of pos < 0, and pos.to_i + 1 unbox at the int-arithmetic site. Expected file: the four "-1" lines that documented the prior broken behavior become empty lines (puts nil.to_s -> "").
  • test/str_method_nil_arg_no_segv.rb.expected: -1 line becomes nil for p "foo".rindex(/missing/). The test's inline comment already documented # CRuby: nil. spinel: -1; the spinel output now matches CRuby.
  • test/string_index_nullable.rb (new): exercises the full nil-lens shape -- found/not-found .inspect, .nil?, the walk-all loop pattern from the issue.

This is option 1 from the #532 design comment; the user explicitly accepted the boxing/unbox cost ("trust future-me to handle nullable well"). The compile_expr_as_int helper handles the bootstrap's own name.index("_cls_") shape and the test fixtures' integer-index uses; future widenings (Array#index, etc.) can extend the same pattern.

Closes #532

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

Details

Commit
880d7270f2c0
Parent
c3e7e0a8d3ff

lib/sp_runtime.hmodifiedView file

@@ -1202,6 +1202,18 @@
12021202 when non-NULL. */
12031203 static inline mrb_int sp_obj_cls_id_of(void *p) { return p ? *(mrb_int *)p : 0; }
12041204 static sp_RbVal sp_box_nullable_str(const char *v) { return v ? sp_box_str(v) : sp_box_nil(); }
1205+/* String#index / #rindex return a boxed nil for not-found, boxed
1206+ int for found. Issue #532: typed-int slot can't represent CRuby's
1207+ nil-vs-real-index distinction in-band; widening the result type
1208+ to sp_RbVal at the call site lets `pos.nil?` and `puts pos.inspect`
1209+ work via the standard poly-tag dispatch. The -1 sentinel comes
1210+ from the underlying sp_str_*_index helpers; we widen here at the
1211+ boxing layer so existing call sites that want the raw int still
1212+ work via `sp_str_index` directly. */
1213+static sp_RbVal sp_str_index_poly(const char *s, const char *sub) { mrb_int n = sp_str_index(s, sub); return n < 0 ? sp_box_nil() : sp_box_int(n); }
1214+static sp_RbVal sp_str_index_from_poly(const char *s, const char *sub, mrb_int start) { mrb_int n = sp_str_index_from(s, sub, start); return n < 0 ? sp_box_nil() : sp_box_int(n); }
1215+static sp_RbVal sp_str_rindex_poly(const char *s, const char *sub) { mrb_int n = sp_str_rindex(s, sub); return n < 0 ? sp_box_nil() : sp_box_int(n); }
1216+static sp_RbVal sp_re_rindex_poly(mrb_regexp_pattern *pat, const char *str) { mrb_int n = sp_re_rindex(pat, str); return n < 0 ? sp_box_nil() : sp_box_int(n); }
12051217 static sp_RbVal sp_box_nullable_obj(void *p, int cls_id) { return p ? sp_box_obj(p, cls_id) : sp_box_nil(); }
12061218 /* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negative
12071219 cls_id so the dispatch path is uniform. */