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
hpohl committed Aug 11, 2013
1 parent 314a1cc commit 6650dc1
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/declaration.h
Expand Up @@ -831,8 +831,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 @@ -850,7 +850,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
46 changes: 36 additions & 10 deletions src/func.c
Expand Up @@ -4102,22 +4102,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 @@ -4127,12 +4127,38 @@ void StaticCtorDeclaration::semantic(Scope *sc)
//printf("StaticCtorDeclaration::semantic()\n");

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

if (storage_class & STC_TYPECTOR)
{
unsigned char mod = 0;
if (storage_class & STCshared && !isSharedStaticCtorDeclaration())
{
::error(loc, "to create a shared static constructor, move 'shared' in front of 'static'");
storage_class &= ~STCshared;
}
if (storage_class & STCimmutable)
mod = MODimmutable;
else
{
if (storage_class & STCconst)
mod |= MODconst;
else if (storage_class & STCwild)
mod |= MODwild;
if (storage_class & STCshared)
mod |= MODshared;
}
if (mod)
::error(loc, "%sstatic constructors cannot be '%s'",
isSharedStaticCtorDeclaration() ? "shared " : "",
MODtoChars(mod));
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 @@ -4213,15 +4239,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
6 changes: 4 additions & 2 deletions src/parse.c
Expand Up @@ -1211,8 +1211,9 @@ StaticCtorDeclaration *Parser::parseStaticCtor()
nextToken();
check(TOKlparen);
check(TOKrparen);
StorageClass stc = parsePostfix();

StaticCtorDeclaration *f = new StaticCtorDeclaration(loc, Loc());
StaticCtorDeclaration *f = new StaticCtorDeclaration(loc, Loc(), stc);
parseContracts(f);
return f;
}
Expand All @@ -1232,8 +1233,9 @@ SharedStaticCtorDeclaration *Parser::parseSharedStaticCtor()
nextToken();
check(TOKlparen);
check(TOKrparen);
StorageClass stc = parsePostfix();

SharedStaticCtorDeclaration *f = new SharedStaticCtorDeclaration(loc, Loc());
SharedStaticCtorDeclaration *f = new SharedStaticCtorDeclaration(loc, Loc(), stc);
parseContracts(f);
return f;
}
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, move 'shared' in front of 'static'
fail_compilation/diag6677.d(5): Error: to create a shared static constructor, move 'shared' in front of 'static'
fail_compilation/diag6677.d(5): Error: static constructors cannot be 'const'
fail_compilation/diag6677.d(7): Error: shared static constructors cannot be 'const'
fail_compilation/diag6677.d(8): Error: shared static constructors cannot be 'inout'
fail_compilation/diag6677.d(9): Error: shared static constructors cannot be 'immutable'
fail_compilation/diag6677.d(10): Error: shared static constructors cannot be 'shared'
fail_compilation/diag6677.d(11): Error: shared static constructors cannot be 'shared const'
---
*/

#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 6650dc1

Please sign in to comment.