Thread overview
[Issue 5500] New: Appender.put causes an invariant violation in struct with overloaded opAssign
Jan 29, 2011
Nicolas Sicard
Jan 29, 2011
Ellery Newcomer
Jan 29, 2011
Nicolas Sicard
Jan 30, 2011
Jonathan M Davis
Feb 01, 2011
Jonathan M Davis
Feb 01, 2011
Nicolas Sicard
May 23, 2013
Jameson
January 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5500

           Summary: Appender.put causes an invariant violation in struct
                    with overloaded opAssign
           Product: D
           Version: D2
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: dransic@free.fr


--- Comment #0 from Nicolas Sicard <dransic@free.fr> 2011-01-29 00:10:59 PST ---
The execution of this code throws an assertion error.

import std.array;

struct Foo {
    int a, b;

    Foo opAssign(Foo rhs) {
        a = rhs.a;
        b = rhs.b;
        return this;
    }

    invariant () {
        assert (a <= b);
    }
}

unittest {
    auto app = appender!(Foo[]);
    auto foo = Foo(1, 2);
    app.put(foo);
}


Stack trace:

core.exception.AssertError@test(13): Assertion failure
----------------
5   test.d.E6E9779DFA87AC0EBDE59516F9288677 0x00009a61 onAssertError + 65
6   test.d.E6E9779DFA87AC0EBDE59516F9288677 0x00012e82 _d_assertm + 30
7   test.d.E6E9779DFA87AC0EBDE59516F9288677 0x00002593 void test.__assert(int)
+ 27
8   test.d.E6E9779DFA87AC0EBDE59516F9288677 0x000025ef void
test.Foo.__invariant() + 23
9   test.d.E6E9779DFA87AC0EBDE59516F9288677 0x000025ae test.Foo
test.Foo.opAssign(test.Foo) + 14
10  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x00002bfd void
std.array.Appender!(test.Foo[]).Appender.put!(test.Foo).put(test.Foo) + 113
11  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x0000262b void test.__unittest1()
+ 55
12  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x00002573 void test.__modtest() +
11
13  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x0000a121 extern (C) bool
core.runtime.runModuleUnitTests().int __foreachbody247(ref object.ModuleInfo*)
+ 45
14  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x0000560f int
object.ModuleInfo.opApply(scope int delegate(ref object.ModuleInfo*)) + 79
15  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x0000a012 runModuleUnitTests + 134
16  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x00013202 extern (C) int
rt.dmain2.main(int, char**).void runAll() + 38
17  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x00013156 extern (C) int
rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 38
18  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x000130e7 main + 179
19  test.d.E6E9779DFA87AC0EBDE59516F9288677 0x0000255d start + 53

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5500


Ellery Newcomer <ellery-newcomer@utulsa.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ellery-newcomer@utulsa.edu


--- Comment #1 from Ellery Newcomer <ellery-newcomer@utulsa.edu> 2011-01-29 05:33:22 PST ---
(In reply to comment #0)
> The execution of this code throws an assertion error.
> 

I can't seem to reproduce this.

dmd 2.051, linux

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5500



--- Comment #2 from Nicolas Sicard <dransic@free.fr> 2011-01-29 06:22:56 PST ---
(In reply to comment #1)
> (In reply to comment #0)
> > The execution of this code throws an assertion error.
> > 
> 
> I can't seem to reproduce this.
> 
> dmd 2.051, linux


Weird. I have this error every time I run the code in
- dmd 2.050 & 2.051 Mac OS X (differents systems)
- dmd 2.051 Linux (Ubuntu 10.10) / Parallels Desktop
I will try on a PC with MSWindows this evening.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5500


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #3 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-01-29 18:03:45 PST ---
I'm not getting this to happen with dmd 2.051 on Arch Linux either (Phobos and druntime are from git). However, if I use the example that you originally gave on D.learn:

import std.array, std.datetime;

unittest {
     auto app = appender!(Interval!Date[]);
     auto interval = Interval!Date(Date(2000, 1, 1), Date(2011, 2, 3));
     app.put(interval);
     // Error: datetime.d(20208): Invariant Failure: begin is not before
or equal to end.
}

it happens some of the time but not all of the time. So, whatever the bug is exactly, it's non-deterministic, and it may easily get affected by the OS that you're on. It may be that some slight tweaks would make it pop up on boxes which currently can't reproduce it. I'm convinced that there is a bug here, but it's non-deterministic enough, that depending on the code and the machine, it could either be reproduced easily or only with difficulty.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 01, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5500



--- Comment #4 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-01-31 17:40:56 PST ---
Actually, I think I know what's happening (roughly speaking). This problem is caused by bug# 5058. Appender has uninitialized data which it then tries to assign to using the assignment operator. Because the invariant is called before opAssign (which I argue in bug# 5058, that it shouldn't be), the invariant is called on garbage data. Sometimes, that garbage data violates the invariant, and sometimes it doesn't. So, unless there's something that Appender can do to work around bug# 5058, it's likely not a bug in Appender so much as it's a bug in how invariants work with regards to opAssign.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 01, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5500



--- Comment #5 from Nicolas Sicard <dransic@free.fr> 2011-01-31 22:50:35 PST ---
> Actually, I think I know what's happening (roughly speaking). This problem is
> caused by bug# 5058.

I think you are right, these bugs look very similar.

It definitely is related to opAssign, since it never occurs when opAssign is not redefined. This would also mean that the compiler-generated opAssign is either not calling invariant() or calling it at the right time, after data is properly initialized. But I don't know what happens behind the scene in this matter.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5500


Jameson <beatgammit@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |beatgammit@gmail.com


--- Comment #6 from Jameson <beatgammit@gmail.com> 2013-05-23 00:05:43 PDT ---
I can't reproduce this:

DMD 2.062
LDC2 (based on 2.061)
Arch Linux x86_64

Can anyone else reproduce this? This bug is quite old...

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