www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5500] New: Appender.put causes an invariant violation in struct with overloaded opAssign

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



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: -------
Jan 29 2011
next sibling parent d-bugmail puremagic.com writes:
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



05:33:22 PST ---

 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: -------
Jan 29 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5500






 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: -------
Jan 29 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5500


Jonathan M Davis <jmdavisProg gmx.com> changed:

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



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: -------
Jan 29 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5500




PST ---
Actually, I think I know what's happening (roughly speaking). This problem is

assign to using the assignment operator. Because the invariant is called before

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

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: -------
Jan 31 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5500




 Actually, I think I know what's happening (roughly speaking). This problem is

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: -------
Jan 31 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5500


Jameson <beatgammit gmail.com> changed:

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



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: -------
May 23 2013