From 84d931415ee8d8839c7fcc65047a8b1112ab851d Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 31 Oct 2017 09:33:22 +0000 Subject: [PATCH] compile.c: ensure after return in library toplevel * compile.c (compile_return): execute ensure clause after toplevel return even in library toplevel other than the main script. [ruby-core:83589] [Bug #14061] test git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60590 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- compile.c | 19 +++++-------------- test/ruby/test_syntax.rb | 21 ++++++++++++++++----- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/compile.c b/compile.c index e687e441cf..77d6fc8672 100644 --- a/compile.c +++ b/compile.c @@ -5111,27 +5111,18 @@ compile_return(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, enum iseq_type type = iseq->body->type; const rb_iseq_t *parent_iseq = iseq->body->parent_iseq; enum iseq_type parent_type; + const NODE *retval = node->nd_stts; LABEL *splabel = 0; if (type == ISEQ_TYPE_TOP) { - splabel = NEW_LABEL(line); - ADD_LABEL(ret, splabel); - ADD_ADJUST(ret, line, 0); - ADD_INSN(ret, line, putnil); - ADD_INSN(ret, line, leave); - ADD_ADJUST_RESTORE(ret, splabel); - return COMPILE_OK; + retval = 0; + type = ISEQ_TYPE_METHOD; } else if ((type == ISEQ_TYPE_RESCUE || type == ISEQ_TYPE_ENSURE || type == ISEQ_TYPE_MAIN) && parent_iseq && ((parent_type = parent_iseq->body->type) == ISEQ_TYPE_TOP || parent_type == ISEQ_TYPE_MAIN)) { - ADD_INSN(ret, line, putnil); - ADD_INSN1(ret, line, throw, INT2FIX(TAG_RETURN)); - if (popped) { - ADD_INSN(ret, line, pop); - } - return COMPILE_OK; + retval = 0; } if (type == ISEQ_TYPE_METHOD) { @@ -5140,7 +5131,7 @@ compile_return(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, ADD_ADJUST(ret, line, 0); } - CHECK(COMPILE(ret, "return nd_stts (return val)", node->nd_stts)); + CHECK(COMPILE(ret, "return nd_stts (return val)", retval)); if (type == ISEQ_TYPE_METHOD) { add_ensure_iseq(ret, iseq, 1); diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index beb85272ff..0a0ef83f8b 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -990,7 +990,8 @@ eom def test_return_toplevel feature4840 = '[ruby-core:36785] [Feature #4840]' - code = "#{<<~"begin;"}\n#{<<~'end;'}" + line = __LINE__+2 + code = "#{<<~"begin;"}#{<<~'end;'}" begin; return; raise begin return; rescue SystemExit; exit false; end @@ -1006,10 +1007,20 @@ eom begin raise; ensure return; end and self nil&defined?0--begin e=no_method_error(); return; 0;end end; - all_assertions_foreach(feature4840, *code.split(/\n/)) do |s| - assert_in_out_err(%[-W0], s, [*s[/#=> (.*)/, 1]], [], - proc {RubyVM::InstructionSequence.compile(s).disasm}, - success: true) + .split(/\n/).map {|s|[(line+=1), *s.split(/#=> /, 2)]} + failed = proc do |n, s| + RubyVM::InstructionSequence.compile(s, __FILE__, nil, n).disasm + end + all_assertions_foreach(feature4840, *code) do |n, s, *ex| + assert_in_out_err(%[-W0], src = s, ex, [], proc {failed[n, s]}, success: true) + end + Tempfile.create(%w"test_return_ .rb") do |lib| + lib.close + args = %W[-W0 -r#{lib.path}] + all_assertions_foreach(feature4840, *code) do |n, s, *ex| + File.write(lib, s) + assert_in_out_err(args, "", ex, [], proc {failed[n, s]}, success: true) + end end end