[ruby-core:118670] [Ruby master Bug#20647] Invalid return in class/module body from within singleton class

Issue #20647 has been reported by kddnewton (Kevin Newton). ---------------------------------------- Bug #20647: Invalid return in class/module body from within singleton class https://bugs.ruby-lang.org/issues/20647 * Author: kddnewton (Kevin Newton) * Status: Open * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Currently, Ruby will raise a syntax error for invalid returns within class and module bodies, but skips it for singleton classes. I think we should be able to add this same error for singleton classes, as I believe these always raise a LocalJumpError. ```ruby class << Object return end ``` -- https://bugs.ruby-lang.org/

Issue #20647 has been updated by zverok (Victor Shepelev). That’s actually interesting, because return in the middle of the class body, while might be considered questionable, is also meaningful. I think making it a syntax error in normal classes was (probably?) a decision from before 2.4 (when top-level `return` had no useful semantics). I can imagine code like ```ruby class MyService # some generic stuff return if RUBY_PLATFORM.match?(/windows/i) # some Unix-only stuff end ``` ...and even more so, the same in the middle of a singleton class definition in some DSL method :shrug: ---------------------------------------- Bug #20647: Invalid return in class/module body from within singleton class https://bugs.ruby-lang.org/issues/20647#change-109203 * Author: kddnewton (Kevin Newton) * Status: Open * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Currently, Ruby will raise a syntax error for invalid returns within class and module bodies, but skips it for singleton classes. I think we should be able to add this same error for singleton classes, as I believe these always raise a LocalJumpError. ```ruby class << Object return end ``` -- https://bugs.ruby-lang.org/

Issue #20647 has been updated by nobu (Nobuyoshi Nakada). https://github.com/ruby/ruby/pull/11234 ---------------------------------------- Bug #20647: Invalid return in class/module body from within singleton class https://bugs.ruby-lang.org/issues/20647#change-109206 * Author: kddnewton (Kevin Newton) * Status: Open * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Currently, Ruby will raise a syntax error for invalid returns within class and module bodies, but skips it for singleton classes. I think we should be able to add this same error for singleton classes, as I believe these always raise a LocalJumpError. ```ruby class << Object return end ``` -- https://bugs.ruby-lang.org/
participants (3)
-
kddnewton (Kevin Newton)
-
nobu (Nobuyoshi Nakada)
-
zverok (Victor Shepelev)