ruby-changes:44028
From: shugo <ko1@a...>
Date: Thu, 8 Sep 2016 13:44:57 +0900 (JST)
Subject: [ruby-changes:44028] shugo:r56101 (trunk): * insns.def (setclassvariable, setconstant): warn when self is a
shugo 2016-09-08 13:44:51 +0900 (Thu, 08 Sep 2016) New Revision: 56101 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56101 Log: * insns.def (setclassvariable, setconstant): warn when self is a refinement. [Bug #10103] [ruby-core:64143] Modified files: trunk/ChangeLog trunk/insns.def trunk/test/ruby/test_refinement.rb trunk/vm_insnhelper.c Index: ChangeLog =================================================================== --- ChangeLog (revision 56100) +++ ChangeLog (revision 56101) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Sep 8 13:41:46 2016 Shugo Maeda <shugo@r...> + + * insns.def (setclassvariable, setconstant): warn when self is a + refinement. [Bug #10103] [ruby-core:64143] + Thu Sep 8 11:29:00 2016 Kenta Murata <mrkn@m...> * hash.c (rb_hash_transform_values, rb_hash_transform_values_bang): Index: insns.def =================================================================== --- insns.def (revision 56100) +++ insns.def (revision 56101) @@ -173,6 +173,7 @@ setclassvariable https://github.com/ruby/ruby/blob/trunk/insns.def#L173 (VALUE val) () { + vm_ensure_not_refinement_module(GET_SELF()); rb_cvar_set(vm_get_cvar_base(rb_vm_get_cref(GET_EP()), GET_CFP()), id, val); } @@ -217,6 +218,7 @@ setconstant https://github.com/ruby/ruby/blob/trunk/insns.def#L218 () { vm_check_if_namespace(cbase); + vm_ensure_not_refinement_module(GET_SELF()); rb_const_set(cbase, id, val); } Index: vm_insnhelper.c =================================================================== --- vm_insnhelper.c (revision 56100) +++ vm_insnhelper.c (revision 56101) @@ -733,6 +733,14 @@ vm_check_if_namespace(VALUE klass) https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L733 } } +static inline void +vm_ensure_not_refinement_module(VALUE self) +{ + if (RB_TYPE_P(self, T_MODULE) && FL_TEST(self, RMODULE_IS_REFINEMENT)) { + rb_warn("not defined at the refinement, but at the outer class/module"); + } +} + static inline VALUE vm_get_iclass(rb_control_frame_t *cfp, VALUE klass) { Index: test/ruby/test_refinement.rb =================================================================== --- test/ruby/test_refinement.rb (revision 56100) +++ test/ruby/test_refinement.rb (revision 56101) @@ -1674,6 +1674,22 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L1674 assert_equal [ref::RefB, ref::RefA], ref::Combined::USED_REFS end + def test_warn_setconst_in_refinmenet + bug10103 = '[ruby-core:64143] [Bug #10103]' + warnings = [ + "-:3: warning: not defined at the refinement, but at the outer class/module", + "-:4: warning: not defined at the refinement, but at the outer class/module" + ] + assert_in_out_err([], <<-INPUT, [], warnings, bug10103) + module M + refine String do + FOO = 123 + @@foo = 456 + end + end + INPUT + end + private def eval_using(mod, s) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/