Skip to content

Commit 70b0dc1

Browse files
committed
Kernel#raise must accept extra kwargs for compatibility
1 parent 59552db commit 70b0dc1

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

spec/ruby/shared/kernel/raise.rb

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,41 @@ def initialize(data)
2929
@data = data
3030
end
3131
end
32-
-> { @object.raise(data_error, {:data => 42}) }.should raise_error(data_error) do |ex|
33-
ex.data.should == {:data => 42}
32+
33+
-> { @object.raise(data_error, {data: 42}) }.should raise_error(data_error) do |ex|
34+
ex.data.should == {data: 42}
35+
end
36+
end
37+
38+
# https://bugs.ruby-lang.org/issues/8257#note-36
39+
it "allows extra keyword arguments for compatibility" do
40+
data_error = Class.new(StandardError) do
41+
attr_reader :data
42+
def initialize(data)
43+
@data = data
44+
end
45+
end
46+
47+
-> { @object.raise(data_error, data: 42) }.should raise_error(data_error) do |ex|
48+
ex.data.should == {data: 42}
3449
end
3550
end
3651

52+
it "does not allow message and extra keyword arguments" do
53+
data_error = Class.new(StandardError) do
54+
attr_reader :data
55+
def initialize(data)
56+
@data = data
57+
end
58+
end
59+
60+
-> { @object.raise(data_error, {a: 1}, b: 2) }.should raise_error(StandardError) do |e|
61+
[TypeError, ArgumentError].should.include?(e.class)
62+
end
63+
64+
-> { @object.raise(data_error, {a: 1}, [], b: 2) }.should raise_error(ArgumentError)
65+
end
66+
3767
it "raises RuntimeError if no exception class is given" do
3868
-> { @object.raise }.should raise_error(RuntimeError, "")
3969
end

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,22 @@ def warn(*messages, uplevel: undefined, category: nil)
670670
end
671671
module_function :warn
672672

673-
def raise(exc = undefined, msg = undefined, ctx = nil, cause: undefined)
673+
def raise(exc = undefined, msg = undefined, ctx = nil, cause: undefined, **kwargs)
674674
cause_given = !Primitive.undefined?(cause)
675675
cause = cause_given ? cause : $!
676676

677677
if Primitive.undefined?(exc) and cause
678678
raise ArgumentError, 'only cause is given with no arguments' if cause_given
679679
exc = cause
680680
else
681+
unless kwargs.empty?
682+
if Primitive.undefined?(msg)
683+
msg = kwargs
684+
else
685+
raise ArgumentError, 'cannot give both message and extra keyword arguments'
686+
end
687+
end
688+
681689
exc = Truffle::ExceptionOperations.build_exception_for_raise(exc, msg)
682690

683691
exc.set_backtrace(ctx) if ctx

0 commit comments

Comments
 (0)