www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 671] New: Weird class reference declaration compiles

reply d-bugmail puremagic.com writes:
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;
}


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


bugzilla digitalmars.com changed:

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





opAssign isn't for object construction. The object t was never created or
constructed.


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







 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. --
Dec 09 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
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                     |





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.


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






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)


-- 
Dec 10 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jan 21 2012
prev sibling parent d-bugmail puremagic.com writes:
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



*** 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: -------
Mar 27 2012