1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2017-10-31 09:33:22 +00:00
parent 6806c16fef
commit 84d931415e
2 changed files with 21 additions and 19 deletions

View file

@ -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);

View file

@ -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