# \[Feature:1.9\] location of already initialized constant

**URL:** <https://www.ruby-forum.com/t/feature-1-9-location-of-already-initialized-constant/165274>\
**Category:** Development\
**Created:** [April 11, 2009, 4:41am UTC](https://www.ruby-forum.com/t/feature-1-9-location-of-already-initialized-constant/165274 "2009-04-11T04:41:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9c0ed/32.png) [@nobu](https://www.ruby-forum.com/u/nobu)\
**Post date:** [April 11, 2009, 4:41am UTC](https://www.ruby-forum.com/t/feature-1-9-location-of-already-initialized-constant/165274/1 "2009-04-11T04:41:40Z")

</div>

e$B$J$+$@$G$9!#e(B

e$B=i4|2=:Q$\_Dj?t$N7Y9p$G!"Dj5A$5$l$?\>l=j$rI=\<($9$k$N$O$I$&$G$7$g$&$+!#e(B

$ ./ruby -e X=1 -e X=2  
-e:2: warning: already initialized constant X at -e:1

# Index: class.c

— class.c (revision 23168)  
+++ class.c (working copy)  
@@ -26,4 +26,5 @@ class\_alloc(VALUE flags, VALUE klass)  
RCLASS\_IV\_TBL(obj) = 0;  
RCLASS\_M\_TBL(obj) = 0;

- RCLASS\_LOC\_TBL(obj) = 0;  
RCLASS\_SUPER(obj) = 0;  
RCLASS\_IV\_INDEX\_TBL(obj) = 0;  
Index: gc.c  
===================================================================  
— gc.c (revision 23168)  
+++ gc.c (working copy)  
@@ -1552,4 +1552,5 @@ gc\_mark\_children(rb\_objspace\_t \*objspace  
mark\_tbl(objspace, RCLASS\_M\_TBL(obj), lev);  
mark\_tbl(objspace, RCLASS\_IV\_TBL(obj), lev);
- mark\_tbl(objspace, RCLASS\_LOC\_TBL(obj), lev);  
ptr = RCLASS\_SUPER(obj);  
goto again;  
Index: variable.c  
===================================================================  
— variable.c (revision 23168)  
+++ variable.c (working copy)  
@@ -1512,4 +1512,12 @@  
{  
return rb\_const\_get\_0(klass, id, Qtrue, Qfalse);  
+}
- 

+static void  
+remove\_loc(VALUE mod, ID id)  
+{

- if (RCLASS\_LOC\_TBL(mod)) {
- st\_delete(RCLASS\_LOC\_TBL(mod), &id, 0);
- }  
}

@@ -1545,4 +1553,5 @@ rb\_mod\_remove\_const(VALUE mod, VALUE nam  
val = Qnil;  
}

- remove\_loc(mod, id);  
return val;  
}  
@@ -1712,11 +1721,28 @@ mod\_av\_set(VALUE klass, ID id, VALUE val  
}  
else if (isconst) {

- VALUE value = Qfalse;

- st\_data\_t value = Qfalse;

- 

```
 if (value == Qundef)

```

- 

```
   autoload_delete(klass, id);

```

- 

```
 else

```

- 

```
   rb_warn("already initialized %s %s", dest, rb_id2name(id));

```

- 

```
 if ((VALUE)value == Qundef)

```

- autoload\_delete(klass, id);
- 

```
 else {

```

- VALUE file = Qnil;
- long line = 0;
- if (st\_lookup(RCLASS\_LOC\_TBL(klass), id, &value)) {
- 

```
   file = RNODE(value)->nd_lit;

```

- 

```
   line = RNODE(value)->nd_cnt;

```

- }
- if (!RTEST(file)) {
- 

```
   rb_warn("already initialized %s %s", dest, rb_id2name(id));

```

- }
- else if (!line) {
- 

```
   rb_warn("already initialized %s %s in %s", dest, 

```

rb\_id2name(id),

- 

```
     RSTRING_PTR(file));

```

- }
- else {
- 

```
   rb_warn("already initialized %s %s at %s:%ld", dest, 

```

rb\_id2name(id),

- 

```
     RSTRING_PTR(file), line);

```

- }
- 

```
 }

```

}  
}  
@@ -1726,4 +1752,12 @@ mod\_av\_set(VALUE klass, ID id, VALUE val  
}  
st\_insert(RCLASS\_IV\_TBL(klass), id, val);
- if (!NIL\_P(ruby\_verbose)) {
- NODE \*loc = NEW\_LIT(rb\_sourcefile\_str());
- loc-\>nd\_cnt = rb\_sourceline();
- if (!RCLASS\_LOC\_TBL(klass)) {
- 

```
 RCLASS_LOC_TBL(klass) = st_init_numtable();

```

- }
- st\_insert(RCLASS\_LOC\_TBL(klass), id, (st\_data\_t)loc);
- }  
}

@@ -1958,4 +1992,5 @@ rb\_mod\_remove\_cvar(VALUE mod, VALUE name

```
 if (RCLASS_IV_TBL(mod) && st_delete(RCLASS_IV_TBL(mod), &n, &val)) 

```

{

- remove\_loc(mod, id);  
return (VALUE)val;  
}  
Index: vm.c  
===================================================================  
— vm.c (revision 23168)  
+++ vm.c (working copy)  
@@ -749,9 +749,17 @@ const char \*  
rb\_sourcefile(void)  
{
- VALUE file = rb\_sourcefile\_str();
- if (file) return RSTRING\_PTR(file);
- return 0;  
+}
- 

+VALUE  
+rb\_sourcefile\_str(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 RSTRING\_PTR(cfp-\>iseq-\>filename);

- return cfp-\>iseq-\>filename;  
}  
else {  
Index: include/ruby/intern.h  
===================================================================  
— include/ruby/intern.h (revision 23168)  
+++ include/ruby/intern.h (working copy)  
@@ -200,4 +200,5 @@ void rb\_check\_frozen(VALUE);  
int rb\_sourceline(void);  
const char \*rb\_sourcefile(void);  
+VALUE rb\_sourcefile\_str(void);

# #if defined(NFDBITS) && defined(HAVE\_RB\_FD\_INIT) Index: include/ruby/ruby.h

— include/ruby/ruby.h (revision 23168)  
+++ include/ruby/ruby.h (working copy)  
@@ -581,4 +581,5 @@ typedef struct {  
VALUE super;  
struct st\_table \*iv\_tbl;

- struct st\_table \*loc\_tbl;  
} rb\_classext\_t;

@@ -591,4 +592,5 @@ struct RClass {  
#define RCLASS\_IV\_TBL© (RCLASS©-\>ptr-\>iv\_tbl)  
#define RCLASS\_M\_TBL© (RCLASS©-\>m\_tbl)  
+#define RCLASS\_LOC\_TBL© (RCLASS©-\>ptr-\>loc\_tbl)  
#define RCLASS\_SUPER© (RCLASS©-\>ptr-\>super)  
#define RCLASS\_IV\_INDEX\_TBL© (RCLASS©-\>iv\_index\_tbl)

---

<div class="post-metadata">

**Author:** ![nobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9c0ed/32.png) [@nobu](https://www.ruby-forum.com/u/nobu)\
**Post date:** [March 19, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/feature-1-9-location-of-already-initialized-constant/165274/2 "2010-03-19T15:44:34Z")

</div>

e$B%A%1%C%He(B #1374 e$B$,99?7$5$l$^$7$?!#e(B (by Yusuke E.)

Target version 1.9.xe$B$K%;%C%He(B

e$B1sF#$G$9!#e(B

e$B$3$N%Q%C%A$Oe(B rb\_classext\_t e$B$r$$$8$k$N$G!"e(Bprivate constant  
e$B$H$H$b$Ke(B  
1.9.3 e$B$rL;X$7$^$7$g$&!#e(B

e$B$H$$$&$3$H$Ge(B target e$B$re(B 1.9.x e$B$K$7$^$9!#e(B

## – Yusuke E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[http://redmine.ruby-lang.org/issues/show/1374](http://redmine.ruby-lang.org/issues/show/1374)
