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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51444 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-07-30 04:20:00 +00:00
Родитель c3f6e4ea07
Коммит 83cd51e3fe
7 изменённых файлов: 51 добавлений и 6 удалений

Просмотреть файл

@ -1,3 +1,10 @@
Thu Jul 30 13:19:54 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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@ruby-lang.org>
* thread.c (rb_thread_s_handle_interrupt): make identical hash,

3
NEWS
Просмотреть файл

@ -44,6 +44,9 @@ with all sufficient information, see the ChangeLog file.
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]

Просмотреть файл

@ -12,15 +12,21 @@
#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 {
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);

Просмотреть файл

@ -120,3 +120,6 @@ end
# Another name for Timeout::Error, defined for backwards compatibility with
# earlier versions of timeout.rb.
TimeoutError = Timeout::Error
class Object
deprecate_constant :TimeoutError
end

Просмотреть файл

@ -3414,6 +3414,7 @@ Init_Object(void)
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);

Просмотреть файл

@ -1360,6 +1360,13 @@ class TestModule < Test::Unit::TestCase
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

Просмотреть файл

@ -2149,6 +2149,15 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
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, VALUE val)
}
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, const VALUE *argv, rb_const_flag_t fla
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, const VALUE *argv, rb_const_flag_t fla
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 VALUE *argv, VALUE obj)
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;
}