Commit 25d4bb47

25d4bb471a10Merged

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

Description

feat(runtime,analyze,codegen): String#index returns int?

Mirrors the prior commit's Array#index slice, applied to the String#index call surface. Replaces the sp_str_index_poly / sp_str_index_from_poly emit with sp_str_index_opt / sp_str_index_from_opt — both return mrb_int with SP_INT_NIL on not-found instead of the boxed sp_RbVal shape.

Knock-on fixes the slice surfaced:

  • compile_eq learned an <int?> == nil / <int?> != nil arm. Previously NilNode lowered to "0" and the comparison silently matched on a real-zero int? value — the canonical false positive on s.index(needle) == nil when needle sits at position 0. Routes through sp_int_is_nil instead.

  • int?-recv dispatch in compile_call_expr now splits .to_s and .inspect into separate arms. CRuby's nil.to_s is "" but nil.inspect is "nil"; the shared sp_int_opt_inspect path was printing "nil" for both, breaking the walk-all-dots / walk-all-tokens idioms that close with puts pos.to_s on a not-found terminator.

  • sp_int_opt_to_s landed in the runtime alongside the existing sp_int_opt_inspect.

  • Analyzer's index/find_index/rindex inference splits string vs string-rindex: index goes int?, rindex stays poly because the regex-receiver dispatch (sp_re_rindex_poly) still uses the boxed shape.

Tests: 583 pass / 0 fail. Bootstrap fixpoint clean. The existing test/string_index_nullable.rb and nil_guard_narrows_int_or_nil.rb stay byte-identical to their .expected — they cover the `break if pos.nil?` / pos == nil / pos != nil / pos.to_s / pos.to_i + 1 shapes that the new arms had to absorb.

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

Details

Commit
25d4bb471a10
Parent
2fad57854031

lib/sp_runtime.hmodifiedView file

@@ -1467,6 +1467,14 @@
14671467 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); }
14681468 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); }
14691469 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); }
1470+/* int? siblings of the String#index family. Same not-found
1471+ semantics as the _poly variants, but the result is the int?
1472+ sentinel (SP_INT_NIL) rather than a boxed sp_RbVal — keeps the
1473+ `i = s.index(sub); return ... if i.nil?; i + 1` idiom on the
1474+ direct integer arithmetic path. */
1475+static mrb_int sp_str_index_opt(const char *s, const char *sub) { mrb_int n = sp_str_index(s, sub); return n < 0 ? SP_INT_NIL : n; }
1476+static mrb_int sp_str_index_from_opt(const char *s, const char *sub, mrb_int start) { mrb_int n = sp_str_index_from(s, sub, start); return n < 0 ? SP_INT_NIL : n; }
1477+static mrb_int sp_str_rindex_opt(const char *s, const char *sub) { mrb_int n = sp_str_rindex(s, sub); return n < 0 ? SP_INT_NIL : n; }
14701478 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); }
14711479 /* CRuby-compatible =~ wrapper: SP_TAG_INT(pos) on match, SP_TAG_NIL
14721480 on miss. Codegen routes the `=~` operator here so
@@ -1555,10 +1563,12 @@
15551563 i.nil? ? ... : <use i as int>` idiom. */
15561564 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; }
15571565 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. */
1566+/* Inspect / to_s for an int? value. CRuby distinguishes the two on
1567+ nil: `nil.to_s` is "" while `nil.inspect` is "nil". For a real
1568+ integer they agree (Integer#to_s and #inspect are both the decimal
1569+ form). Two wrappers keep call-site emit local. */
15611570 static const char *sp_int_opt_inspect(mrb_int v) { return sp_int_is_nil(v) ? "nil" : sp_int_to_s(v); }
1571+static const char *sp_int_opt_to_s(mrb_int v) { return sp_int_is_nil(v) ? "" : sp_int_to_s(v); }
15621572 /* sp_Range is a 16-byte value type that doesn't fit in sp_RbVal's union
15631573 (max 8 bytes). When a Range crosses into a poly slot (heterogeneous
15641574 hash / array / param / ivar), copy it onto the GC heap and box the