Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa97410

Browse files
committed
Warn if using return at top-level with an argument
Fixes [Bug #14062]
1 parent c2428b8 commit aa97410

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

compile.c

+3
Original file line numberDiff line numberDiff line change
@@ -6372,6 +6372,9 @@ compile_return(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
63726372
switch (t) {
63736373
case ISEQ_TYPE_TOP:
63746374
case ISEQ_TYPE_MAIN:
6375+
if (retval) {
6376+
rb_warn("argument of top-level return is ignored");
6377+
}
63756378
if (is == iseq) {
63766379
/* plain top-level, leave directly */
63776380
type = ISEQ_TYPE_METHOD;

spec/ruby/language/return_spec.rb

+19-6
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,26 @@ class ReturnSpecs::A
484484
end
485485

486486
describe "return with argument" do
487-
# https://bugs.ruby-lang.org/issues/14062
488-
it "does not affect exit status" do
489-
ruby_exe(<<-END_OF_CODE).should == ""
490-
return 10
491-
END_OF_CODE
487+
ruby_version_is ""..."2.7" do
488+
it "does not affect exit status" do
489+
ruby_exe(<<-END_OF_CODE).should == ""
490+
return 10
491+
END_OF_CODE
492+
493+
$?.exitstatus.should == 0
494+
end
495+
end
496+
497+
ruby_version_is "2.7" do
498+
it "warns but does not affect exit status" do
499+
ruby_exe(<<-END_OF_CODE).should == "-e: warning: argument of top-level return is ignored\n"
500+
$stderr.reopen($stdout)
501+
system(ENV['RUBY_EXE'], '-e', 'return 10')
502+
exit($?.exitstatus)
503+
END_OF_CODE
492504

493-
$?.exitstatus.should == 0
505+
$?.exitstatus.should == 0
506+
end
494507
end
495508
end
496509
end

test/ruby/test_syntax.rb

+4
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,10 @@ def test_return_toplevel
11951195
end
11961196
end
11971197

1198+
def test_return_toplevel_with_argument
1199+
assert_warn(/argument of top-level return is ignored/) {eval("return 1")}
1200+
end
1201+
11981202
def test_syntax_error_in_rescue
11991203
bug12613 = '[ruby-core:76531] [Bug #12613]'
12001204
assert_syntax_error("#{<<-"begin;"}\n#{<<-"end;"}", /Invalid retry/, bug12613)

0 commit comments

Comments
 (0)