Skip to content

Commit

Permalink
Postblit is still a form of copy constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
edi33416 committed Aug 5, 2019
1 parent 011b7c6 commit 17432c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
33 changes: 23 additions & 10 deletions src/dmd/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,7 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
sm => sm.isTemplateDeclaration() !is null) != 0;
});
}
if (e.ident == Id.isPOD ||
e.ident == Id.hasElaborateCopyConstructor)
if (e.ident == Id.isPOD)
{
if (dim != 1)
return dimError(1);
Expand All @@ -615,17 +614,31 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
Type tb = t.baseElemOf();
if (auto sd = tb.ty == Tstruct ? (cast(TypeStruct)tb).sym : null)
{
if (e.ident == Id.isPOD)
{
return sd.isPOD() ? True() : False();
}
else if (e.ident == Id.hasElaborateCopyConstructor)
{
return sd.hasCopyCtor ? True() : False();
}
return sd.isPOD() ? True() : False();
}
return True();
}
if (e.ident == Id.hasElaborateCopyConstructor)
{
if (dim != 1)
return dimError(1);

auto o = (*e.args)[0];
auto t = isType(o);
if (!t)
{
e.error("type expected as second argument of __traits `%s` instead of `%s`",
e.ident.toChars(), o.toChars());
return new ErrorExp();
}

Type tb = t.baseElemOf();
if (auto sd = tb.ty == Tstruct ? (cast(TypeStruct)tb).sym : null)
{
return sd.hasCopyCtor || sd.postblit ? True() : False();
}
return False();
}
if (e.ident == Id.isNested)
{
if (dim != 1)
Expand Down
17 changes: 14 additions & 3 deletions test/compilable/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ struct OuterS
{
this (ref S rhs) {}
}

S s;
}

void foo(T)()
Expand All @@ -112,19 +114,28 @@ void foo(T)()
static assert (__traits(hasElaborateCopyConstructor, S!int));
}

struct U(T) {
struct U(T)
{
this (ref U rhs) {}
}

struct SPostblit {
struct SPostblit
{
this(this) {}
}

struct NoCpCtor { }
class C19902 { }

static assert(__traits(hasElaborateCopyConstructor, S));
static assert(__traits(hasElaborateCopyConstructor, OuterS.S));
static assert(__traits(hasElaborateCopyConstructor, OuterS));
static assert(__traits(compiles, foo!int));
static assert(__traits(compiles, foo!S));
static assert(__traits(hasElaborateCopyConstructor, U!int));
static assert(__traits(hasElaborateCopyConstructor, U!S));
static assert(__traits(hasElaborateCopyConstructor, SPostblit));

static assert(!__traits(hasElaborateCopyConstructor, SPostblit));
static assert(!__traits(hasElaborateCopyConstructor, NoCpCtor));
static assert(!__traits(hasElaborateCopyConstructor, C19902));
static assert(!__traits(hasElaborateCopyConstructor, int));

0 comments on commit 17432c3

Please sign in to comment.