Skip to content

Toplevel return behavior (2.4) #4761

@headius

Description

@headius

This issue tracks unimplemented top-level return behavior added in Ruby 2.4.

In MRI this feature was tracked in https://bugs.ruby-lang.org/issues/4840

The relevant tests, from test/mri/ruby/test_syntax.rb:test_return_toplevel, are copied here:

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

Each of these lines in a -e should exit the process cleanly. The rescue-based cases expect the return to immediately exit the script body without firing ensures or rescues (more in this later). The remaining cases just check that the return does not execute any more lines in the script.

JRuby never implemented a top-level error when a return occurs outside of a method or block at toplevel, so some of these pass already. However the cases that check for ensures not running fail, since we implement those to the letter of the law in JRuby by using Java try/finally logic.

I am going to open an issue with MRI to clarify whether ensures should fire or not. I believe they should, since they do in all other contexts.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions