[Issue 22252] New: ImportC: C Array syntax is mapped to D array semantics with disastrous consequences.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Aug 30 04:01:53 UTC 2021


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

          Issue ID: 22252
           Summary: ImportC: C Array syntax is mapped to D array semantics
                    with disastrous consequences.
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: maxhaton at gmail.com

C arrays are being mapped to D arrays - This is not even wrong, as the saying
goes.

Let's say we have the following:

---
int setAtOffset(int array[], unsigned int offset)
{
    array[offset] = 4;
}
---

Which we then import into our D file and use in the following deliberately
nonsensical example: 
---
void offsetTest()
{
    setAtOffset(null, 3);
}
---

This yields the following asm 

000000000000190c <_D6setjmp10offsetTestFZv>:
    190c:       push   rbp
    190d:       mov    rbp,rsp
    1910:       sub    rsp,0x10
    1914:       mov    edx,0x3 <-
    1919:       mov    QWORD PTR [rbp-0x8],rdx <-
    191d:       xor    edi,edi
    191f:       xor    edx,edx
    1921:       mov    rsi,rdx
    1924:       mov    rdx,QWORD PTR [rbp-0x8] <-
    1928:       call   1938 <setAtOffset>

Which is passing 3 into rdx, so as per SystemV it thinks it's the third
parameter. 

Compare with GCC (exactly the same code, it's valid C):
offsetTest:
        mov     esi, 3
        xor     edi, edi
        jmp     setAtOffset
Similarly:
pragma(msg, typeof(setAtOffset));

Yields:
"extern (C) int(int[] array, uint offset)", which is wrong

--


More information about the Digitalmars-d-bugs mailing list