Project

General

Profile

Feature #14382 ยป 0001-Make-public-access-of-a-private-constant-call-const_.patch

jeremyevans0 (Jeremy Evans), 01/22/2018 03:47 AM

View differences:

test/ruby/test_module.rb
1391 1391
    RUBY
1392 1392
  end
1393 1393

  
1394
  def test_private_constant_const_missing
1395
    c = Class.new
1396
    c.const_set(:FOO, "foo")
1397
    c.private_constant(:FOO)
1398
    class << c
1399
      attr_reader :const_missing_arg
1400
      def const_missing(name)
1401
        @const_missing_arg = name
1402
	name == :FOO ? const_get(:FOO) : super
1403
      end
1404
    end
1405
    assert_equal("foo", c::FOO)
1406
    assert_equal(:FOO, c.const_missing_arg)
1407
    assert_raise(NameError) { c::BAR }
1408
  end
1409

  
1394 1410
  class PrivateClass
1395 1411
  end
1396 1412
  private_constant :PrivateClass
variable.c
1794 1794
rb_mod_const_missing(VALUE klass, VALUE name)
1795 1795
{
1796 1796
    rb_vm_pop_cfunc_frame();
1797
    if (RB_TYPE_P(name, RUBY_T_SYMBOL)) {
1798
	rb_const_search(klass, SYM2ID(name), 0, 1, 2);
1799
    }
1797 1800
    uninitialized_constant(klass, name);
1798 1801

  
1799 1802
    UNREACHABLE;
......
2247 2250

  
2248 2251
	while ((ce = rb_const_lookup(tmp, id))) {
2249 2252
	    if (visibility && RB_CONST_PRIVATE_P(ce)) {
2250
		rb_name_err_raise("private constant %2$s::%1$s referenced",
2251
				  tmp, ID2SYM(id));
2253
		if (visibility == 2) {
2254
		    rb_name_err_raise("private constant %2$s::%1$s referenced",
2255
				      tmp, ID2SYM(id));
2256
		} else {
2257
		    return rb_const_missing(klass, ID2SYM(id));
2258
		}
2252 2259
	    }
2253 2260
	    rb_const_warn_if_deprecated(ce, tmp, id);
2254 2261
	    value = ce->value;
2255
-