fix #20614 Add __traits(hasMoveConstructor, type) (#20615)

This commit is contained in:
Walter Bright 2024-12-28 22:02:47 -08:00 committed by GitHub
parent 1b9bc74edc
commit cc21914461
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 17 additions and 3 deletions

View file

@ -8978,6 +8978,7 @@ struct Id final
static Identifier* getLocation;
static Identifier* hasPostblit;
static Identifier* hasCopyConstructor;
static Identifier* hasMoveConstructor;
static Identifier* isCopyable;
static Identifier* toType;
static Identifier* parameters;

View file

@ -519,6 +519,7 @@ immutable Msgtable[] msgtable =
{ "getLocation" },
{ "hasPostblit" },
{ "hasCopyConstructor" },
{ "hasMoveConstructor" },
{ "isCopyable" },
{ "toType" },
{ "parameters" },

View file

@ -544,7 +544,9 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
}
return True();
}
if (e.ident == Id.hasCopyConstructor || e.ident == Id.hasPostblit)
if (e.ident == Id.hasCopyConstructor ||
e.ident == Id.hasMoveConstructor ||
e.ident == Id.hasPostblit)
{
if (dim != 1)
return dimError(1);
@ -562,8 +564,14 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
auto ts = tb.isTypeStruct();
if (auto sd = ts ? ts.sym : null)
{
return (e.ident == Id.hasPostblit) ? (sd.postblit ? True() : False())
: (sd.hasCopyCtor ? True() : False());
bool result;
if (e.ident == Id.hasPostblit)
result = sd.postblit !is null;
else if (e.ident == Id. hasCopyConstructor)
result = sd.hasCopyCtor;
else
result = sd.hasMoveCtor;
return result ? True() : False();
}
return False();
}

View file

@ -137,6 +137,8 @@ struct DisabledPostblit
struct NoCpCtor { }
class C19902 { }
struct MoveCtor { this(MoveCtor) { } }
static assert(__traits(hasCopyConstructor, S));
static assert(__traits(hasCopyConstructor, OuterS.S));
static assert(__traits(hasCopyConstructor, OuterS));
@ -147,6 +149,8 @@ static assert(__traits(hasCopyConstructor, U!S));
static assert(!__traits(hasPostblit, U!S));
static assert(__traits(hasPostblit, SPostblit));
static assert(!__traits(hasCopyConstructor, SPostblit));
static assert(__traits(hasMoveConstructor, MoveCtor));
static assert(!__traits(hasMoveConstructor, NoCpCtor));
static assert(!__traits(hasCopyConstructor, NoCpCtor));
static assert(!__traits(hasCopyConstructor, C19902));