www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21887] New: Compiler crashes when interfacing C++

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

          Issue ID: 21887
           Summary: Compiler crashes when interfacing C++
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: feco.graczer gmail.com

ldc2 --version
LDC - the LLVM D compiler (1.25.1):
  based on DMD v2.095.1 and LLVM 11.0.1
  built with LDC - the LLVM D compiler (1.25.1)
  Default target: x86_64-unknown-linux-gnu
  Host CPU: skylake
  http://dlang.org - http://wiki.dlang.org/LDC



gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

-----------
CPP file
compiled with
gcc -c test.cpp -lstdc++ -o test.o

#include <set>
#include <cstdint>

std::set<unsigned int>* makeTestSet()
{
    std::set<unsigned int>* set = new std::set<unsigned int>({1, 2, 3, 4, 5});
    return set;
}

template<typename T>
int cpp_set_foreach_ind(std::set<T>& setptr, int (*dg)(uint64_t, const T&
value))
{
    uint64_t ind = 0;
    for (auto const &elem : setptr)
    {
        int res = dg(ind++, elem);
        if (res != 0)
            return res;
    }

    return 0;
}

#define CPPSETFOREACHINSTIND(T) template int
cpp_set_foreach_ind<T>(std::set<T>&, int (*)(uint64_t, const T& value));
CPPSETFOREACHINSTIND(unsigned int)

-----------
DLang file
ldc2 tests.d test.o 

module tests;
import core.stdc.stdint : uint64_t;

alias dgType(Key) = extern(D) scope int delegate(uint64_t ind, ref const(Key));
extern(C++) private int cpp_set_foreach_ind(Key)(ref set!Key set, dgType!Key
dg);

extern(C++, `std`) {
    public extern(C++, class) struct set (Key)
    {
        void*[3] ptr;
        extern(D) public int opApply (scope int delegate(uint64_t ind, ref
const(Key)) dg)
        {
            return cpp_set_foreach_ind!Key(this, dg);
        }
    }
}

extern(C++) set!uint* makeTestSet();

int main()
{
    auto set = makeTestSet;
    foreach (i, val; *set)
    {
        writeln(i, " -> ", val);
    }
    return 1;
}

----
compiler crashes:


(/home/dan/dev/thirdparty/ldc2-1.25.1/bin/ldc2+0x3446c24)
[1]    45904 segmentation fault (core dumped)  ldc2 tests.d test.o

--
May 02 2021