Skip to content

Commit e2a94c7

Browse files
committed
Fix exception messages and use global variable alias names.
Fix TestString#test_fs_setter Original error: ``` 1) Failure: TestString#test_fs_setter [/b/b/e/main/test/mri/tests/ruby/test_string.rb:2704]: Expected Exception(TypeError) was raised, but the message doesn't match. Expected /\$分行/ to match "$/ must be a String". ```
1 parent 89ceb11 commit e2a94c7

File tree

9 files changed

+144
-53
lines changed

9 files changed

+144
-53
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ def rb_backref_get
21132113
end
21142114

21152115
def rb_backref_set(value)
2116-
Truffle::RegexpOperations::LAST_MATCH_SET.call(value, rb_get_special_vars())
2116+
Truffle::RegexpOperations::LAST_MATCH_SET.call(nil, value, rb_get_special_vars())
21172117
end
21182118

21192119
def rb_gv_set(name, value)
@@ -2152,7 +2152,7 @@ def rb_define_hooked_variable(name, gvar, getter, setter)
21522152
nil)
21532153
}
21542154

2155-
setter_proc = -> value {
2155+
setter_proc = -> _, value {
21562156
Primitive.call_with_c_mutex_and_frame(
21572157
POINTER3_TO_VOID_WRAPPER,
21582158
[setter, Primitive.cext_wrap(value), Primitive.cext_wrap(id), gvar],

spec/ruby/language/predefined_spec.rb

Lines changed: 112 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def obj.foo2(&proc); proc.call; end
8888
$~ = /foo/.match("foo")
8989
$~.should be_an_instance_of(MatchData)
9090

91-
-> { $~ = Object.new }.should raise_error(TypeError)
92-
-> { $~ = 1 }.should raise_error(TypeError)
91+
-> { $~ = Object.new }.should raise_error(TypeError, 'wrong argument type Object (expected MatchData)')
92+
-> { $~ = 1 }.should raise_error(TypeError, 'wrong argument type Integer (expected MatchData)')
9393
end
9494

9595
it "changes the value of derived capture globals when assigned" do
@@ -136,6 +136,12 @@ def obj.foo2(&proc); proc.call; end
136136
"abc".dup.force_encoding(Encoding::EUC_JP) =~ /b/
137137
$&.encoding.should equal(Encoding::EUC_JP)
138138
end
139+
140+
it "is read-only" do
141+
-> {
142+
eval %q{$& = ""}
143+
}.should raise_error(SyntaxError, /Can't set variable \$&/)
144+
end
139145
end
140146

141147
describe "Predefined global $`" do
@@ -154,6 +160,12 @@ def obj.foo2(&proc); proc.call; end
154160
"abc".dup.force_encoding(Encoding::ISO_8859_1) =~ /a/
155161
$`.encoding.should equal(Encoding::ISO_8859_1)
156162
end
163+
164+
it "is read-only" do
165+
-> {
166+
eval %q{$` = ""}
167+
}.should raise_error(SyntaxError, /Can't set variable \$`/)
168+
end
157169
end
158170

159171
describe "Predefined global $'" do
@@ -172,6 +184,12 @@ def obj.foo2(&proc); proc.call; end
172184
"abc".dup.force_encoding(Encoding::ISO_8859_1) =~ /c/
173185
$'.encoding.should equal(Encoding::ISO_8859_1)
174186
end
187+
188+
it "is read-only" do
189+
-> {
190+
eval %q{$' = ""}
191+
}.should raise_error(SyntaxError, /Can't set variable \$'/)
192+
end
175193
end
176194

177195
describe "Predefined global $+" do
@@ -190,6 +208,12 @@ def obj.foo2(&proc); proc.call; end
190208
"abc".dup.force_encoding(Encoding::EUC_JP) =~ /(b)/
191209
$+.encoding.should equal(Encoding::EUC_JP)
192210
end
211+
212+
it "is read-only" do
213+
-> {
214+
eval %q{$+ = ""}
215+
}.should raise_error(SyntaxError, /Can't set variable \$\+/)
216+
end
193217
end
194218

195219
describe "Predefined globals $1..N" do
@@ -229,7 +253,7 @@ def test(arg)
229253
end
230254

231255
it "raises TypeError error if assigned to nil" do
232-
-> { $stdout = nil }.should raise_error(TypeError)
256+
-> { $stdout = nil }.should raise_error(TypeError, '$stdout must have write method, NilClass given')
233257
end
234258

235259
it "raises TypeError error if assigned to object that doesn't respond to #write" do
@@ -253,6 +277,12 @@ def test(arg)
253277
$!.should == nil
254278
end
255279

280+
it "is read-only" do
281+
-> {
282+
$! = []
283+
}.should raise_error(NameError, '$! is a read-only variable')
284+
end
285+
256286
# See http://jira.codehaus.org/browse/JRUBY-5550
257287
it "remains nil after a failed core class \"checked\" coercion against a class that defines method_missing" do
258288
$!.should == nil
@@ -512,6 +542,64 @@ def foo
512542
end
513543
end
514544

545+
describe "Predefined global $@" do
546+
it "is Fiber-local" do
547+
Fiber.new do
548+
raise "hi"
549+
rescue
550+
Fiber.yield
551+
end.resume
552+
553+
$@.should == nil
554+
end
555+
556+
it "is set to a backtrace of a rescued exception" do
557+
begin
558+
raise
559+
rescue
560+
$@.should be_an_instance_of(Array)
561+
$@.should == $!.backtrace
562+
end
563+
end
564+
565+
it "is cleared when an exception is rescued" do
566+
begin
567+
raise
568+
rescue
569+
end
570+
571+
$@.should == nil
572+
end
573+
574+
it "is not set when there is no current exception" do
575+
$@.should == nil
576+
end
577+
578+
it "is set to a backtrace of a rescued exception" do
579+
begin
580+
raise
581+
rescue
582+
$@.should be_an_instance_of(Array)
583+
$@.should == $!.backtrace
584+
end
585+
end
586+
587+
it "is not read-only" do
588+
begin
589+
raise
590+
rescue
591+
$@ = []
592+
$@.should == []
593+
end
594+
end
595+
596+
it "cannot be assigned when there is no a rescued exception" do
597+
-> {
598+
$@ = []
599+
}.should raise_error(ArgumentError, '$! not set')
600+
end
601+
end
602+
515603
# Input/Output Variables
516604
# ---------------------------------------------------------------------------------------------------
517605
#
@@ -583,15 +671,15 @@ def foo
583671
obj = mock("$/ value")
584672
obj.should_not_receive(:to_str)
585673

586-
-> { $/ = obj }.should raise_error(TypeError)
674+
-> { $/ = obj }.should raise_error(TypeError, 'value of $/ must be String')
587675
end
588676

589677
it "raises a TypeError if assigned an Integer" do
590-
-> { $/ = 1 }.should raise_error(TypeError)
678+
-> { $/ = 1 }.should raise_error(TypeError, 'value of $/ must be String')
591679
end
592680

593681
it "raises a TypeError if assigned a boolean" do
594-
-> { $/ = true }.should raise_error(TypeError)
682+
-> { $/ = true }.should raise_error(TypeError, 'value of $/ must be String')
595683
end
596684
end
597685

@@ -632,15 +720,15 @@ def foo
632720
obj = mock("$-0 value")
633721
obj.should_not_receive(:to_str)
634722

635-
-> { $-0 = obj }.should raise_error(TypeError)
723+
-> { $-0 = obj }.should raise_error(TypeError, 'value of $-0 must be String')
636724
end
637725

638726
it "raises a TypeError if assigned an Integer" do
639-
-> { $-0 = 1 }.should raise_error(TypeError)
727+
-> { $-0 = 1 }.should raise_error(TypeError, 'value of $-0 must be String')
640728
end
641729

642730
it "raises a TypeError if assigned a boolean" do
643-
-> { $-0 = true }.should raise_error(TypeError)
731+
-> { $-0 = true }.should raise_error(TypeError, 'value of $-0 must be String')
644732
end
645733
end
646734

@@ -674,12 +762,12 @@ def foo
674762
obj = mock("$\\ value")
675763
obj.should_not_receive(:to_str)
676764

677-
-> { $\ = obj }.should raise_error(TypeError)
765+
-> { $\ = obj }.should raise_error(TypeError, 'value of $\ must be String')
678766
end
679767

680768
it "raises a TypeError if assigned not String" do
681-
-> { $\ = 1 }.should raise_error(TypeError)
682-
-> { $\ = true }.should raise_error(TypeError)
769+
-> { $\ = 1 }.should raise_error(TypeError, 'value of $\ must be String')
770+
-> { $\ = true }.should raise_error(TypeError, 'value of $\ must be String')
683771
end
684772
end
685773

@@ -693,7 +781,7 @@ def foo
693781
end
694782

695783
it "raises TypeError if assigned a non-String" do
696-
-> { $, = Object.new }.should raise_error(TypeError)
784+
-> { $, = Object.new }.should raise_error(TypeError, 'value of $, must be String')
697785
end
698786

699787
it "warns if assigned non-nil" do
@@ -878,15 +966,15 @@ def obj.foo2; yield; end
878966
it "is read-only" do
879967
-> {
880968
$: = []
881-
}.should raise_error(NameError)
969+
}.should raise_error(NameError, '$: is a read-only variable')
882970

883971
-> {
884972
$LOAD_PATH = []
885-
}.should raise_error(NameError)
973+
}.should raise_error(NameError, '$LOAD_PATH is a read-only variable')
886974

887975
-> {
888976
$-I = []
889-
}.should raise_error(NameError)
977+
}.should raise_error(NameError, '$-I is a read-only variable')
890978
end
891979

892980
it "default $LOAD_PATH entries until sitelibdir included have @gem_prelude_index set" do
@@ -908,35 +996,35 @@ def obj.foo2; yield; end
908996
it "is read-only" do
909997
-> {
910998
$" = []
911-
}.should raise_error(NameError)
999+
}.should raise_error(NameError, '$" is a read-only variable')
9121000

9131001
-> {
9141002
$LOADED_FEATURES = []
915-
}.should raise_error(NameError)
1003+
}.should raise_error(NameError, '$LOADED_FEATURES is a read-only variable')
9161004
end
9171005
end
9181006

9191007
describe "Global variable $<" do
9201008
it "is read-only" do
9211009
-> {
9221010
$< = nil
923-
}.should raise_error(NameError)
1011+
}.should raise_error(NameError, '$< is a read-only variable')
9241012
end
9251013
end
9261014

9271015
describe "Global variable $FILENAME" do
9281016
it "is read-only" do
9291017
-> {
9301018
$FILENAME = "-"
931-
}.should raise_error(NameError)
1019+
}.should raise_error(NameError, '$FILENAME is a read-only variable')
9321020
end
9331021
end
9341022

9351023
describe "Global variable $?" do
9361024
it "is read-only" do
9371025
-> {
9381026
$? = nil
939-
}.should raise_error(NameError)
1027+
}.should raise_error(NameError, '$? is a read-only variable')
9401028
end
9411029

9421030
it "is thread-local" do
@@ -947,19 +1035,19 @@ def obj.foo2; yield; end
9471035

9481036
describe "Global variable $-a" do
9491037
it "is read-only" do
950-
-> { $-a = true }.should raise_error(NameError)
1038+
-> { $-a = true }.should raise_error(NameError, '$-a is a read-only variable')
9511039
end
9521040
end
9531041

9541042
describe "Global variable $-l" do
9551043
it "is read-only" do
956-
-> { $-l = true }.should raise_error(NameError)
1044+
-> { $-l = true }.should raise_error(NameError, '$-l is a read-only variable')
9571045
end
9581046
end
9591047

9601048
describe "Global variable $-p" do
9611049
it "is read-only" do
962-
-> { $-p = true }.should raise_error(NameError)
1050+
-> { $-p = true }.should raise_error(NameError, '$-p is a read-only variable')
9631051
end
9641052
end
9651053

src/main/java/org/truffleruby/language/globals/WriteGlobalVariableNode.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,27 @@ Object write(VirtualFrame frame, Object value,
5252
return value;
5353
}
5454

55-
@Specialization(guards = { "storage.hasHooks()", "arity != 2" })
55+
@Specialization(guards = { "storage.hasHooks()", "arity != 3" })
5656
Object writeHooks(VirtualFrame frame, Object value,
5757
@Bind("getStorage(frame)") GlobalVariableStorage storage,
5858
@Bind("setterArity(storage)") int arity,
59+
@Bind("name") String boundName,
5960
@Cached @Exclusive CallBlockNode yieldNode) {
60-
yieldNode.yield(this, storage.getSetter(), value);
61+
yieldNode.yield(this, storage.getSetter(), boundName, value);
6162
return value;
6263
}
6364

64-
@Specialization(guards = { "storage.hasHooks()", "arity == 2" })
65-
static Object writeHooksWithStorage(VirtualFrame frame, Object value,
65+
@Specialization(guards = { "storage.hasHooks()", "arity == 3" })
66+
Object writeHooksWithStorage(VirtualFrame frame, Object value,
6667
@Bind("getStorage(frame)") GlobalVariableStorage storage,
6768
@Bind("setterArity(storage)") int arity,
69+
@Bind("name") String boundName,
6870
@Cached @Exclusive CallBlockNode yieldNode,
6971
@Cached GetSpecialVariableStorage storageNode,
7072
@Bind Node node) {
7173
yieldNode.yield(node,
7274
storage.getSetter(),
75+
boundName,
7376
value,
7477
storageNode.execute(frame, node));
7578
return value;

src/main/ruby/truffleruby/core/argf.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,5 +637,5 @@ def advance!
637637
Truffle::KernelOperations.define_hooked_variable(
638638
:$.,
639639
-> { ARGF.instance_variable_get(:@last_lineno) },
640-
-> value { value = Primitive.rb_to_int(value)
641-
ARGF.instance_variable_set(:@last_lineno, value) } )
640+
-> _, value { value = Primitive.rb_to_int(value)
641+
ARGF.instance_variable_set(:@last_lineno, value) } )

src/main/ruby/truffleruby/core/io.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2488,4 +2488,4 @@ def write_nonblock(data, **options)
24882488
Truffle::KernelOperations.define_hooked_variable(
24892489
:$_,
24902490
-> s { Primitive.io_last_line_get(s) },
2491-
-> v, s { Primitive.io_last_line_set(s, v) })
2491+
-> _, v, s { Primitive.io_last_line_set(s, v) })

src/main/ruby/truffleruby/core/process.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ def switch
927927
Truffle::KernelOperations.define_hooked_variable(
928928
:'$0',
929929
-> { Primitive.global_variable_get :'$0' },
930-
-> v {
930+
-> _, v {
931931
v = StringValue(v)
932932
Process.setproctitle(v)
933933
Primitive.global_variable_set :'$0', v

src/main/ruby/truffleruby/core/thread.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,6 @@ def marshal_dump
377377
Truffle::KernelOperations.define_hooked_variable(
378378
:$@,
379379
-> { $!.backtrace if $! },
380-
-> value { raise ArgumentError, '$! not set' unless $!
381-
$!.set_backtrace value }
380+
-> _, value { raise ArgumentError, '$! not set' unless $!
381+
$!.set_backtrace value }
382382
)

0 commit comments

Comments
 (0)