[ruby-core:95138] [Ruby master Bug#16181] return from a proc in a module/class body returns out of script. Should be LJE.

From: merch-redmine@...
Date: 2019-09-27 23:00:35 UTC
List: ruby-core #95138
Issue #16181 has been updated by jeremyevans0 (Jeremy Evans).


It looked into this behavior, which started in Ruby 2.4 (when top level return started being allowed).

`return` not being allowed directly in class/module body is a parse error, because the parser knows that it cannot be valid.  However, you cannot have the behavior inside of a block inside of class/module be a parse error, because you do not know how the block will be evaluated:

```ruby
class Foo
  alias proc lambda
  proc { return }.call
end
puts "NEVER SEEN"
```

Similarly, though this operates as a top level return, the warning for top level return with argument does not take effect, because the compiler doesn't know it will operate as a top level return:

```ruby
class Foo
  proc { return 1 }.call # no top level return with argument warning
end
puts "NEVER SEEN"
```

I don't think the compiler has enough information to handle this correctly, because it doesn't know at compilation time whether the block will be executed as a proc or as a lambda.

I do agree that a LocalJumpError is the most appropriate way to handle this.

----------------------------------------
Bug #16181: return from a proc in a module/class body returns out of script.  Should be LJE.
https://bugs.ruby-lang.org/issues/16181#change-81777

* Author: enebo (Thomas Enebo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
return is not allowed from class/module body.  But if we insert a return into a block then we can invoke the block then it returns all the way out of the script.  This has to be accidental behavior doesn't it?  I believe the case below should end up as a LocalJumpError:


```
class Foo
  proc { return }.call
end
puts "NEVER SEEN"
```

This behavior started some time in 2.5 (it used to be an LJE).



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next