Commit 7caf9b6d

7caf9b6db62eMerged

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

Description

fix(codegen): endless/beginless range String#[] returns substring not ""

Sam Ruby's #543: ":id"[1..] returned "" instead of "id". The Ruby 2.6+ endless-range shorthand is the idiomatic way to "take from index N to end of string"; spinel lowered the missing right endpoint of the RangeNode to the literal 0, producing a zero-length slice silently.

Roundhouse's router uses pp[1..] to strip the leading : from :id pattern segments. Pre-fix every /articles/N request 404'd because the params capture key was always "" (falling back to the params.fetch("id", "0") default).

Fix in the string-[] RangeNode arm: when the RangeNode's @nd_left is -1 (no value, beginless), emit 0 as the lower bound; when @nd_right is -1 (endless), emit -1 as the upper bound and force the exclusive flag to 0. CRuby treats s[1..] and s[1...] as the same s[1..-1] -- the exclusive flag on the missing endpoint is meaningless.

Variants verified (test/string_endless_range.rb):

  • s[1..] -> "ello"
  • s[1...] -> "ello"
  • s[-2..] -> "lo"
  • s[..2] -> "hel"
  • s[...2] -> "he"
  • ":id"[1..] -> "id"

Note: s[..] (both ends nil) doesn't parse under prism's current grammar; if it did, the same arm would handle it as s[0..-1].

Closes #543

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
7caf9b6db62e
Parent
7243161f669d

spinel_codegen.rbmodifiedView file

@@ -14983,9 +14983,33 @@
1498314983 # length first. Issue #496: the prior `right - left + 1`
1498414984 # formula silently produced a negative length for `s[1..-2]`
1498514985 # and returned "".
14986- left = compile_expr(@nd_left[a[0]])
14987- right = compile_expr(@nd_right[a[0]])
14988- return (use_len ? "sp_str_sub_range_len_r" : "sp_str_sub_range_r") + "(" + lprefix + ", " + left + ", " + right + ", " + (range_excl_end(a[0]) == 1 ? "1" : "0") + ")"
14986+ #
14987+ # Endless / beginless range support (Ruby 2.6+). `s[1..]`
14988+ # parses as RangeNode { left: 1, right: -1 (nil) }; CRuby
14989+ # treats it as `s[1..-1]` (inclusive to end). `s[..5]`
14990+ # mirrors with `left: -1, right: 5` and is equivalent to
14991+ # `s[0..5]`. The exclusive flag on the missing endpoint is
14992+ # ignored in CRuby (`s[1..]` == `s[1...]` == `s[1..-1]`).
14993+ # Issue #543. Without these arms, `compile_expr(-1)` lowered
14994+ # the missing endpoint to literal `0`, so `s[1..]` became
14995+ # `sp_str_sub_range_r(s, 1, 0, 0)` -- length-zero slice,
14996+ # silent empty string.
14997+ range_node_543 = a[0]
14998+ left_nid_543 = @nd_left[range_node_543]
14999+ right_nid_543 = @nd_right[range_node_543]
15000+ if left_nid_543 < 0
15001+ left = "0"
15002+ else
15003+ left = compile_expr(left_nid_543)
15004+ end
15005+ if right_nid_543 < 0
15006+ right = "-1"
15007+ excl_543 = "0"
15008+ else
15009+ right = compile_expr(right_nid_543)
15010+ excl_543 = (range_excl_end(range_node_543) == 1 ? "1" : "0")
15011+ end
15012+ return (use_len ? "sp_str_sub_range_len_r" : "sp_str_sub_range_r") + "(" + lprefix + ", " + left + ", " + right + ", " + excl_543 + ")"
1498915013 end
1499015014 if a.length >= 2
1499115015 # s[0, 2]