Commit 1aeb8559
1aeb8559dc2fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): String#rindex(str) returns int? to enable nil-narrow
s.rindex(needle) with a plain string arg used to widen to
"poly" via sp_str_rindex_poly (sp_RbVal return). The subsequent
nil-check ternary couldn't narrow the slot (the b3f12b3 narrow
infra only fires when the slot is nullable, not poly), so
slash + 1 flowed through sp_poly_add and the result couldn't
pass as the mrb_int arg to sp_str_sub_range_r.
Split the rindex dispatch like String#index:
- analyze: when the arg is a regex, return "poly"; otherwise return "int?".
- codegen: regex arg routes through sp_re_rindex_poly (returns sp_RbVal); string arg routes through sp_str_rindex_opt (returns mrb_int with SP_INT_NIL for not-found).
String#rindex_opt already exists in lib/sp_runtime.h; this just
flips the default plain-string dispatcher to use it. Pairs with
the existing compile_if_expr nil-check narrow (b3f12b3): with
slash now int?, the else arm of slash == nil ? T : F sees
slash narrowed to int, the .v.i unbox stays a no-op (already
mrb_int), and slash + 1 lowers cleanly.
Closes #645.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 1aeb8559dc2f
- Parent
- 92306b7fe57b
spinel_analyze.rbmodifiedView file
# `sp_*Array_index_poly` runtime wrappers. if recv >= 0 rt_idx = infer_type(recv) # String#index uses the int? sentinel path (mname == "index" # only; rindex still hits the regex-vs-string overload and # stays on poly for a follow-up). String#rindex stays on poly # because the regex-receiver branch of compile-side dispatch # routes through sp_re_rindex_poly which can't fit the int? # encoding cleanly yet. if (rt_idx == "string" || rt_idx == "mutable_str") && mname == "index" return "int?" end # String#index / String#rindex with a plain string arg use the # int? sentinel path (SP_INT_NIL for not-found) — the codegen # routes through sp_str_index_opt / sp_str_rindex_opt. With a # regex arg, the dispatch picks sp_re_*index_poly which returns # sp_RbVal, so the static return stays "poly". if rt_idx == "string" || rt_idx == "mutable_str" @needs_rb_value = 1 return "poly" arg_t_idx_645 = "string" arg_ids_idx_645 = [] if @nd_arguments[nid] >= 0 arg_ids_idx_645 = get_args(@nd_arguments[nid]) end if arg_ids_idx_645.length > 0 && @nd_type[arg_ids_idx_645[0]] == "RegularExpressionNode" arg_t_idx_645 = "regex" end if arg_t_idx_645 == "regex" @needs_rb_value = 1 return "poly" end return "int?" end # int_array uses the int? sentinel path (returns mrb_int with # SP_INT_NIL marking not-found). str_array still goes throughspinel_codegen.rbmodifiedView file
Loading diff…
test/str_rindex_optional_narrow.rbaddedView file
Loading diff…
test/str_rindex_optional_narrow.rb.expectedaddedView file
Loading diff…