Commit 0bc1d675

0bc1d675ac21Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · +1 added · ~1 modified

Description

Fix String#slice(Range) — decompose Range instead of passing sp_range_new

A single Range argument to String#slice fell through to the single-index arm, where compile_arg0_as_int lowered the RangeNode to sp_range_new(first, last) and passed that struct where sp_str_sub_range expects an mrb_int start — a hard incompatible-type C compile error.

Detect a lone RangeNode argument and decompose it the same way String# does, dispatching to sp_str_sub_range_r with the normalized (start, end, excl) triple. Endless/beginless/negative endpoints and the exclusive flag all flow through correctly.

Closes #1085

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
0bc1d675ac21
Parent
bbec0b77db32

spinel_codegen.rbmodifiedView file

@@ -21702,6 +21702,32 @@
2170221702 args_id = @nd_arguments[nid]
2170321703 if args_id >= 0
2170421704 a = get_args(args_id)
21705+ # s.slice(1..3) / s.slice(1...3) — decompose the Range and
21706+ # dispatch to the same `_r` runtime helper that s[1..3] uses,
21707+ # which normalizes negative endpoints and the exclusive flag.
21708+ # Without this arm the single Range arg fell through to
21709+ # compile_arg0_as_int below, emitting sp_range_new(...) where
21710+ # an mrb_int start was expected -> incompatible-type C error.
21711+ # Endless/beginless ranges mirror the s[..] handling: a missing
21712+ # start becomes 0 and a missing end becomes -1 (inclusive to end).
21713+ if a.length == 1 && @nd_type[a[0]] == "RangeNode"
21714+ range_node_sl = a[0]
21715+ left_nid_sl = @nd_left[range_node_sl]
21716+ right_nid_sl = @nd_right[range_node_sl]
21717+ if left_nid_sl < 0
21718+ left_sl = "0"
21719+ else
21720+ left_sl = compile_expr_as_int(left_nid_sl)
21721+ end
21722+ if right_nid_sl < 0
21723+ right_sl = "-1"
21724+ excl_sl = "0"
21725+ else
21726+ right_sl = compile_expr_as_int(right_nid_sl)
21727+ excl_sl = (range_excl_end(range_node_sl) == 1 ? "1" : "0")
21728+ end
21729+ return "sp_str_sub_range_r(" + rc + ", " + left_sl + ", " + right_sl + ", " + excl_sl + ")"
21730+ end
2170521731 if a.length >= 2
2170621732 return "sp_str_sub_range(" + rc + ", " + compile_expr_as_int(a[0]) + ", " + compile_expr_as_int(a[1]) + ")"
2170721733 end