www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6178] New: Struct inside the AA are not init correctly

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

           Summary: Struct inside the AA are not init correctly
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: jsancio gmail.com





-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 19 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




Err -- Hit submit without a description...

I am going to assume that this was not already filed. If so please accept my
apologies.

If you create an AA that points to a struct the is not initialized properly. In
other word it is not the same as S.init. If this stays that way in can
completely confuse the dtor. For example:

import std.stdio;

struct Bug
{
   this(this) { writefln("%s postblit ctor called", var); }
   void opAssign(Bug rhs) { writefln("%s opAssign called", var); }
   ~this() { writefln("%s dtor called", var); }

   int var = 0;
}

void main()
{
   Bug[int] map;
   {
      map[0] = Bug(1);
   }
}

This yields the following output (you need -O or you will hit bug 6177):

dmd -w -O map_test.d && ./map_test
3 opAssign called
1 dtor called
3 dtor called

Where in the world is 3 coming from? You output may be slightly different.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


Jose Garcia <jsancio gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P1
          Component|DMD                         |druntime
           Platform|Other                       |x86
         OS/Version|Windows                     |Linux
           Severity|normal                      |blocker



Moving it to druntime since it probably belongs there.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




I am getting around this issue with:

enum : uint { TOKEN = 987654321 }

struct Bug
{
   ~this()
   {
      if(token == TOKEN) { ...}
   }

   private uint token = TOKEN;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


kennytm gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kennytm gmail.com
          Component|druntime                    |DMD



With an opAssign, an assignment to an AA struct will become something like

    Bug __aatmp1234 = void;   // <---
    __aatmp1234.opAssign(1);
    map.set(0, __aatmp1234);
    map[0].___postblit();

the '3' is likely due to the '<---' line. See bug 2451.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei metalanguage.com



08:40:19 PST ---
Update - with the dmd from the head the example causes 

Internal error: backend/cgcs.c 162

If the destructor is commented out, the printed message on my machine is

-2084965984 opAssign called

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 18 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




Andrei,

Do you still get the compiler internal error with -0? See bug 6177.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 18 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




07:11:42 PST ---

 Andrei,
 
 Do you still get the compiler internal error with -0? See bug 6177.
I don't understand the question. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 19 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


hsteoh quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh quickfur.ath.cx



I think he meant -O, not -0.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




On Linux, I see the bug only with 32 bits, it works OK with 64 bits.

With -m64 and -m64 -O, I get

0 opAssign called
1 dtor called
0 dtor called

whereas with -m32 I get

-142997715 opAssign called
1 dtor called
-142997715 dtor called

and with -m32 -O

2 opAssign called
1 dtor called
2 dtor called

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 13 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim maxim-fomin.ru



---
*** Issue 9084 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 13 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


SomeDude <lovelydear mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear mailmetrash.com



PST ---
Different output for different compilers:
http://dpaste.dzfl.pl/c94eea76

Success with GDC 2.060 both 32/64 bits
0 opAssign called
1 dtor called
0 dtor called

Fails identically with LDC 2.060 32 and 64 bits
-1 opAssign called
1 dtor called
-1 dtor called

Success with DMD 2.060 32 bits, fails in 64 bits


Compilation error with Git HEAD 32 bits and failure in 64 bits

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 28 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


Dmitry Olshansky <dmitry.olsh gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmitry.olsh gmail.com



14:19:41 PST ---
I've just hit another case of this bug on Win32. 

It's a major impediment for new std.uni as it may result in memery corruption 
if a set of code points is written to AA like this:

//this one calls destructor twice and no postblit and causes memory corruption
props["Set"] = CodepointSet(a1, a2+1);

//While this one is fine (it calls postblit):
auto set = CodepointSet(a1, a2+1);
props["Set"] = set;

There is no opAssign only postblit + destructor.

The problem is that in the real world the destructor is tricky and
expects at least T.init or some valid object else it blows up quite nasty.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




---
Well, seems in 2.061 the behavior was changed. 

Without any options, the output is like:

-1154412584 opAssign called (big value)
1 dtor called
-1154412584 dtor called

With -release or -noboundscheck the output is more robust:

0 opAssign called
1 dtor called
0 dtor called

With -O option output is:

32638 opAssign called // close to signed short int max
1 dtor called
32638 dtor called

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




01:46:18 PST ---

 Well, seems in 2.061 the behavior was changed. 
 
 Without any options, the output is like:
 
 -1154412584 opAssign called (big value)
 1 dtor called
 -1154412584 dtor called
 
 With -release or -noboundscheck the output is more robust:
 
 0 opAssign called
 1 dtor called
 0 dtor called
 
 With -O option output is:
 
 32638 opAssign called // close to signed short int max
 1 dtor called
 32638 dtor called
This is shitty but I'll use -release for the moment. BTW it seems to require both module with struct *and* the one where AA is used to be compiled wiht -release. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




---

 This is shitty but I'll use -release for the moment. BTW it seems 
 to require both module with struct  *and* the one where AA is used to be
 compiled wiht -release.
Than this a part of a bigger shit. /* Known as a problem of filling newly created space of AA array with T.init before assigning actual object. If operation is interrupted, this leads to AA array containing "orphan" T.init objects by no reason. Was reported in BZ. */ import std.stdio; int foo() { throw new Exception(""); } int[int] array; void main() { try { array[1] = foo(); } catch(Exception e) { } writeln(array); } Compiling with -O => [1:0] Compiling with -release => [] Compiling with -noboundscheck => [] So, it appears that there is not only bug with AA assignment, but the bug depends on compiler options. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




---

 Than this a part of a bigger shit.
 
 /* Known as a problem of filling newly created space of AA array
    with T.init before assigning actual object.
    If operation is interrupted, this leads to AA array containing
    "orphan" T.init objects by no reason. Was reported in BZ.
 */
 import std.stdio;
 
 int foo()
 {
     throw new Exception("");
 }
 
 int[int] array;
 
 void main()
 {
     try
     {
         array[1] = foo();
     }
     catch(Exception e)
     {
 
     }
     writeln(array);
 }
 
 Compiling with -O => [1:0]
 Compiling with -release => []
 Compiling with -noboundscheck => []
 
 So, it appears that there is not only bug with AA assignment, but the bug
 depends on compiler options.
It was bug 3825, and has already fixed in 2.063 (git head). -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 04 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samukha voliacable.com



---
*** Issue 5234 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 07 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |canidae exent.net



---
*** Issue 7503 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 07 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |szabobogdan yahoo.com



---
*** Issue 10356 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 07 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




---

 With an opAssign, an assignment to an AA struct will become something like
 
     Bug __aatmp1234 = void;   // <---
     __aatmp1234.opAssign(1);
     map.set(0, __aatmp1234);
     map[0].___postblit();
 
 the '3' is likely due to the '<---' line. See bug 2451.
This is the root cause. If the value struct in AA has opAssign method, it would be called on uninitialized object. For such structs, AA value setting should do additional work to distinguish initializing and assignment. struct S1 { void opAssign(S1 rhs) {} } S1[int] aa1; aa1[1] = S1(); // [*] I think the line at [*] should behave as follows: 1. If the key doesn't exist in aa yet, so it should work as initializing. 2. If the key already exists in aa, so it should work as assignment (== opAssign call). -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 07 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull, wrong-code
           Platform|x86                         |All
         OS/Version|Linux                       |All



---

 For such structs, AA value setting should do additional work to distinguish
 initializing and assignment.
 
 struct S1 {
     void opAssign(S1 rhs) {}
 }
 
 S1[int] aa1;
 aa1[1] = S1();  // [*]
 
 I think the line at [*] should behave as follows:
 1. If the key doesn't exist in aa yet, so it should work as initializing.
 2. If the key already exists in aa, so it should work as assignment
    (== opAssign call).
https://github.com/D-Programming-Language/dmd/pull/2539 https://github.com/D-Programming-Language/phobos/pull/1554 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 08 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/e36292ebe5f9569de0956f261ceb2eed7123271c
Supplement fix fox issue 6178

AA value setting through alias this should not be allowed, because it could
access invalid object state.

https://github.com/D-Programming-Language/phobos/commit/4721a44fb30632e81c430e886851d833f0a43ba4


Supplement fix fox issue 6178

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




---
*** Issue 5822 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |juhonurm gmail.com



---
*** Issue 8170 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/1f5be26103a644b2650ae2d811edbc04c18236b7
fix Issue 6178 - Struct inside the AA are not init correctly

If the AA value setting invokes opAsign method, it would need additional
runtime const to distinguish construction and assignment.

https://github.com/D-Programming-Language/dmd/commit/26ca8f7c83808a2dd2c1f4e2a80a08ec567cac34


Issue 6178 - Struct inside the AA are not init correctly

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 22 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6178




Commit pushed to master at https://github.com/D-Programming-Language/dlang.org

https://github.com/D-Programming-Language/dlang.org/commit/ebcc484ea5be319beb61aceaf4ec30a0dff0f4f5
fix Issue 6178 - Struct inside the AA are not init correctly

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