Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect path taken when rescue nested inside a rescue/else #1201

Merged
merged 6 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/natalie/compiler/instructions/try_instruction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ def generate(transform)
code << '} catch(ExceptionObject *exception) {'

code << 'auto exception_was = env->exception()'

code << 'GlobalEnv::the()->set_rescued(true)'
code << 'env->set_exception(exception)'

transform.with_same_scope(catch_body) do |t|
code << t.transform(@discard_catch_result ? nil : "#{result} =")
end

code << 'if (exception_was) env->set_exception(exception_was)'
code << 'else env->clear_exception()'
code << 'env->set_exception(exception_was)'
code << 'GlobalEnv::the()->set_rescued(true)'

code << '}'
end
Expand Down
4 changes: 1 addition & 3 deletions src/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,9 @@ Value Env::exception_object() {

ExceptionObject *Env::exception() {
auto e = this;
for (;;) {
while (e) {
if (e->m_exception)
return e->m_exception;
if (!e->m_caller)
break;
e = e->m_caller;
}
return nullptr;
Expand Down
5 changes: 2 additions & 3 deletions src/kernel_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,8 @@ Value KernelModule::puts(Env *env, Args args) {

Value KernelModule::raise(Env *env, Value klass, Value message) {
if (!klass) {
if (env->exception())
klass = env->exception();
else {
klass = env->exception();
if (!klass) {
klass = find_top_level_const(env, "RuntimeError"_s);
message = new StringObject { "" };
}
Expand Down
43 changes: 43 additions & 0 deletions test/natalie/rescue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,49 @@ def test_until2
end
$!.message.should == 'foo'
end
$!.should be_nil

begin
raise 'foo'
rescue
begin
rescue
else
$!.message.should == 'foo'
end
$!.message.should == 'foo'
end
end

it 'does not get confused by nested rescues' do
ran = []
begin
raise 'foo'
rescue
ran << :rescue
begin
rescue
end
else
ran << :else
end
ran.should == [:rescue]
end

it 'does not get confused by an unused LocalJumpError' do
ran = []
l = -> {
begin
raise 'foo'
rescue
ran << :rescue
nil.tap { return 'bar' if false } # return here could raise a LocalJumpError, but doesn't
else
ran << :else
end
}
l.call
ran.should == [:rescue]
end
end

Expand Down