Project

General

Profile

Bug #14062

Top-level return allows an argument

Added by Eregon (Benoit Daloze) about 2 years ago. Updated 4 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.5.0dev (2017-10-26 trunk 60450) [x86_64-linux]
[ruby-core:83590]

Description

puts "Here"
return 42 # or :foo, or any value
ruby test.rb
Here

Should it be a SyntaxError, as mentioned in https://bugs.ruby-lang.org/issues/4840#note-24 ?
It seems confusing to accept it silently, as one could expect the exit code to be affected by it (that should not be the case imho).

Discovered in https://github.com/ruby/spec/pull/530


Files

top-level-return-warn-argument.patch (1.32 KB) top-level-return-warn-argument.patch jeremyevans0 (Jeremy Evans), 07/26/2019 10:15 PM

Related issues

Related to Ruby master - Feature #4840: Allow returning from requireClosedActions

Associated revisions

Revision aa97410b
Added by jeremyevans (Jeremy Evans) 4 months ago

Warn if using return at top-level with an argument

Fixes [Bug #14062]

History

Updated by duerst (Martin Dürst) about 2 years ago

Wouldn't this be equivalent to C's return statement in main()?

It is used to tell the outer process (usually a shell) about the success (0) or failure (anything else than 0) of the program.

In the average shell, you should be able to test it with e.g.

ruby test.rb && echo "Previous process was successful."

which would not print the "Previous process was successful." text because the return value was something else than 0.

Updated by Eregon (Benoit Daloze) about 2 years ago

Currently the argument is ignored.
And I think it would make little sense in a file loaded by #require to affect the global exit status if it does a "return 1".
"exit 1" can be used for that, top-level return is to avoid loading anything further in the file when it's not needed in my understanding.

#3

Updated by matz (Yukihiro Matsumoto) almost 2 years ago

Updated by matz (Yukihiro Matsumoto) almost 2 years ago

I am against making it a syntax error. Adding warnings is OK (but maybe we can rely on Rubocop etc. to detect them).

Matz.

Updated by jeremyevans0 (Jeremy Evans) 5 months ago

Attached is a patch that adds a warning when using a top-level return with an argument.

#6

Updated by Eregon (Benoit Daloze) 5 months ago

  • Description updated (diff)

Updated by Eregon (Benoit Daloze) 5 months ago

jeremyevans0 (Jeremy Evans) wrote:

Attached is a patch that adds a warning when using a top-level return with an argument.

Thank you, it looks good to me.
Although, I would suggest to change the warning message to one of these:

argument of top-level return is ignored
argument of return at top-level is ignored

Could you commit it?

#8

Updated by jeremyevans (Jeremy Evans) 4 months ago

  • Status changed from Open to Closed

Applied in changeset git|aa97410b0a85cb4ceb956ab943b5eee92a128411.


Warn if using return at top-level with an argument

Fixes [Bug #14062]

Also available in: Atom PDF