Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile.c: toplevel return #1043

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4535,7 +4535,13 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
ADD_INSN(ret, line, nop);
}
else {
if (iseq->body->type == ISEQ_TYPE_MAIN) {
ADD_ADJUST(ret, line, lstart);
}
ADD_SEQ(ret, ensr);
if (iseq->body->type == ISEQ_TYPE_MAIN) {
ADD_ADJUST(ret, line, lstart);
}
}
ADD_LABEL(ret, lcont);

Expand Down Expand Up @@ -5344,21 +5350,27 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
rb_iseq_t *is = iseq;

if (is) {
if (is->body->type == ISEQ_TYPE_TOP) {
COMPILE_ERROR(ERROR_ARGS "Invalid return");
enum iseq_type type = is->body->type;

if (type == ISEQ_TYPE_TOP ||
type == ISEQ_TYPE_MAIN ||
type == ISEQ_TYPE_ENSURE ||
0) {
ADD_INSN(ret, line, putnil);
ADD_INSN(ret, line, leave);
}
else {
LABEL *splabel = 0;

if (is->body->type == ISEQ_TYPE_METHOD) {
if (type == ISEQ_TYPE_METHOD) {
splabel = NEW_LABEL(0);
ADD_LABEL(ret, splabel);
ADD_ADJUST(ret, line, 0);
}

COMPILE(ret, "return nd_stts (return val)", node->nd_stts);

if (is->body->type == ISEQ_TYPE_METHOD) {
if (splabel) {
add_ensure_iseq(ret, iseq, 1);
ADD_TRACE(ret, line, RUBY_EVENT_RETURN);
ADD_INSN(ret, line, leave);
Expand Down
3 changes: 2 additions & 1 deletion iseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -2419,6 +2419,7 @@ Init_ISeq(void)
{
/* declare ::RubyVM::InstructionSequence */
rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
rb_define_global_const("ISeq", rb_cISeq);
rb_define_method(rb_cISeq, "inspect", iseqw_inspect, 0);
rb_define_method(rb_cISeq, "disasm", iseqw_disasm, 0);
rb_define_method(rb_cISeq, "disassemble", iseqw_disasm, 0);
Expand Down Expand Up @@ -2450,9 +2451,9 @@ Init_ISeq(void)
#if 0 /* TBD */
rb_define_private_method(rb_cISeq, "marshal_dump", iseqw_marshal_dump, 0);
rb_define_private_method(rb_cISeq, "marshal_load", iseqw_marshal_load, 1);
#endif
/* disable this feature because there is no verifier. */
rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1);
#endif
(void)iseq_s_load;

rb_define_singleton_method(rb_cISeq, "compile", iseqw_s_compile, -1);
Expand Down
20 changes: 20 additions & 0 deletions test/ruby/test_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,26 @@ def test_parenthesised_statement_argument
assert_valid_syntax("foo (bar rescue nil)")
end

def test_return_toplevel
feature4840 = '[ruby-core:36785] [Feature #4840]'
code = <<~'CODE'
return; raise
begin return; rescue SystemExit; exit false; end
begin return; ensure exit false; end
begin ensure return; end
begin raise; ensure; return; end
begin raise; rescue; return; end
return false; raise
return 1; raise
CODE
all_assertions(feature4840) do |a|
code.each_line do |s|
s.chomp!
a.for(s) {assert_ruby_status([], s)}
end
end
end

private

def not_label(x) @result = x; @not_label ||= nil end
Expand Down