www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 780] New: The assignment of 'this' is allowed

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=780

           Summary: The assignment of 'this' is allowed
           Product: D
           Version: 0.178
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: brunodomedeiros+bugz gmail.com


The compiler allows assignment of the 'this' special reference. Instead 'this'
should be final (i.e., non assignable). In some cases, like in a class
constructor, 'this' is treated as if an inout reference, changing a variable in
another scope:

--------  --------
import std.stdio;

class Foo {
  static bool first = true;

  this() {
    if(first){
      first = false;
      this = new Foo();
      writefln("this= ", cast(void *)this);
    } 
  } 

}


void main(char[][] args) {

        Foo foo = new Foo();
        writefln("foo= ", cast(void *)foo);
}

--------  outputs:  --------
this before= 8B0FE0
this after= 8B0FD0
foo= 8B0FD0


-- 
Dec 30 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=780





------- Comment #1 from thomas-dloop kuehne.cn  2007-02-27 10:47 -------
http://www.digitalmars.com/d/class.html#constructors
# Constructors are defined with a function name of this and having no return 
value

However the compiler implements class constructors as returning the this
pointer
#
# module wood;
# class Tree{
#    this(){
#    }
# }
#

dmd -c wood.d && nm wood.o | grep _ctor | sed "s:.* ::"
_D4wood4Tree5_ctorMFZC4wood4Tree 

demangled: wood.Tree wood.Tree.this(this)


-- 
Feb 27 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=780


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |bugzilla digitalmars.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 26 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=780



--- Comment #2 from Andrei Alexandrescu <andrei metalanguage.com> 2011-01-08
14:10:50 PST ---
Before and after return the same thing in 2.051. Nevertheless, assigning to
this should be syntactically disallowed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 08 2011