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?> != nilarm. Previously NilNode lowered to "0" and the comparison silently matched on a real-zero int? value — the canonical false positive ons.index(needle) == nilwhen 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_son 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
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); }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); }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); }/* int? siblings of the String#index family. Same not-found semantics as the _poly variants, but the result is the int? sentinel (SP_INT_NIL) rather than a boxed sp_RbVal — keeps the `i = s.index(sub); return ... if i.nil?; i + 1` idiom on the direct integer arithmetic path. */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; }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; }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; }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); }/* CRuby-compatible =~ wrapper: SP_TAG_INT(pos) on match, SP_TAG_NIL on miss. Codegen routes the `=~` operator here so 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. *//* Inspect / to_s for an int? value. CRuby distinguishes the two on nil: `nil.to_s` is "" while `nil.inspect` is "nil". For a real integer they agree (Integer#to_s and #inspect are both the decimal form). Two wrappers keep call-site emit local. */static const char *sp_int_opt_inspect(mrb_int v) { return sp_int_is_nil(v) ? "nil" : sp_int_to_s(v); }static const char *sp_int_opt_to_s(mrb_int v) { return sp_int_is_nil(v) ? "" : 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…