From 2c2ac58e1519c35f93526c8967138dda0239364e Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 24 Aug 2026 17:59:18 +0200 Subject: [PATCH] Symbol#[]: run the full String#[] protocol MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Symbol#[] / #slice only handled an Integer index, an index and length, a Range or a substring — a Regexp argument raised "no implicit conversion of Regexp into Integer", so none of the Regexp forms (with or without a capture index, and the $~ they set) worked. They now delegate to String#[] against the symbol's name, so the whole protocol — including the Regexp and capture-group forms that set $~ — works and yields a String (or nil), exactly like sym.to_s[...]. The tiny stringIndex wrapper that only backed the old Symbol#[] is removed. Verified against ruby 4.0.6; core/symbol/element_reference_spec 35 -> 44. (The remaining errors are the Float / Range-subclass index coercion that String#[] itself does not yet do — a shared follow-up.) Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 18 +++++++----- internal/vm/symbol_index_internal_test.go | 36 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 internal/vm/symbol_index_internal_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 5e22ed64..76bec168 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1401,11 +1401,8 @@ func (vm *VM) bootstrap() { } vm.cSymbol.define("succ", symSucc) vm.cSymbol.define("next", symSucc) - symIndex := func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return stringIndex(symStr(self), args) // [] / slice yield a String, like MRI - } - vm.cSymbol.define("[]", symIndex) - vm.cSymbol.define("slice", symIndex) + // Symbol#[] / #slice run the whole String#[] protocol against the symbol's + // name (they are registered after strIndexFn is defined, below). vm.cSymbol.define("start_with?", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { s := symStr(self) for _, a := range args { @@ -1900,6 +1897,14 @@ func (vm *VM) bootstrap() { return res } vm.cString.define("[]", strIndexFn) + // Symbol#[] / #slice yield a String by running the full String#[] protocol + // (Integer/Range/String/Regexp with capture groups, setting $~) against the + // symbol's name, exactly like sym.to_s[...]. + symIndexFn := func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + return strIndexFn(vm, object.NewString(symStr(self)), args, blk) + } + vm.cSymbol.define("[]", symIndexFn) + vm.cSymbol.define("slice", symIndexFn) // #slice is a true alias of #[] (shares the exact method record). aliasBuiltin(vm.cString, "slice", "[]") vm.cString.define("ord", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { @@ -5524,9 +5529,6 @@ func parseLeadingFloat(s string) float64 { // stringIndex implements String#[]: s[i], s[i, len], and s[range], all // rune-indexed, returning nil for an out-of-range start. -func stringIndex(s string, args []object.Value) object.Value { - return stringIndexEnc(s, args, false) -} // stringIndexEnc implements String#[] / #slice indexing. When binary is true the // receiver is ASCII-8BIT, so the integer index/length forms count BYTES (not diff --git a/internal/vm/symbol_index_internal_test.go b/internal/vm/symbol_index_internal_test.go new file mode 100644 index 00000000..b40d98f3 --- /dev/null +++ b/internal/vm/symbol_index_internal_test.go @@ -0,0 +1,36 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestSymbolIndex covers Symbol#[] / Symbol#slice running the full String#[] +// protocol against the symbol's name: an Integer index, an index and length, a +// Range, a substring, and a Regexp (with and without a capture index, setting +// $~), each yielding a String or nil. Verified against ruby 4.0.6. +func TestSymbolIndex(t *testing.T) { + cases := []struct{ src, want string }{ + {`p :hello[1]`, `"e"`}, + {`p :hello[-1]`, `"o"`}, + {`p :hello[1, 3]`, `"ell"`}, + {`p :hello[1..3]`, `"ell"`}, + {`p :hello["ell"]`, `"ell"`}, + {`p :hello["xyz"]`, `nil`}, + {`p :hello[10]`, `nil`}, + // Regexp forms. + {`p :hello[/l+/]`, `"ll"`}, + {`p :hello[/l(l)/, 1]`, `"l"`}, + {`p :hello[/xyz/]`, `nil`}, + // A Regexp match sets $~. + {`:hello[/l+/]; p $~[0]`, `"ll"`}, + // #slice is the same. + {`p :hello.slice(1, 3)`, `"ell"`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +}