On Sat, Jul 12, 2014 at 09:30:44PM -0700, H. S. Teoh via Digitalmars-d-learn wrote: > On Sat, Jul 12, 2014 at 09:20:06PM -0700, Ali Çehreli via Digitalmars-d-learn > wrote: [...] > > The twist here is that the OP's function returned 'this' by > > reference. Changing that not only not have any effect on the > > object, it would also be undefined behavior because 'this' is a > > local variable in that use. > > > > class C { > > ref C getPtr() { return this; } > > } > > > > void main() > > { > > auto c = new C(); > > assert(c.getPtr() is c); > > > > c.getPtr() = new C(); // modifying dead variable > > assert(c.getPtr() is c); // no effect > > } > [...] > > Hmm. Shouldn't this be a compiler bug?? Returning a ref to a local > variable should be illegal, even implicit ones like 'this'. [...]
Filed as bug: https://issues.dlang.org/show_bug.cgi?id=13116 It's pretty serious, since it causes memory corruption. In fact, it also breaks @safe-ty (see second bug note in above link). T -- The early bird gets the worm. Moral: ewww...