Project

General

Profile

Actions

Bug #19779

closed

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

Added by andrykonchin (Andrew Konchin) 10 months ago. Updated 5 months 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>'
Actions #1

Updated by andrykonchin (Andrew Konchin) 10 months ago

  • ruby -v set to 3.2.1

Updated by matz (Yukihiro Matsumoto) 9 months 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) 6 months ago

I submitted a pull request to fix this: https://github.com/ruby/ruby/pull/8766

Updated by mame (Yusuke Endoh) 5 months ago

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

Actions #5

Updated by jeremyevans (Jeremy Evans) 5 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]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like1Like0Like0