Commit 9191dec7
9191dec7118dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): String#[Range] skips single-char eq fast path
s[i..i] == "/" was caught by compile_eq_char_index's single-char
fast path, which casts the [] arg to mrb_int and emits a direct
C array lookup against a char literal. With a Range arg this
produced lv_s[(mrb_int)sp_range_new(i, i)] == '/' — "aggregate
value used where an integer was expected" at C compile time.
Add a guard: bail out of the fast path when the [] arg is a
RangeNode literal, a node whose inferred type is range, or a
2-arg s[start, len] form. Lets the call fall through to the
proper sp_str_sub_range dispatch + string compare.
Test: test/str_index_range_eq.rb covers Range LV, Range literal, exclusive range, and 2-arg [].
Closes #644.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 9191dec7118d
- Parent
- 756c6c3fde33
spinel_codegen.rbmodifiedView file
if sr < 0 || infer_type(sr) != "string" return "" end # Skip the single-char fast path when the `[]` arg is a Range # (or anything other than an int). `s[i..j] == "/"` should # dispatch through sp_str_sub_range and a string compare, not # cast a RangeNode to mrb_int. Issue #644. sub_args_id = @nd_arguments[recv] if sub_args_id >= 0 sub_a = get_args(sub_args_id) if sub_a.length > 0 idx_t_chk = infer_type(sub_a[0]) if idx_t_chk == "range" || sub_a.length >= 2 return "" end if sub_a.length == 1 && @nd_type[sub_a[0]] == "RangeNode" return "" end end end str_c = compile_expr(sr) idx_c = compile_arg0(recv) ch = littest/str_index_range_eq.rbaddedView file
Loading diff…
test/str_index_range_eq.txtaddedView file
Loading diff…