Skip to content

Commit 06d605e

Browse files
tannevaledclaude
andauthored
The separator globals accept a String subclass, as MRI's T_STRING test does (#897)
Closes #884. `language/predefined_spec.rb` goes 143 → 145, errors 6 → 4. ## How this was found By counting, not by reading the backlog. #799's table credits #886 with 4 examples of `language/predefined_spec.rb`; running the file against current main says otherwise: RBGO_RESULT pass=143 fail=16 error=6 RBGO_DETAIL error TypeError Predefined global $/ | makes a new frozen String from the assigned String value of $/ must be String RBGO_DETAIL error TypeError Predefined global $-0 | makes a new frozen String from the assigned String value of $-0 must be String #886 closed TWO of the four, not four, and said so at the time — the String subclass example was named in its body as a known limitation and filed as #884. The tally in the issue kept the old number, and a pass count cannot tell the difference. ## The cause is one assertion, and it was in five places if _, ok := v.(*object.String); !ok { raise("TypeError", ...) } An exact-type assertion. MRI's are RB_TYPE_P(val, T_STRING) in rb_str_setter and rb_check_string_type in rb_fs_check, and both are TRUE for a subclass. rbgo represents an instance of `Class.new(String)` as an RObject wrapping a String, so the assertion missed every one of them. stringTyped is that test. ## The five globals do not do the same thing afterwards Measured on MRI 4.0.7, assigning the same `Class.new(String).new("abc")` with an @ivar: | global | setter | result | |---|---|---| | `$/`, `$-0` | deprecated_rs_setter | a frozen BARE String — class String, no ivar | | `$,`, `$\` | rb_str_setter | the object as handed over, unfrozen | | `$;` | rb_fs_setter | the object as handed over, unfrozen | A fix that froze and copied everywhere would pass the two spec examples and silently rewrite the other three. The test table asserts each separately. ## $; was wrong differently, and worse It did not refuse the subclass — it fell through to #to_str, which a String subclass answers. The assignment SUCCEEDED and quietly replaced the object with a plain String. The others at least raised. ## frozenBareString's comment had outlived its reason It said only the ivar half of BARE_STRING_P was tested, because a subclass "never reaches this function at all". That was true while checkStrGvar rejected it. Now it does reach it, and without the class test the subclass instance would be returned AS IS — ivar and all. The function takes the original value as well as the String inside it, and tests both halves. ## Measured rubydiff against MRI 4.0.7 on all five globals plus the non-String refusals: AGREE. specdelta, provenanced, over language, core/string, core/io and core/kernel: 442 files, control 9666, treatment 9668, +2, 1 file moved, 0 regressed. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent f20a1ac commit 06d605e

2 files changed

Lines changed: 159 additions & 22 deletions

File tree

‎internal/vm/globals.go‎

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,38 @@ func (vm *VM) checkStrGvar(name string, v object.Value) object.Value {
481481
if object.IsNil(v) {
482482
return object.NilV
483483
}
484-
if _, ok := v.(*object.String); !ok {
484+
if _, ok := stringTyped(v); !ok {
485485
raise("TypeError", "value of %s must be String", name)
486486
}
487487
vm.warnDeprecatedGvar("non-nil '" + name + "' is deprecated")
488+
// MRI's rb_str_setter stores what it was handed, subclass and all:
489+
// `$, = Class.new(String).new("x")` keeps that object, unfrozen. Only $/
490+
// and $-0 replace it, and they do it in checkRecordSepGvar.
488491
return v
489492
}
490493

494+
// stringTyped is RB_TYPE_P(val, T_STRING): it reports whether v is a String,
495+
// INCLUDING an instance of a String subclass — which rbgo represents as an
496+
// RObject wrapping one, not as an *object.String. (regexp.go's stringLike is a
497+
// different question: it coerces a Symbol too, and answers with the bytes.)
498+
//
499+
// ⛔ Every separator global refused `Class.new(String).new("x")` because the
500+
// test here was `v.(*object.String)`, an exact-type assertion. MRI's are
501+
// RB_TYPE_P(val, T_STRING) in rb_str_setter and rb_check_string_type in
502+
// rb_fs_check, and BOTH are true for a subclass. Measured on MRI 4.0.7: $/,
503+
// $-0, $,, $\ and $; all accept one, and only $/ and $-0 convert it.
504+
func stringTyped(v object.Value) (*object.String, bool) {
505+
switch s := v.(type) {
506+
case *object.String:
507+
return s, true
508+
case *RObject:
509+
if b, ok := s.builtin.(*object.String); ok {
510+
return b, true
511+
}
512+
}
513+
return nil, false
514+
}
515+
491516
// checkRecordSepGvar applies io.c v4.0.9 deprecated_rs_setter to $/ and $-0,
492517
// which from Ruby 4.0 no longer store the object they were handed:
493518
//
@@ -514,15 +539,15 @@ func (vm *VM) checkRecordSepGvar(name string, v object.Value) object.Value {
514539
if object.IsNil(v) {
515540
return v
516541
}
517-
s := v.(*object.String)
542+
s, _ := stringTyped(v) // checkStrGvar has already refused anything else
518543
// rb_str_equal against a one-byte US-ASCII "\n": byte equality decides it.
519544
// Any ASCII-compatible encoding holding that single byte IS == "\n", and an
520545
// ASCII-incompatible one (UTF-16, UTF-32) cannot hold it in one byte at all,
521546
// so there is no case where the encodings would change the answer.
522547
if s.Str() == defaultRecordSeparator.Str() {
523548
return defaultRecordSeparator
524549
}
525-
return frozenBareString(s)
550+
return frozenBareString(v, s)
526551
}
527552

528553
// frozenBareString is string.c v4.0.9 rb_str_frozen_bare_string:
@@ -536,18 +561,24 @@ func (vm *VM) checkRecordSepGvar(name string, v object.Value) object.Value {
536561
// and a frozen plain String passes both, which is precisely what the three
537562
// examples in language/predefined_spec.rb distinguish.
538563
//
539-
// ⛔ ONLY THE IVAR HALF IS TESTED HERE, and that is not an omission. In rbgo a
564+
// ⛔ BOTH HALVES ARE TESTED, and they are tested on DIFFERENT values. orig is
565+
// what Ruby handed over — an *object.String, or an RObject wrapping one when
566+
// the assignment came from a String subclass — and s is the String inside it.
567+
// The class half is `orig` being an *object.String at all, because in rbgo a
540568
// *object.String is class String by construction (classOf maps the Go type
541-
// straight to vm.cString), and an instance of a String SUBCLASS never reaches
542-
// this function at all — checkStrGvar's type assertion rejects it first, with
543-
// "value of $/ must be String". MRI accepts one, because its check is T_STRING
544-
// and a subclass is still T_STRING. That divergence is a defect of its own,
545-
// filed rather than papered over here: it is why
546-
// predefined_spec.rb's "makes a new frozen String from the assigned String"
547-
// still fails, and fixing it belongs with the assertion, not with this rule.
548-
func frozenBareString(s *object.String) *object.String {
549-
if s.Frozen && genericIvarsOf(s, false) == nil {
550-
return s
569+
// straight to vm.cString) while a subclass instance is an RObject. The ivar
570+
// half is read from s.
571+
//
572+
// Until the subclass was accepted at all this function only ever saw the exact
573+
// type, so the class half had nothing to test and was left out with a note
574+
// saying so. The note outlived its reason: `checkStrGvar` now admits a
575+
// subclass, and without the `orig` test the subclass instance would be
576+
// returned AS IS — frozen or not, @ivar and all — which is the opposite of
577+
// what predefined_spec.rb's "makes a new frozen String from the assigned
578+
// String" asserts.
579+
func frozenBareString(orig object.Value, s *object.String) *object.String {
580+
if bare, ok := orig.(*object.String); ok && bare.Frozen && genericIvarsOf(bare, false) == nil {
581+
return bare
551582
}
552583
out := object.NewString(s.Str())
553584
out.Enc = s.Enc
@@ -563,16 +594,24 @@ func (vm *VM) checkFieldSepGvar(name string, v object.Value) object.Value {
563594
if object.IsNil(v) {
564595
return object.NilV
565596
}
566-
switch v.(type) {
567-
case *object.String, *Regexp:
568-
default:
569-
if vm.respondsToDynamic(v, "to_str") {
570-
if s, ok := vm.send(v, "to_str", nil, nil).(*object.String); ok {
571-
v = s
572-
break
597+
// ⛔ rb_check_string_type answers YES for a String subclass and hands the
598+
// object back unchanged, so `$; = Class.new(String).new("x")` keeps that
599+
// object in MRI. Testing only *object.String sent it down the #to_str path
600+
// instead, which a String subclass answers — so the assignment succeeded
601+
// and silently replaced the object with a plain String. A wrong answer is
602+
// worse here than the TypeError the other separators gave.
603+
if _, ok := stringTyped(v); !ok {
604+
switch v.(type) {
605+
case *Regexp:
606+
default:
607+
if vm.respondsToDynamic(v, "to_str") {
608+
if s, ok := vm.send(v, "to_str", nil, nil).(*object.String); ok {
609+
v = s
610+
break
611+
}
573612
}
613+
raise("TypeError", "value of %s must be String or Regexp", name)
574614
}
575-
raise("TypeError", "value of %s must be String or Regexp", name)
576615
}
577616
vm.warnDeprecatedGvar("non-nil '" + name + "' is deprecated")
578617
return v
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright (c) the go-embedded-ruby/ruby authors
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
package vm
6+
7+
import "testing"
8+
9+
// ⛔ Every separator global refused an instance of a String SUBCLASS. The test
10+
// was `v.(*object.String)`, an exact-type assertion, where MRI's are
11+
// RB_TYPE_P(val, T_STRING) (rb_str_setter) and rb_check_string_type
12+
// (rb_fs_check) — both true for a subclass. rbgo represents such an instance as
13+
// an RObject wrapping a String, so the assertion missed every one of them.
14+
//
15+
// The five globals do NOT all do the same thing with it afterwards, and that is
16+
// the point of the table: $, $\ and $; keep the object as handed over, while $/
17+
// and $-0 replace it with a frozen plain String. Every row was measured against
18+
// MRI 4.0.7 on this host.
19+
func TestSeparatorGlobalsAcceptAStringSubclass(t *testing.T) {
20+
const mk = `
21+
sc = Class.new(String)
22+
str = sc.new("abc")
23+
str.instance_variable_set(:@ivar, 1)
24+
`
25+
for _, tc := range []struct{ name, src, want string }{
26+
// $/ and $-0: deprecated_rs_setter makes a frozen BARE String, so the
27+
// class becomes String and the ivar is gone. These two rows are
28+
// language/predefined_spec.rb's "makes a new frozen String from the
29+
// assigned String", one per global.
30+
{"$/ converts a subclass to a frozen bare String",
31+
mk + `$/ = str
32+
p [$/.instance_of?(String), $/.frozen?, $/.instance_variable_defined?(:@ivar), $/ == str]`,
33+
`[true, true, false, true]`},
34+
{"$-0 converts a subclass to a frozen bare String",
35+
mk + `$-0 = str
36+
p [$-0.instance_of?(String), $-0.frozen?, $-0.instance_variable_defined?(:@ivar), $-0 == str]`,
37+
`[true, true, false, true]`},
38+
39+
// ⛔ $, $\ and $; must NOT convert: rb_str_setter stores what it was
40+
// handed. A fix that froze-and-copied everywhere would pass the two
41+
// rows above and silently rewrite these three.
42+
{"$, keeps the subclass object",
43+
mk + `$, = str
44+
p [$,.equal?(str), $,.frozen?]`,
45+
`[true, false]`},
46+
{"$\\ keeps the subclass object",
47+
mk + `$\ = str
48+
p [$\.equal?(str), $\.frozen?]`,
49+
`[true, false]`},
50+
{"$; keeps the subclass object",
51+
mk + `$; = str
52+
p [$;.equal?(str), $;.frozen?]`,
53+
`[true, false]`},
54+
55+
// ⛔ $; went down a DIFFERENT wrong path from the others: not a
56+
// TypeError but #to_str, which a String subclass answers — so the
57+
// assignment succeeded and quietly replaced the object with a plain
58+
// String. A wrong answer, where the others at least refused.
59+
{"$; no longer silently converts through #to_str",
60+
mk + `$; = str
61+
p $;.class.equal?(sc)`,
62+
`true`},
63+
64+
// Controls: the refusals that must survive.
65+
{"$/ still refuses a non-String",
66+
`begin; $/ = 1; rescue TypeError => e; p e.message; end`,
67+
`"value of $/ must be String"`},
68+
{"$, still refuses a non-String",
69+
`begin; $, = 1; rescue TypeError => e; p e.message; end`,
70+
`"value of $, must be String"`},
71+
{"$; still refuses a non-String non-Regexp",
72+
`begin; $; = 1; rescue TypeError => e; p e.message; end`,
73+
`"value of $; must be String or Regexp"`},
74+
{"$; still accepts a Regexp", `$; = /x/; p $;`, `/x/`},
75+
{"$; still accepts a #to_str duck",
76+
`class D; def to_str = "dd"; end
77+
$; = D.new; p $;`, `"dd"`},
78+
{"nil is still accepted", `$/ = nil; p $/`, `nil`},
79+
80+
// The frozen-bare short-circuit still applies to an exact String, and
81+
// the default separator still wins by identity over it.
82+
{"a frozen bare String is kept by identity",
83+
`s = "abc".freeze; $/ = s; p $/.equal?(s)`, `true`},
84+
{"the default separator wins over a frozen bare equal String",
85+
`was = $/; s = "\n".dup.freeze; $/ = s; p [$/.equal?(s), $/.equal?(was)]`,
86+
`[false, true]`},
87+
{"an exact String with an ivar is copied",
88+
`s = "abc".dup; s.instance_variable_set(:@i, 1); $/ = s
89+
p [$/.equal?(s), $/.frozen?, $/.instance_variable_defined?(:@i)]`,
90+
`[false, true, false]`},
91+
} {
92+
t.Run(tc.name, func(t *testing.T) {
93+
if got := runSrc(t, tc.src); got != tc.want {
94+
t.Errorf("%s\n got %s\nwant %s", tc.src, got, tc.want)
95+
}
96+
})
97+
}
98+
}

0 commit comments

Comments
 (0)