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

@@ -22216,6 +22216,23 @@
2221622216 if sr < 0 || infer_type(sr) != "string"
2221722217 return ""
2221822218 end
22219+ # Skip the single-char fast path when the `[]` arg is a Range
22220+ # (or anything other than an int). `s[i..j] == "/"` should
22221+ # dispatch through sp_str_sub_range and a string compare, not
22222+ # cast a RangeNode to mrb_int. Issue #644.
22223+ sub_args_id = @nd_arguments[recv]
22224+ if sub_args_id >= 0
22225+ sub_a = get_args(sub_args_id)
22226+ if sub_a.length > 0
22227+ idx_t_chk = infer_type(sub_a[0])
22228+ if idx_t_chk == "range" || sub_a.length >= 2
22229+ return ""
22230+ end
22231+ if sub_a.length == 1 && @nd_type[sub_a[0]] == "RangeNode"
22232+ return ""
22233+ end
22234+ end
22235+ end
2221922236 str_c = compile_expr(sr)
2222022237 idx_c = compile_arg0(recv)
2222122238 ch = lit