dmd/compiler/test/runnable_cxx/test24292.d
Tim Schendekehl a4d6972c08 Fixes issues 20339, 24292: Calculate isPOD without information from semantic run
Issue 20339: isPOD returns true if sizeof is accessed inside struct declaration
Issue 24292: Struct with destructor wrongly returned in register

Function StructDeclaration.isPOD can be called before the semantic run
for the struct is finished. It then uses incomplete information about
destructors, postblits and copy constructors. The result of isPOD is
cached, so later calls will also return the wrong result.

This commit changes isPOD, so it uses variables, which are already
filled before the semantic run.
2023-12-21 23:20:11 +01:00

50 lines
1.1 KiB
D

// EXTRA_CPP_SOURCES: test24292.cpp
extern(C++) struct List(T)
{
// Any of the following static ifs can trigger the problem.
static if (T.sizeof > 4) {}
static if (__traits(isZeroInit, T)) {}
static if (__traits(isPOD, T)) {}
T* begin;
}
extern(C++) struct StructWithDestructor
{
~this();
alias L = List!StructWithDestructor;
int i;
}
extern(C++) struct StructWithCopyCtor
{
this(ref const(StructWithCopyCtor));
alias L = List!StructWithCopyCtor;
int i;
}
extern(D) struct StructWithPostblit
{
this(this) {}
alias L = List!StructWithPostblit;
int i;
}
static assert(!__traits(isPOD, StructWithDestructor));
static assert(!__traits(isPOD, StructWithCopyCtor));
static assert(!__traits(isPOD, StructWithPostblit));
extern(C++) StructWithDestructor getStructWithDestructor();
extern(C++) StructWithCopyCtor getStructWithCopyCtor();
void main()
{
StructWithDestructor structWithDestructor = getStructWithDestructor();
assert(structWithDestructor.i == 12345);
StructWithCopyCtor structWithCopyCtor = getStructWithCopyCtor();
assert(structWithCopyCtor.i == 54321);
}