Fix Issue 24139 - 'this' corruption in extern(C++) dtor when destructing via TypeInfo_Struct (#15598)

For the `TypeInfo_Struct.xdtor` field, the linkage of the aggregate
itself plays no role; it's the linkage of the destructor that matters.

A real-life example for this is DMD's own `OutBuffer`.
This commit is contained in:
Martin Kinkelin 2023-09-11 08:55:13 +01:00 committed by GitHub
parent c9d1581f55
commit fbafba8790
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -1126,7 +1126,7 @@ private DtorDeclaration buildExternDDtor(AggregateDeclaration ad, Scope* sc)
return null;
// Generate shim only when ABI incompatible on target platform
if (ad.classKind != ClassKind.cpp || !target.cpp.wrapDtorInExternD)
if (dtor._linkage != LINK.cpp || !target.cpp.wrapDtorInExternD)
return dtor;
// generate member function that adjusts calling convention

View file

@ -0,0 +1,25 @@
// https://issues.dlang.org/show_bug.cgi?id=24139
struct S1
{
int x;
extern(C++) ~this() { assert(&this == s1); }
}
extern(C++) struct S2
{
int x;
~this() { assert(&this == s2); }
}
S1* s1;
S2* s2;
void main()
{
s1 = new S1;
s2 = new S2;
typeid(S1).destroy(s1);
typeid(S2).destroy(s2);
}