summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-27 13:48:53 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-27 13:48:53 +0000
commit4941bba3c8b5eda08691ebcb14930659897b997c (patch)
tree8a193c122448d7af1d66b4534a31817d57536a15
parent7d9572819bfd3932f206549576fbf931551947a6 (diff)
merge revision(s) 63696: [Backport #14853]
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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@64559 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--test/ruby/test_module.rb42
-rw-r--r--variable.c1
-rw-r--r--version.h2
3 files changed, 40 insertions, 5 deletions
diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb
index d749be4361..89e617195c 100644
--- a/test/ruby/test_module.rb
+++ b/test/ruby/test_module.rb
@@ -1352,21 +1352,55 @@ class TestModule < Test::Unit::TestCase
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
diff --git a/variable.c b/variable.c
index b1080d44ab..77d246d728 100644
--- a/variable.c
+++ b/variable.c
@@ -2304,6 +2304,7 @@ rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility)
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));
}
diff --git a/version.h b/version.h
index 59f7433613..ac9a5e7781 100644
--- a/version.h
+++ b/version.h
@@ -1,6 +1,6 @@
#define RUBY_VERSION "2.4.5"
#define RUBY_RELEASE_DATE "2018-08-27"
-#define RUBY_PATCHLEVEL 316
+#define RUBY_PATCHLEVEL 317
#define RUBY_RELEASE_YEAR 2018
#define RUBY_RELEASE_MONTH 8