Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 7829 - Function traits should be usable with subtyping #1445

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/traits.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ Expression *TraitsExp::semantic(Scope *sc)
goto Lfalse; \
goto Ltrue;

#define ISVARDSYMBOL(cond) \
for (size_t i = 0; i < dim; i++) \
{ \
Expression *e; \
Dsymbol *s = getDsymbol((*args)[i]); \
if ((s = getDsymbol((*args)[i])) != NULL) \
{ } \
else if ((e = isExpression((*args)[i])) != NULL) \
{ \
if (e->op == TOKvar) \
s = ((VarExp *)e)->var; \
else if (e->op == TOKdotvar) \
s = ((DotVarExp *)e)->var; \
} \
if (!s) \
goto Lfalse; \
if (!(cond)) \
goto Lfalse; \
} \
if (!dim) \
goto Lfalse; \
goto Ltrue; \


if (ident == Id::isArithmetic)
Expand Down Expand Up @@ -171,28 +193,28 @@ Expression *TraitsExp::semantic(Scope *sc)
else if (ident == Id::isAbstractFunction)
{
FuncDeclaration *f;
ISDSYMBOL((f = s->isFuncDeclaration()) != NULL && f->isAbstract())
ISVARDSYMBOL((f = s->isFuncDeclaration()) != NULL && f->isAbstract())
}
else if (ident == Id::isVirtualFunction)
{
FuncDeclaration *f;
ISDSYMBOL((f = s->isFuncDeclaration()) != NULL && f->isVirtual())
ISVARDSYMBOL((f = s->isFuncDeclaration()) != NULL && f->isVirtual())
}
else if (ident == Id::isVirtualMethod)
{
FuncDeclaration *f;
ISDSYMBOL((f = s->isFuncDeclaration()) != NULL && f->isVirtualMethod())
ISVARDSYMBOL((f = s->isFuncDeclaration()) != NULL && f->isVirtualMethod())
}
else if (ident == Id::isFinalFunction)
{
FuncDeclaration *f;
ISDSYMBOL((f = s->isFuncDeclaration()) != NULL && f->isFinal())
ISVARDSYMBOL((f = s->isFuncDeclaration()) != NULL && f->isFinal())
}
#if DMDV2
else if (ident == Id::isStaticFunction)
{
FuncDeclaration *f;
ISDSYMBOL((f = s->isFuncDeclaration()) != NULL && !f->needThis() && !f->isNested())
ISVARDSYMBOL((f = s->isFuncDeclaration()) != NULL && !f->needThis() && !f->isNested())
}
else if (ident == Id::isRef)
{
Expand Down
35 changes: 35 additions & 0 deletions test/runnable/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,40 @@ void test5978() {

/*************************************************************/

template Tuple7829(TL...) { alias TL Tuple7829; }

void test7829()
{
class C
{
final void fvf() { }
void vf() { }
void vm() { }
abstract void af();
final void ff() { }
static void sf() { }
}

static struct S
{
void nvf() { }
C c;
alias c this;
}

S s;
foreach (sym; Tuple7829!(S, s))
{
static assert(!__traits(isVirtualFunction, sym.nvf));
static assert(__traits(isVirtualFunction, sym.fvf));
static assert(__traits(isVirtualFunction, sym.vf));
static assert(__traits(isVirtualMethod, sym.vm));
static assert(__traits(isAbstractFunction, sym.af));
static assert(__traits(isFinalFunction, sym.ff));
static assert(__traits(isStaticFunction, sym.sf));
}
}

int main()
{
test1();
Expand Down Expand Up @@ -996,6 +1030,7 @@ int main()
test7608();
test7858();
test5978();
test7829();

writeln("Success");
return 0;
Expand Down