ruby-changes:58283
From: Jeremy <ko1@a...>
Date: Thu, 17 Oct 2019 04:50:59 +0900 (JST)
Subject: [ruby-changes:58283] 2993b24a1e (master): Warn for calling public/protected/private/module_function without arguments inside method
https://git.ruby-lang.org/ruby.git/commit/?id=2993b24a1e From 2993b24a1ecc5fa3cc9f140bfd80669c3a3b7b9c Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Sat, 24 Aug 2019 17:11:06 -0700 Subject: Warn for calling public/protected/private/module_function without arguments inside method Calling these methods without an argument does not have the desired effect inside a method. Fixes [Bug #13249] diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb index 6960ab5..407b0aa 100644 --- a/test/ruby/test_class.rb +++ b/test/ruby/test_class.rb @@ -131,6 +131,48 @@ class TestClass < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_class.rb#L131 [:module_function, :extend_object, :append_features, :prepend_features]) end + def test_visibility_inside_method + assert_warn(/calling private without arguments inside a method may not have the intended effect/, '[ruby-core:79751]') do + Class.new do + def self.foo + private + end + foo + end + end + + assert_warn(/calling protected without arguments inside a method may not have the intended effect/, '[ruby-core:79751]') do + Class.new do + def self.foo + protected + end + foo + end + end + + assert_warn(/calling public without arguments inside a method may not have the intended effect/, '[ruby-core:79751]') do + Class.new do + def self.foo + public + end + foo + end + end + + assert_warn(/calling private without arguments inside a method may not have the intended effect/, '[ruby-core:79751]') do + Class.new do + class << self + alias priv private + end + + def self.foo + priv + end + foo + end + end + end + def test_method_redefinition feature2155 = '[ruby-dev:39400]' diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index 8b95ece..79f7965 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -1394,6 +1394,17 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L1394 assert_match(/: warning: previous definition of foo/, stderr) end + def test_module_function_inside_method + assert_warn(/calling module_function without arguments inside a method may not have the intended effect/, '[ruby-core:79751]') do + Module.new do + def self.foo + module_function + end + foo + end + end + end + def test_protected_singleton_method klass = Class.new x = klass.new diff --git a/vm_method.c b/vm_method.c index 6465798..5ee1773 100644 --- a/vm_method.c +++ b/vm_method.c @@ -1127,8 +1127,20 @@ rb_scope_visibility_set(rb_method_visibility_t visi) https://github.com/ruby/ruby/blob/trunk/vm_method.c#L1127 } static void +scope_visibility_check(void) +{ + /* Check for public/protected/private/module_function called inside a method */ + rb_control_frame_t *cfp = rb_current_execution_context()->cfp+1; + if (cfp && cfp->iseq && cfp->iseq->body->type == ISEQ_TYPE_METHOD) { + rb_warn("calling %s without arguments inside a method may not have the intended effect", + rb_id2name(rb_frame_this_func())); + } +} + +static void rb_scope_module_func_set(void) { + scope_visibility_check(); vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE); } @@ -1663,7 +1675,8 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/vm_method.c#L1675 set_visibility(int argc, const VALUE *argv, VALUE module, rb_method_visibility_t visi) { if (argc == 0) { - rb_scope_visibility_set(visi); + scope_visibility_check(); + rb_scope_visibility_set(visi); } else { set_method_visibility(module, argc, argv, visi); -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/