Thread overview
[Issue 671] New: Weird class reference declaration compiles
Dec 10, 2006
d-bugmail
Dec 10, 2006
d-bugmail
Dec 10, 2006
d-bugmail
Dec 10, 2006
d-bugmail
Dec 11, 2006
d-bugmail
[Issue 671] Class initialization should not call opAssign
Jan 21, 2012
Walter Bright
Mar 28, 2012
Kenji Hara
December 10, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=671

           Summary: Weird class reference declaration compiles
           Product: D
           Version: 0.177
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: maxter@i.com.ua


This compiles and fails at runtime with access violation. Seems like opAssign gets called on the uninitialized class reference.

class Test
{
        int a;

        void opAssign(int v)
        {
                a = v;
        }
}

void main()
{
        Test t = 20;
}


-- 

December 10, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=671


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #1 from bugzilla@digitalmars.com  2006-12-09 20:32 -------
opAssign isn't for object construction. The object t was never created or constructed.


-- 

December 10, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=671





------- Comment #2 from ibisbasenji@gmail.com  2006-12-09 23:41 -------
(In reply to comment #1)
> opAssign isn't for object construction. The object t was never created or constructed.
> 

No doubt, though I would argue this is one specific case the compiler could catch and issue an error on (attempt to initialize object variable with non-object).

Either that or rewrite 'Class var = 42' as 'Class var = (new Class) = 42' such that it works with any class defining a default constructor.  I'm not saying I'm fond of that idea (not entirely fond of opAssign at all) but it would seem a natural enough evolution.


-- 

December 10, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=671


smjg@iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com
             Status|RESOLVED                    |REOPENED
           Keywords|                            |accepts-invalid, spec
         Resolution|INVALID                     |




------- Comment #3 from smjg@iname.com  2006-12-10 12:43 -------
operatoroverloading.html

"The assignment operator = can be overloaded if the lvalue is a struct or class aggregate, and opAssign  is a member function of that aggregate."

The assignment _operator_, that is.  The "t = 20" of "Test t = 20;" is not an AssignExpression, or indeed any kind of expression, and so it's not using the assignment operator.  It's a wholly distinct use of the "=" token from a code structure POV.

If this were to be allowed, it would have to be explicitly specified that opAssign applies to initializers as well as AssignExpressions.  Even then, unless as Chris suggests you make it equivalent to

    Test t = (new Test) = 20;

then it makes no sense whatsoever to allow a class (as opposed to struct or union) variable to be initialized in this way.


-- 

December 11, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=671





------- Comment #4 from maxter@i.com.ua  2006-12-10 19:09 -------
Actually, I discovered this bug while playing with dynamic struct initialization (having opCall and opAssign with the same signature in the test struct). I just replaced struct with class and wondered if the compiler choke.

One of the following could fix the issue:

1. Disallow the syntax (Test t = (new Test) = 20; instead?);
2. Implicitly create a Test instance passing 20 to the ctor.
class Test
{
int a;
this(int v)
{
a = v;
}
}

Test t = 20; // creates a Test and initializes a to 20; (bad idea)

3. Call 'static Test opCall(int);' on the class and assign the returned
reference to t (similar to structs, bad idea)


-- 

January 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=671


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|spec                        |
            Summary|Weird class reference       |Class initialization should
                   |declaration compiles        |not call opAssign


--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> 2012-01-21 11:16:06 PST ---
Stewart's right. This is construction, not assignment, and opAssign should not be called. Compiler bug, not spec problem.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=671


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |DUPLICATE


--- Comment #6 from Kenji Hara <k.hara.pg@gmail.com> 2012-03-27 17:40:39 PDT ---
*** This issue has been marked as a duplicate of issue 7641 ***

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