Skip to content

Commit

Permalink
fix issue 6677 - static this attributes position
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jun 2, 2014
2 parents 9d48c47 + 6650dc1 commit f0a7c56
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/declaration.h
Expand Up @@ -815,8 +815,8 @@ class DtorDeclaration : public FuncDeclaration
class StaticCtorDeclaration : public FuncDeclaration
{
public:
StaticCtorDeclaration(Loc loc, Loc endloc);
StaticCtorDeclaration(Loc loc, Loc endloc, const char *name);
StaticCtorDeclaration(Loc loc, Loc endloc, StorageClass stc);
StaticCtorDeclaration(Loc loc, Loc endloc, const char *name, StorageClass stc);
Dsymbol *syntaxCopy(Dsymbol *);
void semantic(Scope *sc);
AggregateDeclaration *isThis();
Expand All @@ -833,7 +833,7 @@ class StaticCtorDeclaration : public FuncDeclaration
class SharedStaticCtorDeclaration : public StaticCtorDeclaration
{
public:
SharedStaticCtorDeclaration(Loc loc, Loc endloc);
SharedStaticCtorDeclaration(Loc loc, Loc endloc, StorageClass stc);
Dsymbol *syntaxCopy(Dsymbol *);
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);

Expand Down
36 changes: 26 additions & 10 deletions src/func.c
Expand Up @@ -4791,22 +4791,22 @@ void DtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)

/********************************* StaticCtorDeclaration ****************************/

StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc)
StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc, StorageClass stc)
: FuncDeclaration(loc, endloc,
Identifier::generateId("_staticCtor"), STCstatic, NULL)
Identifier::generateId("_staticCtor"), STCstatic | stc, NULL)
{
}

StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc, const char *name)
StaticCtorDeclaration::StaticCtorDeclaration(Loc loc, Loc endloc, const char *name, StorageClass stc)
: FuncDeclaration(loc, endloc,
Identifier::generateId(name), STCstatic, NULL)
Identifier::generateId(name), STCstatic | stc, NULL)
{
}

Dsymbol *StaticCtorDeclaration::syntaxCopy(Dsymbol *s)
{
assert(!s);
StaticCtorDeclaration *scd = new StaticCtorDeclaration(loc, endloc);
StaticCtorDeclaration *scd = new StaticCtorDeclaration(loc, endloc, storage_class);
return FuncDeclaration::syntaxCopy(scd);
}

Expand All @@ -4816,12 +4816,28 @@ void StaticCtorDeclaration::semantic(Scope *sc)
//printf("StaticCtorDeclaration::semantic()\n");

if (scope)
{ sc = scope;
{
sc = scope;
scope = NULL;
}

if (storage_class & STCshared && !isSharedStaticCtorDeclaration())
{
::error(loc, "to create a shared static constructor, use 'shared static this'");
storage_class &= ~STCshared;
}

if (storage_class & (STCimmutable | STCconst | STCshared | STCwild))
{
OutBuffer buf;
StorageClassDeclaration::stcToCBuffer(&buf, storage_class & (STCimmutable | STCconst | STCshared | STCwild));
::error(loc, "static constructors cannot be %s", buf.peekString());
}

storage_class &= ~STC_TYPECTOR; // remove qualifiers

if (!type)
type = new TypeFunction(NULL, Type::tvoid, false, LINKd);
type = new TypeFunction(NULL, Type::tvoid, false, LINKd, storage_class);

/* If the static ctor appears within a template instantiation,
* it could get called multiple times by the module constructors
Expand Down Expand Up @@ -4902,15 +4918,15 @@ void StaticCtorDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)

/********************************* SharedStaticCtorDeclaration ****************************/

SharedStaticCtorDeclaration::SharedStaticCtorDeclaration(Loc loc, Loc endloc)
: StaticCtorDeclaration(loc, endloc, "_sharedStaticCtor")
SharedStaticCtorDeclaration::SharedStaticCtorDeclaration(Loc loc, Loc endloc, StorageClass stc)
: StaticCtorDeclaration(loc, endloc, "_sharedStaticCtor", stc)
{
}

Dsymbol *SharedStaticCtorDeclaration::syntaxCopy(Dsymbol *s)
{
assert(!s);
SharedStaticCtorDeclaration *scd = new SharedStaticCtorDeclaration(loc, endloc);
SharedStaticCtorDeclaration *scd = new SharedStaticCtorDeclaration(loc, endloc, storage_class);
return FuncDeclaration::syntaxCopy(scd);
}

Expand Down
20 changes: 4 additions & 16 deletions src/parse.c
Expand Up @@ -1364,16 +1364,10 @@ Dsymbol *Parser::parseStaticCtor()
nextToken();
check(TOKlparen);
check(TOKrparen);
StorageClass stc = parsePostfix(NULL);

//StorageClass stc = parsePostfix(&udas);
StaticCtorDeclaration *f = new StaticCtorDeclaration(loc, Loc());
StaticCtorDeclaration *f = new StaticCtorDeclaration(loc, Loc(), stc);
Dsymbol *s = parseContracts(f);
//if (udas)
//{
// Dsymbols *a = new Dsymbols();
// a->push(f);
// s = new UserAttributeDeclaration(udas, a);
//}
return s;
}

Expand All @@ -1393,16 +1387,10 @@ Dsymbol *Parser::parseSharedStaticCtor()
nextToken();
check(TOKlparen);
check(TOKrparen);
StorageClass stc = parsePostfix(NULL);

//StorageClass stc = parsePostfix(&udas);
SharedStaticCtorDeclaration *f = new SharedStaticCtorDeclaration(loc, Loc());
SharedStaticCtorDeclaration *f = new SharedStaticCtorDeclaration(loc, Loc(), stc);
Dsymbol *s = parseContracts(f);
//if (udas)
//{
// Dsymbols *a = new Dsymbols();
// a->push(f);
// s = new UserAttributeDeclaration(udas, a);
//}
return s;
}

Expand Down
29 changes: 29 additions & 0 deletions test/fail_compilation/diag6677.d
@@ -0,0 +1,29 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag6677.d(1): Error: static constructors cannot be const
fail_compilation/diag6677.d(2): Error: static constructors cannot be inout
fail_compilation/diag6677.d(3): Error: static constructors cannot be immutable
fail_compilation/diag6677.d(4): Error: to create a shared static constructor, use 'shared static this'
fail_compilation/diag6677.d(5): Error: to create a shared static constructor, use 'shared static this'
fail_compilation/diag6677.d(5): Error: static constructors cannot be const
fail_compilation/diag6677.d(7): Error: static constructors cannot be const
fail_compilation/diag6677.d(8): Error: static constructors cannot be inout
fail_compilation/diag6677.d(9): Error: static constructors cannot be immutable
fail_compilation/diag6677.d(10): Error: static constructors cannot be shared
fail_compilation/diag6677.d(11): Error: static constructors cannot be const shared
---
*/

#line 1
static this() const { }
static this() inout { }
static this() immutable { }
static this() shared { }
static this() const shared { }

shared static this() const { }
shared static this() inout { }
shared static this() immutable { }
shared static this() shared { }
shared static this() const shared { }
26 changes: 26 additions & 0 deletions test/runnable/statictor.d
Expand Up @@ -28,6 +28,30 @@ class Bar
static ~this() {printf("Bar static dtor\n");}
}

/***********************************************/
// 6677

int global6677;

static this() nothrow pure @safe
{
int* p;
static assert(!__traits(compiles, ++p));
static assert(!__traits(compiles, ++global6677));
// BUG (not related to this):
//auto throwit = { throw new Exception("sup"); };
//static assert(!__traits(compiles, throwit() )); // Should pass
//throwit() // This fails, but __traits(compiles, ...) claims it compiles
}

shared static this() nothrow pure @safe
{
int* p;
static assert(!__traits(compiles, ++p));
static assert(!__traits(compiles, ++global6677));
}

/***********************************************/
// 7533
struct Foo7533(int n)
{
Expand All @@ -36,6 +60,8 @@ struct Foo7533(int n)

alias Foo7533!5 Bar7533;

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

void main()
{
}
Expand Down

0 comments on commit f0a7c56

Please sign in to comment.