Project

General

Profile

Actions

Bug #12082

closed

Tail-calling method can't catch exception raised by tail-called method

Added by rhenium (Kazuki Yamaguchi) about 8 years ago. Updated almost 8 years ago.

Status:
Closed
Target version:
-
[ruby-core:73871]

Description

The following code doesn't work as expected, on all versions of Ruby with tail call optimization (1.9.1 to 2.3.0).

def do_raise
  raise "should be rescued"
end
options = {
  tailcall_optimization: true,
  trace_instruction: false,
}
RubyVM::InstructionSequence.compile(<<EOF, __FILE__, __FILE__, __LINE__, options).eval
  def test_rescue
    return do_raise
    1 + 2
  rescue
    :ok
  end
EOF

p test_rescue # should print :ok, but raises "should be rescued"

Looks like nop instruction is (also) used to avoid this optimization (compile.c), but when doing early return, no nop is inserted.

I attached a (dirty) fix for this. Maybe there is a cleaner way.


Files

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0