www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19563] New: extern(C++) Incorrect ABI passing small struct

https://issues.dlang.org/show_bug.cgi?id=19563

          Issue ID: 19563
           Summary: extern(C++) Incorrect ABI passing small struct
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: blocker
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: turkeyman gmail.com

test.cpp
--------

struct Test
{
    Test() : ptr(nullptr) {}
    Test(const Test& x)
    {
        ptr = x.ptr + 10;
    }
    char* ptr;
};

int test(Test p);

int main()
{
    Test x;
    test(x);
    return 0;
}

test.d
------

extern(C++):

struct Test
{
    this(this)
    {
        ptr += 10;
    }
    char* ptr;
}

int test(Test p)
{
    assert(p.ptr == null);
    return 10;
}

--------------------------------------------

When I build and run this, passing `x` to test() by value crashes. The function
receives garbage.

If you remove the copy constructors (pass this same struct as a POD), it works
fine!

This was tested in Linux 64bit, against GCC-7.
If it makes any difference, I supplied `-std=c++11 -D_GLIBCXX_USE_CXX11_ABI=0`
to the C compiler.

--
Jan 09