www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13207] New: Wrong code for 'extern' C/C++ function returning

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

          Issue ID: 13207
           Summary: Wrong code for 'extern' C/C++ function returning
                    struct
           Product: D
           Version: D2
          Hardware: x86_64
                OS: All
            Status: NEW
          Keywords: wrong-code
          Severity: critical
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: verylonglogin.reg gmail.com

This program should run fine:

D code:
---
const size_t n = 8;

struct S1 { long l; }       // good
struct S2 { byte[n] arr; }  // causes errors

static assert(S1.sizeof == n);
static assert(S2.sizeof == n);

extern(C):
size_t getN() { return n; }
S1 getS1() { return S1(); } // correct code
S2 getS2() { return S2(); } // wrong code
---

C code:
---
#include<assert.h>

const size_t n = 8;

struct S { long long ll; };

static_assert(sizeof(S) == n, "Size mismatch.");

size_t getN();
S getS1();
S getS2();

int main()
{
    // no 'rt_init' needed.

    assert(getN() == n, "D struct size mismatch.");
    const S s1 = getS1();
    const S s2 = getS2();
    assert(s1.ll == 0); // ok
    assert(s2.ll == 0); // fails

    return 0;
}
---

Checked only on Mac OS X but this issue should also exist at least on other
OS-es with System V AMD64 ABI (i.e. all except Windows).

This issue makes interoperation with C/C++ stuff very difficult.

--
Jul 26 2014