[ruby-core:95372] [Ruby master Bug#13249] Access modifiers don't have an effect inside class methods in Ruby >= 2.3
From:
merch-redmine@...
Date:
2019-10-16 19:10:17 UTC
List:
ruby-core #95372
Issue #13249 has been updated by jeremyevans0 (Jeremy Evans).
nobu (Nobuyoshi Nakada) wrote:
> * Is it necessary that check is placed inside the function `vm_cref_set_visibility`?
> What about calling a separate function where `check_method` flag is 1?
I agree, that makes more sense.
> * `rb_frame_callee` returns the called name, that may be an aliased name.
> Is it intentional?
No. It would be better to use `rb_frame_this_func` instead, I think.
Thank you very much for your review. I've added the modified patch as a pull request (https://github.com/ruby/ruby/pull/2562). Assuming it passes CI, I will merge it.
----------------------------------------
Bug #13249: Access modifiers don't have an effect inside class methods in Ruby >= 2.3
https://bugs.ruby-lang.org/issues/13249#change-82080
* Author: abotalov (Andrei Botalov)
* Status: Assigned
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version:
* ruby -v: 2.3.0, 2.4.0
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Simple example:
~~~ ruby
class C
def self.foo
private
def bar
end
end
end
C.foo
C.new.bar
~~~
This code runs fine on Ruby 2.3 and Ruby 2.4. It raises NoMethodError on Ruby 2.2 and prior versions.
I would expect an error to be raised.
Here is some code that actually uses private access modifier inside a class method - https://github.com/evolve75/RubyTree/blob/db48c35b0a3b96e4da473b095cc00e454d8a9996/lib/tree/utils/camel_case_method_handler.rb#L60
By the way, this code raises an error as expected on Ruby 2.3 and Ruby 2.4:
~~~ ruby
class C
def self.foo
private def bar
end
end
end
C.foo
C.new.bar # NoMethodError: private method `bar' called
~~~
---Files--------------------------------
warn-scope-visibility-in-method-13249.patch (4.18 KB)
--
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>