diff options
author | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-04-22 15:59:34 +0000 |
---|---|---|
committer | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-04-22 15:59:34 +0000 |
commit | b8aa41003e6a4548ecb26a795e653b8cb40b2ca5 (patch) | |
tree | 92cbf230a3658518fef158ed35cc32970e123ae7 /test/ruby | |
parent | 9224427bb5a483bb383c755f33a1d80867a7aa16 (diff) |
merge revision(s) 54141,54542,54548: [Backport #12082]
test_optimization.rb: tailcall
* test/ruby/test_optimization.rb (TestRubyOptimization.tailcall):
helper method to compile methods with tailcall optimization
enabled.
* compile.c (iseq_optimize): disable tail call optimization in
rescued, rescue, and ensure blocks.
[ruby-core:73871] [Bug #12082]
* compile.c (new_label_body): initialize bit fields, since
compile_data_alloc does not clear the memory. [Bug #12082]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_3@54715 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_optimization.rb | 100 |
1 files changed, 64 insertions, 36 deletions
diff --git a/test/ruby/test_optimization.rb b/test/ruby/test_optimization.rb index 17c8a3210b..15fb9195c9 100644 --- a/test/ruby/test_optimization.rb +++ b/test/ruby/test_optimization.rb @@ -230,47 +230,54 @@ class TestRubyOptimization < Test::Unit::TestCase assert_equal true, MyObj.new == nil end + def self.tailcall(klass, src, file = nil, path = nil, line = nil) + unless file + loc, = caller_locations(1, 1) + file = loc.path + line ||= loc.lineno + end + RubyVM::InstructionSequence.new("proc {|_|_.class_eval {#{src}}}", + file, (path || file), line, + tailcall_optimization: true, + trace_instruction: false) + .eval[klass] + end + + def tailcall(*args) + self.class.tailcall(singleton_class, *args) + end + def test_tailcall bug4082 = '[ruby-core:33289]' - option = { - tailcall_optimization: true, - trace_instruction: false, - } - RubyVM::InstructionSequence.new(<<-EOF, "Bug#4082", bug4082, nil, option).eval - class #{self.class}::Tailcall - def fact_helper(n, res) - if n == 1 - res - else - fact_helper(n - 1, n * res) - end - end - def fact(n) - fact_helper(n, 1) + tailcall(<<-EOF) + def fact_helper(n, res) + if n == 1 + res + else + fact_helper(n - 1, n * res) end end + def fact(n) + fact_helper(n, 1) + end EOF - assert_equal(9131, Tailcall.new.fact(3000).to_s.size, bug4082) + assert_equal(9131, fact(3000).to_s.size, bug4082) end def test_tailcall_with_block bug6901 = '[ruby-dev:46065]' - option = { - tailcall_optimization: true, - trace_instruction: false, - } - RubyVM::InstructionSequence.new(<<-EOF, "Bug#6901", bug6901, nil, option).eval - def identity(val) - val - end + tailcall(<<-EOF) + def identity(val) + val + end - def delay - -> { - identity(yield) - } - end + def delay + -> { + identity(yield) + } + end EOF assert_equal(123, delay { 123 }.call, bug6901) end @@ -280,15 +287,36 @@ class TestRubyOptimization < Test::Unit::TestCase end def test_tailcall_inhibited_by_block - assert_separately([], <<~'end;') - def just_yield - yield + tailcall(<<-EOF) + def yield_result + just_yield {:ok} + end + EOF + assert_equal(:ok, yield_result) + end + + def do_raise + raise "should be rescued" + end + + def errinfo + $! + end + + def test_tailcall_inhibited_by_rescue + bug12082 = '[ruby-core:73871] [Bug #12082]' + + tailcall(<<-'end;') + def to_be_rescued + return do_raise + 1 + 2 + rescue + errinfo end - iseq = RubyVM::InstructionSequence - result = iseq.compile("just_yield {:ok}", __FILE__, __FILE__, __LINE__, - tailcall_optimization: true).eval - assert_equal(:ok, result) end; + result = to_be_rescued + assert_instance_of(RuntimeError, result, bug12082) + assert_equal("should be rescued", result.message, bug12082) end class Bug10557 |