ruby-changes:21126
From: nobu <ko1@a...>
Date: Sun, 4 Sep 2011 00:12:04 +0900 (JST)
Subject: [ruby-changes:21126] nobu:r33175 (trunk): * variable.c (rb_const_set): show the previous definition
nobu 2011-09-04 00:11:53 +0900 (Sun, 04 Sep 2011) New Revision: 33175 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=33175 Log: * variable.c (rb_const_set): show the previous definition location. [EXPERIMENTAL] Modified files: trunk/ChangeLog trunk/constant.h trunk/gc.c trunk/internal.h trunk/test/ruby/test_const.rb trunk/variable.c trunk/vm.c Index: ChangeLog =================================================================== --- ChangeLog (revision 33174) +++ ChangeLog (revision 33175) @@ -1,3 +1,8 @@ +Sun Sep 4 00:11:49 2011 Nobuyoshi Nakada <nobu@r...> + + * variable.c (rb_const_set): show the previous definition + location. [EXPERIMENTAL] + Sat Sep 3 23:56:24 2011 Nobuyoshi Nakada <nobu@r...> * configure.in (sizeof_struct_dirent_too_small): check if struct Index: variable.c =================================================================== --- variable.c (revision 33174) +++ variable.c (revision 33175) @@ -2057,8 +2057,13 @@ autoload_delete(klass, id); } else { + const char *name = rb_id2name(id); visibility = ce->flag; - rb_warn("already initialized constant %s", rb_id2name(id)); + rb_warn("already initialized constant %s", name); + if (!NIL_P(ce->file) && ce->line) { + rb_compile_warn(RSTRING_PTR(ce->file), ce->line, + "previous definition of %s was here", name); + } } } } @@ -2068,6 +2073,8 @@ ce = ALLOC(rb_const_entry_t); ce->flag = (rb_const_flag_t)visibility; ce->value = val; + ce->file = rb_sourcefilename(); + ce->line = rb_sourceline(); st_insert(RCLASS_CONST_TBL(klass), (st_data_t)id, (st_data_t)ce); } Index: gc.c =================================================================== --- gc.c (revision 33174) +++ gc.c (revision 33175) @@ -1569,6 +1569,7 @@ { struct mark_tbl_arg *arg = (void*)data; gc_mark(arg->objspace, ce->value, arg->lev); + gc_mark(arg->objspace, ce->file, arg->lev); return ST_CONTINUE; } Index: internal.h =================================================================== --- internal.h (revision 33174) +++ internal.h (revision 33175) @@ -195,6 +195,7 @@ void rb_vm_inc_const_missing_count(void); void rb_thread_mark(void *th); const void **rb_vm_get_insns_address_table(void); +VALUE rb_sourcefilename(void); /* vm_dump.c */ void rb_vm_bugreport(void); Index: vm.c =================================================================== --- vm.c (revision 33174) +++ vm.c (revision 33175) @@ -829,6 +829,20 @@ return rb_ary_reverse(ary); } +VALUE +rb_sourcefilename(void) +{ + rb_thread_t *th = GET_THREAD(); + rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp); + + if (cfp) { + return cfp->iseq->filename; + } + else { + return Qnil; + } +} + const char * rb_sourcefile(void) { Index: test/ruby/test_const.rb =================================================================== --- test/ruby/test_const.rb (revision 33174) +++ test/ruby/test_const.rb (revision 33175) @@ -45,4 +45,13 @@ assert defined?(TEST4) assert_equal 8, TEST4 end + + def test_redefinition + c = Class.new + c.const_set(:X, 1) + assert_output(nil, <<-WARNING) {c.const_set(:X, 2)} +#{__FILE__}:#{__LINE__-1}: warning: already initialized constant X +#{__FILE__}:#{__LINE__-3}: warning: previous definition of X was here +WARNING + end end Index: constant.h =================================================================== --- constant.h (revision 33174) +++ constant.h (revision 33175) @@ -19,6 +19,8 @@ typedef struct rb_const_entry_struct { rb_const_flag_t flag; VALUE value; /* should be mark */ + VALUE file; + int line; } rb_const_entry_t; VALUE rb_mod_private_constant(int argc, VALUE *argv, VALUE obj); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/