ruby-changes:51486
From: nobu <ko1@a...>
Date: Tue, 19 Jun 2018 17:15:56 +0900 (JST)
Subject: [ruby-changes:51486] nobu:r63696 (trunk): variable.c: fix receiver on private constant
nobu 2018-06-19 17:15:52 +0900 (Tue, 19 Jun 2018) New Revision: 63696 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63696 Log: variable.c: fix receiver on private constant * variable.c (rb_const_search): fix NameError :receiver attribute on private constant, should raise with the included module, not the ICLASS. Modified files: trunk/test/ruby/test_module.rb trunk/variable.c Index: variable.c =================================================================== --- variable.c (revision 63695) +++ variable.c (revision 63696) @@ -2362,6 +2362,7 @@ rb_const_search(VALUE klass, ID id, int https://github.com/ruby/ruby/blob/trunk/variable.c#L2362 while ((ce = rb_const_lookup(tmp, id))) { if (visibility && RB_CONST_PRIVATE_P(ce)) { + if (BUILTIN_TYPE(tmp) == T_ICLASS) tmp = RBASIC(tmp)->klass; rb_name_err_raise("private constant %2$s::%1$s referenced", tmp, ID2SYM(id)); } Index: test/ruby/test_module.rb =================================================================== --- test/ruby/test_module.rb (revision 63695) +++ test/ruby/test_module.rb (revision 63696) @@ -1349,21 +1349,55 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L1349 assert_raise(ArgumentError, bug8540) { c.new.send :foo= } end - def test_private_constant + def test_private_constant_in_class c = Class.new c.const_set(:FOO, "foo") assert_equal("foo", c::FOO) c.private_constant(:FOO) - assert_raise(NameError) { c::FOO } + e = assert_raise(NameError) {c::FOO} + assert_equal(c, e.receiver) + assert_equal(:FOO, e.name) assert_equal("foo", c.class_eval("FOO")) assert_equal("foo", c.const_get("FOO")) $VERBOSE, verbose = nil, $VERBOSE c.const_set(:FOO, "foo") $VERBOSE = verbose - assert_raise(NameError) { c::FOO } - assert_raise_with_message(NameError, /#{c}::FOO/) do + e = assert_raise(NameError) {c::FOO} + assert_equal(c, e.receiver) + assert_equal(:FOO, e.name) + e = assert_raise_with_message(NameError, /#{c}::FOO/) do Class.new(c)::FOO end + assert_equal(c, e.receiver) + assert_equal(:FOO, e.name) + end + + def test_private_constant_in_module + m = Module.new + m.const_set(:FOO, "foo") + assert_equal("foo", m::FOO) + m.private_constant(:FOO) + e = assert_raise(NameError) {m::FOO} + assert_equal(m, e.receiver) + assert_equal(:FOO, e.name) + assert_equal("foo", m.class_eval("FOO")) + assert_equal("foo", m.const_get("FOO")) + $VERBOSE, verbose = nil, $VERBOSE + m.const_set(:FOO, "foo") + $VERBOSE = verbose + e = assert_raise(NameError) {m::FOO} + assert_equal(m, e.receiver) + assert_equal(:FOO, e.name) + e = assert_raise(NameError, /#{m}::FOO/) do + Module.new {include m}::FOO + end + assert_equal(m, e.receiver) + assert_equal(:FOO, e.name) + e = assert_raise(NameError, /#{m}::FOO/) do + Class.new {include m}::FOO + end + assert_equal(m, e.receiver) + assert_equal(:FOO, e.name) end def test_private_constant2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/