Skip to content

Commit 011b7c6

Browse files
committed
Add hasElaborateCopyConstructor trait
1 parent cb9a800 commit 011b7c6

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/dmd/id.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ immutable Msgtable[] msgtable =
428428
{ "isZeroInit" },
429429
{ "getTargetInfo" },
430430
{ "getLocation" },
431+
{ "hasElaborateCopyConstructor" },
431432

432433
// For C++ mangling
433434
{ "allocator" },

src/dmd/traits.d

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ shared static this()
144144
"isZeroInit",
145145
"getTargetInfo",
146146
"getLocation",
147+
"hasElaborateCopyConstructor"
147148
];
148149

149150
traitsStringTable._init(names.length);
@@ -596,7 +597,8 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
596597
sm => sm.isTemplateDeclaration() !is null) != 0;
597598
});
598599
}
599-
if (e.ident == Id.isPOD)
600+
if (e.ident == Id.isPOD ||
601+
e.ident == Id.hasElaborateCopyConstructor)
600602
{
601603
if (dim != 1)
602604
return dimError(1);
@@ -613,7 +615,14 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
613615
Type tb = t.baseElemOf();
614616
if (auto sd = tb.ty == Tstruct ? (cast(TypeStruct)tb).sym : null)
615617
{
616-
return sd.isPOD() ? True() : False();
618+
if (e.ident == Id.isPOD)
619+
{
620+
return sd.isPOD() ? True() : False();
621+
}
622+
else if (e.ident == Id.hasElaborateCopyConstructor)
623+
{
624+
return sd.hasCopyCtor ? True() : False();
625+
}
617626
}
618627
return True();
619628
}

test/compilable/traits.d

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,45 @@ struct Outer
8686
static assert(__traits(getLocation, Outer.Nested)[1] == 82);
8787
static assert(__traits(getLocation, Outer.method)[1] == 84);
8888

89+
/******************************************/
90+
// https://issues.dlang.org/show_bug.cgi?id=19902
91+
// Define hasElaborateCopyConstructor trait
92+
93+
struct S
94+
{
95+
this (ref S rhs) {}
96+
}
97+
98+
struct OuterS
99+
{
100+
struct S
101+
{
102+
this (ref S rhs) {}
103+
}
104+
}
105+
106+
void foo(T)()
107+
{
108+
struct S(U)
109+
{
110+
this (ref S rhs) {}
111+
}
112+
static assert (__traits(hasElaborateCopyConstructor, S!int));
113+
}
114+
115+
struct U(T) {
116+
this (ref U rhs) {}
117+
}
118+
119+
struct SPostblit {
120+
this(this) {}
121+
}
122+
123+
static assert(__traits(hasElaborateCopyConstructor, S));
124+
static assert(__traits(hasElaborateCopyConstructor, OuterS.S));
125+
static assert(__traits(compiles, foo!int));
126+
static assert(__traits(compiles, foo!S));
127+
static assert(__traits(hasElaborateCopyConstructor, U!int));
128+
static assert(__traits(hasElaborateCopyConstructor, U!S));
129+
130+
static assert(!__traits(hasElaborateCopyConstructor, SPostblit));

0 commit comments

Comments
 (0)