Bug #9114
closedInstructionSequence.compile w/tailcall_optimization: true, trace_instruction: false not working as expected
Description
Code to reproduce is a recursive sort I wrote; I was trying to compile RubyVM::InstructionSequence with tailcall_optimization: true, trace_instruction: false, which has worked before, but not for this case.
method_string = <<RUBY
def recursively_sort(obj, in_sort_by=false)
if obj.respond_to?(:sort_by)
obj.sort_by{|*args|args.map{|v|recursively_sort(v, true)}}
end rescue nil
in_sort_by ? (obj.to_s rescue obj.inspect rescue nil) : obj
end
RUBY
RubyVM::InstructionSequence.new(method_string, nil, nil, nil, tailcall_optimization: true, trace_instruction: false).eval
a = eval '[{b:['*1000 + '2,1' + '],a:1}]'*1000
recursively_sort a
results in error for latest releases of Ruby 1.9.3 and 2.0.0:
1.9.3-p448 :014 > recursively_sort a
SystemStackError: stack level too deep
from /Users/gary/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!
2.0.0p247 :014 > recursively_sort a
SystemStackError: stack level too deep
from /Users/gary/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!
I had assumed that since the blocks were defined within the tail call optimized method that referenced the method compiled with TCO, it would still be tail call optimized.
Did I do something wrong/is there a suggested workaround?
Thanks for looking at this!