aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--compile.c3
-rw-r--r--spec/ruby/language/return_spec.rb25
-rw-r--r--test/ruby/test_syntax.rb4
3 files changed, 26 insertions, 6 deletions
diff --git a/compile.c b/compile.c
index 7d71f26208..774dadfc79 100644
--- a/compile.c
+++ b/compile.c
@@ -6372,6 +6372,9 @@ compile_return(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
switch (t) {
case ISEQ_TYPE_TOP:
case ISEQ_TYPE_MAIN:
+ if (retval) {
+ rb_warn("argument of top-level return is ignored");
+ }
if (is == iseq) {
/* plain top-level, leave directly */
type = ISEQ_TYPE_METHOD;
diff --git a/spec/ruby/language/return_spec.rb b/spec/ruby/language/return_spec.rb
index 1af88c55cd..27729750f1 100644
--- a/spec/ruby/language/return_spec.rb
+++ b/spec/ruby/language/return_spec.rb
@@ -484,13 +484,26 @@ describe "The return keyword" do
end
describe "return with argument" do
- # https://bugs.ruby-lang.org/issues/14062
- it "does not affect exit status" do
- ruby_exe(<<-END_OF_CODE).should == ""
- return 10
- END_OF_CODE
+ ruby_version_is ""..."2.7" do
+ it "does not affect exit status" do
+ ruby_exe(<<-END_OF_CODE).should == ""
+ return 10
+ END_OF_CODE
+
+ $?.exitstatus.should == 0
+ end
+ end
+
+ ruby_version_is "2.7" do
+ it "warns but does not affect exit status" do
+ ruby_exe(<<-END_OF_CODE).should == "-e: warning: argument of top-level return is ignored\n"
+ $stderr.reopen($stdout)
+ system(ENV['RUBY_EXE'], '-e', 'return 10')
+ exit($?.exitstatus)
+ END_OF_CODE
- $?.exitstatus.should == 0
+ $?.exitstatus.should == 0
+ end
end
end
end
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index c5c3737b30..f0a22903b1 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -1195,6 +1195,10 @@ eom
end
end
+ def test_return_toplevel_with_argument
+ assert_warn(/argument of top-level return is ignored/) {eval("return 1")}
+ end
+
def test_syntax_error_in_rescue
bug12613 = '[ruby-core:76531] [Bug #12613]'
assert_syntax_error("#{<<-"begin;"}\n#{<<-"end;"}", /Invalid retry/, bug12613)