Skip to content

Commit d8f759e

Browse files
committed
[GR-18163] Refactor Thread#raise to share logic with Kernel#raise (#2045).
PullRequest: truffleruby/1765
2 parents dd6c140 + 91f1e36 commit d8f759e

File tree

13 files changed

+90
-89
lines changed

13 files changed

+90
-89
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Compatibility:
6161
* Change the lookup methods to achieve Refinements specification (#2033, @ssnickolay)
6262
* Implemented `Digest::Instance#new` (#2040).
6363
* Implemented `ONIGENC_MBC_CASE_FOLD`.
64+
* Fixed `Thread#raise` to call the exception class' constructor with no arguments when given no message (#2045).
6465

6566
Performance:
6667

lib/truffle/truffle/cext.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ def rb_ruby_debug_ptr
14121412
end
14131413

14141414
def rb_tr_error(message)
1415-
Truffle::KernelOperations.internal_raise RuntimeError, message, nil, true
1415+
raise RuntimeError, message
14161416
end
14171417

14181418
def test_kwargs(kwargs, raise_error)

spec/ruby/core/kernel/raise_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@
66
it "is a private method" do
77
Kernel.should have_private_instance_method(:raise)
88
end
9+
10+
it "re-raises the previously rescued exception if no exception is specified" do
11+
ScratchPad.record nil
12+
13+
-> do
14+
begin
15+
raise Exception, "outer"
16+
ScratchPad.record :no_abort
17+
rescue Exception
18+
begin
19+
raise StandardError, "inner"
20+
rescue StandardError
21+
end
22+
23+
raise
24+
ScratchPad.record :no_reraise
25+
end
26+
end.should raise_error(Exception, "outer")
27+
28+
ScratchPad.recorded.should be_nil
29+
end
930
end
1031

1132
describe "Kernel#raise" do

spec/ruby/core/thread/raise_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,6 @@
203203
Thread.current.raise
204204
end
205205
end
206-
-> { t.value }.should raise_error(RuntimeError)
206+
-> { t.value }.should raise_error(RuntimeError, '')
207207
end
208208
end

spec/ruby/shared/kernel/raise.rb

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
-> { @object.raise("a bad thing") }.should raise_error(RuntimeError)
2626
end
2727

28+
it "passes no arguments to the constructor when given only an exception class" do
29+
klass = Class.new(Exception) do
30+
def initialize
31+
end
32+
end
33+
-> { @object.raise(klass) }.should raise_error(klass) { |e| e.message.should == klass.to_s }
34+
end
35+
2836
it "raises a TypeError when passed a non-Exception object" do
2937
-> { @object.raise(Object.new) }.should raise_error(TypeError)
3038
end
@@ -41,25 +49,6 @@
4149
-> { @object.raise(nil) }.should raise_error(TypeError)
4250
end
4351

44-
it "re-raises the previously rescued exception if no exception is specified" do
45-
-> do
46-
begin
47-
@object.raise Exception, "outer"
48-
ScratchPad.record :no_abort
49-
rescue
50-
begin
51-
@object.raise StandardError, "inner"
52-
rescue
53-
end
54-
55-
@object.raise
56-
ScratchPad.record :no_reraise
57-
end
58-
end.should raise_error(Exception, "outer")
59-
60-
ScratchPad.recorded.should be_nil
61-
end
62-
6352
it "re-raises a previously rescued exception without overwriting the backtrace" do
6453
begin
6554
initial_raise_line = __LINE__; @object.raise 'raised'

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
11411141
"/core/string.rb",
11421142
"/core/random.rb",
11431143
"/core/truffle/kernel_operations.rb",
1144+
"/core/truffle/exception_operations.rb",
11441145
"/core/truffle/feature_loader.rb",
11451146
"/core/truffle/gem_util.rb",
11461147
"/core/thread.rb",
@@ -1155,7 +1156,6 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
11551156
"/core/truffle/boot.rb",
11561157
"/core/truffle/debug.rb",
11571158
"/core/truffle/encoding_operations.rb",
1158-
"/core/truffle/exception_operations.rb",
11591159
"/core/truffle/hash_operations.rb",
11601160
"/core/truffle/numeric_operations.rb",
11611161
"/core/truffle/proc_operations.rb",

src/main/java/org/truffleruby/core/VMPrimitiveNodes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected Object vmMethodLookup(VirtualFrame frame, Object receiver, Object name
168168
public static abstract class VMRaiseExceptionNode extends PrimitiveArrayArgumentsNode {
169169

170170
@Specialization(guards = "isRubyException(exception)")
171-
protected DynamicObject vmRaiseException(DynamicObject exception, boolean internal,
171+
protected DynamicObject vmRaiseException(DynamicObject exception,
172172
@Cached ConditionProfile reRaiseProfile) {
173173
final Backtrace backtrace = Layouts.EXCEPTION.getBacktrace(exception);
174174
if (reRaiseProfile.profile(backtrace != null && backtrace.getRaiseException() != null)) {
@@ -181,7 +181,7 @@ protected DynamicObject vmRaiseException(DynamicObject exception, boolean intern
181181
}
182182
throw backtrace.getRaiseException();
183183
} else {
184-
throw new RaiseException(getContext(), exception, internal);
184+
throw new RaiseException(getContext(), exception);
185185
}
186186
}
187187

@@ -192,7 +192,7 @@ public static void reRaiseException(RubyContext context, DynamicObject exception
192192
// TruffleStackTrace stored in it.
193193
throw backtrace.getRaiseException();
194194
} else {
195-
throw new RaiseException(context, exception, false);
195+
throw new RaiseException(context, exception);
196196
}
197197
}
198198

src/main/java/org/truffleruby/core/exception/ExceptionNodes.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,9 @@ private Object readCustomBacktrace(DynamicObject exception) {
239239
}
240240

241241
@NonStandard
242-
@CoreMethod(names = "capture_backtrace!", optional = 1, lowerFixnum = 1)
242+
@CoreMethod(names = "capture_backtrace!", required = 1, lowerFixnum = 1)
243243
public abstract static class CaptureBacktraceNode extends CoreMethodArrayArgumentsNode {
244244

245-
@Specialization
246-
protected Object captureBacktrace(DynamicObject exception, NotProvided offset) {
247-
return captureBacktrace(exception, 1);
248-
}
249-
250245
@Specialization
251246
protected Object captureBacktrace(DynamicObject exception, int offset) {
252247
final Backtrace backtrace = getContext().getCallStack().getBacktrace(this, offset);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def fill(io, max = DEFAULT_READ_SIZE)
177177
# Detect if another thread has updated the buffer
178178
# and now there isn't enough room for this data.
179179
if bytes_read > unused
180-
Truffle::KernelOperations.internal_raise RuntimeError, 'internal implementation error - IO buffer overrun', nil, true
180+
raise RuntimeError, 'internal implementation error - IO buffer overrun'
181181
end
182182
@storage.fill(@used, buffer, 0, bytes_read)
183183
@used += bytes_read

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,21 @@ def warn(*messages, uplevel: undefined)
657657
end
658658
module_function :warn
659659

660-
def raise(exc=undefined, msg=undefined, ctx=nil)
661-
Truffle::KernelOperations.internal_raise exc, msg, ctx, false
660+
def raise(exc = undefined, msg = undefined, ctx = nil)
661+
last = $!
662+
if Primitive.undefined?(exc) and last
663+
exc = last
664+
else
665+
exc = Truffle::ExceptionOperations.build_exception_for_raise(exc, msg)
666+
667+
exc.set_context ctx if ctx
668+
exc.capture_backtrace!(1) unless exc.backtrace?
669+
Primitive.exception_set_cause exc, last unless Primitive.object_equal(exc, last)
670+
end
671+
672+
Truffle::ExceptionOperations.show_exception_for_debug(exc, 1) if $DEBUG
673+
674+
Primitive.vm_raise_exception exc
662675
end
663676
module_function :raise
664677

0 commit comments

Comments
 (0)