atilaneves / dpp

Directly include C headers in D source code
Boost Software License 1.0
231 stars 31 forks source link

Crash with rvalue structs #158

Closed atilaneves closed 5 years ago

atilaneves commented 5 years ago

Code to reproduce:

# build.sh
clang++ -c cppstl.cpp
./d++ --ignore-ns std --ignore-ns __gnu_cxx --ignore-ns __gnu_debug --ignore-ns __cxxabiv1 stl.dpp
// cppstl.hpp
#include <vector>

struct Problem {
    Problem(int length);
    int numInts() const;

private:
    std::vector<int> ints;
};
// cppstl.cpp
#include "cppstl.hpp"
#include <iostream>

using namespace std;

Problem::Problem(int length):ints(length) {
    cout << "C++ Problem ctor this: "  << this << " length: " << length << endl;
    cout << "C++ ints length: " << ints.size() << endl;
}

int Problem::numInts() const {
    cout << "C++ numInts this: " << this << endl;
    return ints.size();
}
#include "cppstl.hpp"
void main() {
    assert(Problem(42).numInts == 42);
}

Running produces this output:

C++ Problem ctor this: 0x7fffde27cf98 length: 42
C++ ints length: 42
C++ numInts this: 0x4f98decc6d648d00
zsh: segmentation fault (core dumped)  ./stl

Notice that the pointer passed to numInts is not the same as the pointer of the created struct. The code works as expected if passed as an rvalue.

atilaneves commented 5 years ago

This is actually this dmd issue.

It works fine with ldc.