Bug #19779
closed`eval "return"` at top level raises `LocalJumpError`
Description
Wondering whether it's intentional behaviour. It seems it's the only difference I've noticed between calling return
with and without eval
:
ruby -e 'return'
ruby -e 'eval "return"'
(eval):1:in `<main>': unexpected return (LocalJumpError)
from -e:1:in `eval'
from -e:1:in `<main>'
Updated by matz (Yukihiro Matsumoto) about 1 year ago
Since we allow top-level return (from 2.4), it is nice to allow this too. But to be frank, it is low priority.
Matz.
Updated by jeremyevans0 (Jeremy Evans) about 1 year ago
I submitted a pull request to fix this: https://github.com/ruby/ruby/pull/8766
Updated by mame (Yusuke Endoh) 11 months ago
@jeremyevans0 (Jeremy Evans) Matz said go ahead in today's dev meeting.
Updated by jeremyevans (Jeremy Evans) 11 months ago
- Status changed from Open to Closed
Applied in changeset git|3a88de3ca73052809f5c0bfb4ef8cd435b29ae5f.
Support eval "return" at toplevel
Since Ruby 2.4, return
is supported at toplevel. This makes eval "return"
also supported at toplevel.
This mostly uses the same tests as direct return
at toplevel, with a couple
differences:
END {return if false}
is a SyntaxError, but END {eval "return" if false}
is not an error since the eval is never executed. END {return}
is a
SyntaxError, but END {eval "return"}
is a LocalJumpError.
The following is a SyntaxError:
class X
nil&defined?0--begin e=no_method_error(); return; 0;end
end
However, the following is not, because the eval is never executed:
class X
nil&defined?0--begin e=no_method_error(); eval "return"; 0;end
end
Fixes [Bug #19779]