Skip to content

Commit ef69738

Browse files
authored
Treat return in block in class/module as LocalJumpError (ruby#2511)
return directly in class/module is an error, so return in proc in class/module should also be an error. I believe the previous behavior was an unintentional oversight during the addition of top-level return in 2.4.
1 parent 9759e3c commit ef69738

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

spec/ruby/language/return_spec.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,17 +443,18 @@ class ReturnSpecs::A
443443
end
444444

445445
describe "within a block within a class" do
446-
it "is allowed" do
447-
File.write(@filename, <<-END_OF_CODE)
448-
class ReturnSpecs::A
449-
ScratchPad << "before return"
450-
1.times { return }
451-
ScratchPad << "after return"
452-
end
453-
END_OF_CODE
446+
ruby_version_is "2.7" do
447+
it "is not allowed" do
448+
File.write(@filename, <<-END_OF_CODE)
449+
class ReturnSpecs::A
450+
ScratchPad << "before return"
451+
1.times { return }
452+
ScratchPad << "after return"
453+
end
454+
END_OF_CODE
454455

455-
load @filename
456-
ScratchPad.recorded.should == ["before return"]
456+
-> { load @filename }.should raise_error(LocalJumpError)
457+
end
457458
end
458459
end
459460

test/ruby/test_syntax.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,10 @@ def test_return_toplevel_with_argument
12721272
assert_warn(/argument of top-level return is ignored/) {eval("return 1")}
12731273
end
12741274

1275+
def test_return_in_proc_in_class
1276+
assert_in_out_err(['-e', 'class TestSyntax; proc{ return }.call; end'], "", [], /^-e:1:.*unexpected return \(LocalJumpError\)/)
1277+
end
1278+
12751279
def test_syntax_error_in_rescue
12761280
bug12613 = '[ruby-core:76531] [Bug #12613]'
12771281
assert_syntax_error("#{<<-"begin;"}\n#{<<-"end;"}", /Invalid retry/, bug12613)

vm_insnhelper.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,10 @@ vm_throw_start(const rb_execution_context_t *ec, rb_control_frame_t *const reg_c
12581258
switch (escape_cfp->iseq->body->type) {
12591259
case ISEQ_TYPE_TOP:
12601260
case ISEQ_TYPE_MAIN:
1261-
if (toplevel) goto valid_return;
1261+
if (toplevel) {
1262+
if (in_class_frame) goto unexpected_return;
1263+
goto valid_return;
1264+
}
12621265
break;
12631266
case ISEQ_TYPE_EVAL:
12641267
case ISEQ_TYPE_CLASS:
@@ -1276,6 +1279,7 @@ vm_throw_start(const rb_execution_context_t *ec, rb_control_frame_t *const reg_c
12761279

12771280
escape_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(escape_cfp);
12781281
}
1282+
unexpected_return:;
12791283
rb_vm_localjump_error("unexpected return", throwobj, TAG_RETURN);
12801284

12811285
valid_return:;

0 commit comments

Comments
 (0)