Skip to content

Commit

Permalink
Merge pull request #2076 from WalterBright/revert93towarning
Browse files Browse the repository at this point in the history
make initialized const fields a warning
  • Loading branch information
9rnsr committed May 27, 2013
1 parent 44fc669 commit a584488
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 28 deletions.
20 changes: 17 additions & 3 deletions src/declaration.c
@@ -1,6 +1,6 @@

// Compiler implementation of the D programming language
// Copyright (c) 1999-2012 by Digital Mars
// Copyright (c) 1999-2013 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
Expand Down Expand Up @@ -84,7 +84,7 @@ Declaration::Declaration(Identifier *id)
linkage = LINKdefault;
inuse = 0;
sem = SemanticStart;
mangleOverride = NULL;
mangleOverride = NULL;
}

void Declaration::semantic(Scope *sc)
Expand Down Expand Up @@ -1185,7 +1185,7 @@ void VarDeclaration::semantic(Scope *sc)
AggregateDeclaration *aad = parent->isAggregateDeclaration();
if (aad)
{
#if DMDV2
#if PULL93
assert(!(storage_class & (STCextern | STCstatic | STCtls | STCgshared)));
if (storage_class & (STCconst | STCimmutable) && init &&
global.params.vfield)
Expand All @@ -1194,6 +1194,16 @@ void VarDeclaration::semantic(Scope *sc)
const char *s = (storage_class & STCimmutable) ? "immutable" : "const";
fprintf(stderr, "%s: %s.%s is %s field\n", p ? p : "", ad->toPrettyChars(), toChars(), s);
}
#else
if (storage_class & (STCconst | STCimmutable) && init)
{
StorageClass stc = storage_class & (STCconst | STCimmutable);
warning(loc, "%s field with initializer should be static, __gshared, or an enum",
StorageClassDeclaration::stcToChars(NULL, stc));
if (!tb->isTypeBasic())
storage_class |= STCstatic;
}
else
#endif
{
storage_class |= STCfield;
Expand Down Expand Up @@ -1912,6 +1922,10 @@ AggregateDeclaration *VarDeclaration::isThis()
if (!(storage_class & (STCstatic | STCextern | STCmanifest | STCtemplateparameter |
STCtls | STCgshared | STCctfe)))
{
#if !PULL93
if ((storage_class & (STCconst | STCimmutable | STCwild)) && init)
return NULL;
#endif
for (Dsymbol *s = this; s; s = s->parent)
{
ad = s->isMember();
Expand Down
2 changes: 2 additions & 0 deletions src/expression.c
Expand Up @@ -7498,7 +7498,9 @@ Expression *DotVarExp::semantic(Scope *sc)
accessCheck(loc, sc, e1, var);

VarDeclaration *v = var->isVarDeclaration();
#if PULL93
if (v && (v->isDataseg() || (v->storage_class & STCmanifest)))
#endif
{
Expression *e = expandVar(WANTvalue, v);
if (e)
Expand Down
9 changes: 9 additions & 0 deletions src/mars.c
Expand Up @@ -621,11 +621,18 @@ int tryMain(size_t argc, char *argv[])
{
if (strcmp(p + 12, "?") == 0)
{
#if PULL93
printf("\
Language changes listed by -transition=id:\n\
=field,3449 do list all non-mutable fields occupies object instance\n\
=tls do list all variables going into thread local storage\n\
");
#else
printf("\
Language changes listed by -transition=id:\n\
=tls do list all variables going into thread local storage\n\
");
#endif
return EXIT_FAILURE;
}
if (isdigit((unsigned char)p[12]))
Expand All @@ -648,8 +655,10 @@ Language changes listed by -transition=id:\n\
{
if (strcmp(p + 12, "tls") == 0)
global.params.vtls = 1;
#if PULL93
else if (strcmp(p + 12, "field") == 0)
global.params.vfield = 1;
#endif
}
else
goto Lerror;
Expand Down
1 change: 1 addition & 0 deletions src/mars.h
Expand Up @@ -88,6 +88,7 @@ void unittests();
#define DMDV2 1 // Version 2.0 features
#define SNAN_DEFAULT_INIT DMDV2 // if floats are default initialized to signalling NaN
#define MODULEINFO_IS_STRUCT DMDV2 // if ModuleInfo is a struct rather than a class
#define PULL93 0 // controversial pull #93 for bugzilla 3449

// Set if C++ mangling is done by the front end
#define CPP_MANGLE (DMDV2 && (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS))
Expand Down
25 changes: 0 additions & 25 deletions test/compilable/sw_transition_3449.d

This file was deleted.

4 changes: 4 additions & 0 deletions test/compilable/sw_transition_field.d
Expand Up @@ -3,6 +3,10 @@
/*
TEST_OUTPUT:
---
---
*/
/*
---
compilable/sw_transition_field.d(15): sw_transition_field.S1.ix is immutable field
compilable/sw_transition_field.d(16): sw_transition_field.S1.cx is const field
compilable/sw_transition_field.d(21): sw_transition_field.S2!(immutable(int)).S2.f is immutable field
Expand Down
7 changes: 7 additions & 0 deletions test/runnable/test3449.d
@@ -1,3 +1,5 @@
version (PULL93)
{
template TypeTuple(T...) { alias TypeTuple = T; }

// If module variable has no explicit initializer,
Expand Down Expand Up @@ -83,3 +85,8 @@ void main()
assert(s2.field2 == 10);
}
}
}
else
{
void main() { }
}
25 changes: 25 additions & 0 deletions test/runnable/testconst.d
Expand Up @@ -533,8 +533,16 @@ struct S40

void test40()
{
version (PULL93)
{
assert(S40.sizeof == 8);
assert(S40.init.b == 3);
}
else
{
assert(S40.sizeof == 4);
assert(S40.b == 3);
}
}

/************************************/
Expand Down Expand Up @@ -566,7 +574,14 @@ class C42
{
int a = ctfe() - 2;
const int b;
version (PULL93)
{
enum int c = ctfe();
}
else
{
const int c = ctfe();
}
static const int d;
static const int e = ctfe() + 2;

Expand All @@ -584,7 +599,14 @@ class C42
void test42()
{
printf("%d\n", C42.classinfo.init.length);
version (PULL93)
{
assert(C42.classinfo.init.length == 12 + (void*).sizeof + (void*).sizeof);
}
else
{
assert(C42.classinfo.init.length == 8 + (void*).sizeof + (void*).sizeof);
}
C42 c = new C42;
assert(c.a == 1);
assert(c.b == 2);
Expand All @@ -595,8 +617,11 @@ void test42()
const(int)*p;
p = &c.b;
assert(*p == 2);
version (PULL93)
{
p = &c.c;
assert(*p == 3);
}
p = &c.d;
assert(*p == 4);
p = &c.e;
Expand Down

0 comments on commit a584488

Please sign in to comment.