Project

General

Profile

Actions

Bug #12905

closed

tailcall_optimization not working as expected under certain condition order

Added by sunkai612 (Kai Sun) about 8 years ago. Updated about 8 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:78015]

Description

Below code gets stack too deep error:

RubyVM::InstructionSequence.compile_option = {
  :tailcall_optimization => true,
  :trace_instruction => false
}

def run_forever(current, final)
  if current < final
    run_forever(current+1, final)
  else
    nil
  end
end

run_forever(1, Float::INFINITY)

However, below code works as expected:

RubyVM::InstructionSequence.compile_option = {
  :tailcall_optimization => true,
  :trace_instruction => false
}

def run_forever(current, final)
  if current >= final
    nil
  else
    run_forever(current+1, final)
  end
end

run_forever(1, Float::INFINITY)

Thanks for looking at this :)

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0