ruby-changes:39363
From: nobu <ko1@a...>
Date: Thu, 30 Jul 2015 13:20:29 +0900 (JST)
Subject: [ruby-changes:39363] nobu:r51444 (trunk): variable.c: Module#deprecate_constant
nobu 2015-07-30 13:20:00 +0900 (Thu, 30 Jul 2015) New Revision: 51444 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51444 Log: variable.c: Module#deprecate_constant * variable.c (rb_const_get_0): warn deprecated constant reference. * variable.c (rb_mod_deprecate_constant): mark constants to be warned as deprecated. [Feature #11398] Modified files: trunk/ChangeLog trunk/NEWS trunk/constant.h trunk/lib/timeout.rb trunk/object.c trunk/test/ruby/test_module.rb trunk/variable.c Index: ChangeLog =================================================================== --- ChangeLog (revision 51443) +++ ChangeLog (revision 51444) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Jul 30 13:19:54 2015 Nobuyoshi Nakada <nobu@r...> + + * variable.c (rb_const_get_0): warn deprecated constant reference. + + * variable.c (rb_mod_deprecate_constant): mark constants to be + warned as deprecated. [Feature #11398] + Thu Jul 30 11:53:54 2015 Nobuyoshi Nakada <nobu@r...> * thread.c (rb_thread_s_handle_interrupt): make identical hash, Index: variable.c =================================================================== --- variable.c (revision 51443) +++ variable.c (revision 51444) @@ -2149,6 +2149,15 @@ rb_const_get_0(VALUE klass, ID id, int e https://github.com/ruby/ruby/blob/trunk/variable.c#L2149 rb_name_error(id, "private constant %"PRIsVALUE"::%"PRIsVALUE" referenced", rb_class_name(klass), QUOTE_ID(id)); } + if (RB_CONST_DEPRECATED_P(ce)) { + if (klass == rb_cObject) { + rb_warn("constant ::%"PRIsVALUE" is deprecated", QUOTE_ID(id)); + } + else { + rb_warn("constant %"PRIsVALUE"::%"PRIsVALUE" is deprecated", + rb_class_name(klass), QUOTE_ID(id)); + } + } value = ce->value; if (value == Qundef) { if (am == tmp) break; @@ -2576,7 +2585,7 @@ rb_define_global_const(const char *name, https://github.com/ruby/ruby/blob/trunk/variable.c#L2585 } static void -set_const_visibility(VALUE mod, int argc, const VALUE *argv, rb_const_flag_t flag) +set_const_visibility(VALUE mod, int argc, const VALUE *argv, rb_const_flag_t flag, rb_const_flag_t mask) { int i; rb_const_entry_t *ce; @@ -2600,7 +2609,8 @@ set_const_visibility(VALUE mod, int argc https://github.com/ruby/ruby/blob/trunk/variable.c#L2609 rb_class_name(mod), QUOTE(val)); } if ((ce = rb_const_lookup(mod, id))) { - ce->flag = flag; + ce->flag &= ~mask; + ce->flag |= flag; } else { if (i > 0) { @@ -2623,7 +2633,7 @@ set_const_visibility(VALUE mod, int argc https://github.com/ruby/ruby/blob/trunk/variable.c#L2633 VALUE rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj) { - set_const_visibility(obj, argc, argv, CONST_PRIVATE); + set_const_visibility(obj, argc, argv, CONST_PRIVATE, CONST_VISIBILITY_MASK); return obj; } @@ -2637,7 +2647,14 @@ rb_mod_private_constant(int argc, const https://github.com/ruby/ruby/blob/trunk/variable.c#L2647 VALUE rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj) { - set_const_visibility(obj, argc, argv, CONST_PUBLIC); + set_const_visibility(obj, argc, argv, CONST_PUBLIC, CONST_VISIBILITY_MASK); + return obj; +} + +VALUE +rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj) +{ + set_const_visibility(obj, argc, argv, CONST_DEPRECATED, CONST_DEPRECATED); return obj; } Index: object.c =================================================================== --- object.c (revision 51443) +++ object.c (revision 51444) @@ -3414,6 +3414,7 @@ Init_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L3414 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1); rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */ rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */ + rb_define_method(rb_cModule, "deprecate_constant", rb_mod_deprecate_constant, -1); /* in variable.c */ rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0); rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0); Index: lib/timeout.rb =================================================================== --- lib/timeout.rb (revision 51443) +++ lib/timeout.rb (revision 51444) @@ -120,3 +120,6 @@ end https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L120 # Another name for Timeout::Error, defined for backwards compatibility with # earlier versions of timeout.rb. TimeoutError = Timeout::Error +class Object + deprecate_constant :TimeoutError +end Index: NEWS =================================================================== --- NEWS (revision 51443) +++ NEWS (revision 51444) @@ -44,6 +44,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L44 this parameter is bitwise-ORed to oflags generated by normal mode argument. [Feature #11253] +* Module + * Module#deprecate_constant [Feature #11398] + * NameError * NameError#receiver is added to take the receiver object. [Feature #10881] Index: constant.h =================================================================== --- constant.h (revision 51443) +++ constant.h (revision 51444) @@ -12,15 +12,21 @@ https://github.com/ruby/ruby/blob/trunk/constant.h#L12 #define CONSTANT_H typedef enum { + CONST_DEPRECATED = 0x100, + + CONST_VISIBILITY_MASK = 0xff, CONST_PUBLIC = 0x00, CONST_PRIVATE, CONST_VISIBILITY_MAX } rb_const_flag_t; #define RB_CONST_PRIVATE_P(ce) \ - ((ce)->flag == CONST_PRIVATE) + (((ce)->flag & CONST_VISIBILITY_MASK) == CONST_PRIVATE) #define RB_CONST_PUBLIC_P(ce) \ - ((ce)->flag == CONST_PUBLIC) + (((ce)->flag & CONST_VISIBILITY_MASK) == CONST_PUBLIC) + +#define RB_CONST_DEPRECATED_P(ce) \ + ((ce)->flag & CONST_DEPRECATED) typedef struct rb_const_entry_struct { rb_const_flag_t flag; @@ -31,6 +37,7 @@ typedef struct rb_const_entry_struct { https://github.com/ruby/ruby/blob/trunk/constant.h#L37 VALUE rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj); VALUE rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj); +VALUE rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj); void rb_free_const_table(st_table *tbl); VALUE rb_public_const_get(VALUE klass, ID id); VALUE rb_public_const_get_at(VALUE klass, ID id); Index: test/ruby/test_module.rb =================================================================== --- test/ruby/test_module.rb (revision 51443) +++ test/ruby/test_module.rb (revision 51444) @@ -1360,6 +1360,13 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L1360 assert_equal("foo", c::FOO) end + def test_deprecate_constant + c = Class.new + c.const_set(:FOO, "foo") + c.deprecate_constant(:FOO) + assert_warn(/deprecated/) {c::FOO} + end + def test_constants_with_private_constant assert_not_include(::TestModule.constants, :PrivateClass) end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/