Project

General

Profile

Actions

Bug #12905

closed

tailcall_optimization not working as expected under certain condition order

Added by sunkai612 (Kai Sun) over 7 years ago. Updated over 7 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