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
Linux vubuntu 5.8.0-50-generic #56~20.04.1-Ubuntu
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:
#0 0x0000000003446c24 PrintStackTraceSignalHandler(void*) (/home/dan/dev/thirdparty/ldc2-1.25.1/bin/ldc2+0x3446c24)
[1] 45904 segmentation fault (core dumped) ldc2 tests.d test.o
Comment #1 by feco.graczer — 2021-05-03T03:43:50Z
crashes with dmd too
Comment #2 by moonlightsentinel — 2021-05-03T10:49:17Z
Possible reduction (but might also be another bug):
=====================================================
alias dgType() = extern(D) int delegate();
extern(C++) int cpp_set_foreach_ind()(dgType!() dg);
int foo(int delegate() dg)
{
return cpp_set_foreach_ind!()(dg);
}
=====================================================
Comment #3 by robert.schadek — 2024-12-13T19:16:06Z