Project

General

Profile

Actions

Bug #19779

closed

`eval "return"` at top level raises `LocalJumpError`

Bug #19779: `eval "return"` at top level raises `LocalJumpError`

Added by andrykonchin (Andrew Konchin) over 2 years ago. Updated almost 2 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:114251]

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 andrykonchin (Andrew Konchin) over 2 years ago Actions #1

  • ruby -v set to 3.2.1

Updated by matz (Yukihiro Matsumoto) about 2 years ago Actions #2 [ruby-core:114478]

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 mame (Yusuke Endoh) almost 2 years ago Actions #4 [ruby-core:115639]

@jeremyevans0 (Jeremy Evans) Matz said go ahead in today's dev meeting.

Updated by jeremyevans (Jeremy Evans) almost 2 years ago Actions #5

  • 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]

Actions

Also available in: PDF Atom