[ruby-core:78019] [Ruby trunk Bug#12905] tailcall_optimization not working as expected under certain condition order

From: tonysun329@...
Date: 2016-11-06 16:46:50 UTC
List: ruby-core #78019
Issue #12905 has been updated by Kai Sun.


I have also tried below and still get SystemStackError:

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

RubyVM::InstructionSequence.new(<<-EOF).eval
def run_forever(current, final)
  if current < final
    run_forever(current+1, final)
  else
    nil
  end
end
EOF

run_forever(1, Float::INFINITY)
~~~

However, if we alter the conditional order, it will work as expected:

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

RubyVM::InstructionSequence.new(<<-EOF).eval
def run_forever(current, final)
  if current >= final
    nil
  else
    run_forever(current+1, final)
  end
end
EOF

run_forever(1, Float::INFINITY)
~~~


----------------------------------------
Bug #12905: tailcall_optimization not working as expected under certain condition order
https://bugs.ruby-lang.org/issues/12905#change-61362

* Author: Kai Sun
* Status: Rejected
* Priority: Normal
* Assignee: 
* ruby -v: 2.3.1
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
Below code gets stack too deep error:

~~~ ruby
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:

~~~ ruby
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 :)



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next