Skip to content

Commit

Permalink
Merge pull request #965 from 9rnsr/fix8099
Browse files Browse the repository at this point in the history
Issue 8099 - Inner class's outer pointer matches constancy of inner, but can be set to object of arbitrary constancy
  • Loading branch information
WalterBright committed May 25, 2012
2 parents dfb9f29 + 6499c5c commit 4c9b474
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/expression.c
Expand Up @@ -4386,6 +4386,13 @@ Expression *NewExp::semantic(Scope *sc)
sc = sc->push(cdthis);
type = newtype->semantic(loc, sc);
sc = sc->pop();

if (!MODimplicitConv(thisexp->type->mod, newtype->mod))
{
error("nested type %s should have the same or weak constancy as enclosing type %s",
newtype->toChars(), thisexp->type->toChars());
goto Lerr;
}
}
else
{
Expand Down
36 changes: 36 additions & 0 deletions test/runnable/testconst.d
Expand Up @@ -2681,6 +2681,41 @@ void test8098()
static assert(is(typeof(x.inner) == const(Outer8098.Inner)));
}

/************************************/
// 8099

void test8099()
{
static class Outer
{
class Inner {}
}

auto m = new Outer;
auto c = new const(Outer);
auto i = new immutable(Outer);

auto mm = m.new Inner; // m -> m OK
auto mc = m.new const(Inner); // m -> c OK
static assert(!__traits(compiles, {
auto mi = m.new immutable(Inner); // m -> i bad
}));

static assert(!__traits(compiles, {
auto cm = c.new Inner; // c -> m bad
}));
auto cc = c.new const(Inner); // c -> c OK
static assert(!__traits(compiles, {
auto ci = c.new immutable(Inner); // c -> i bad
}));

static assert(!__traits(compiles, {
auto im = i.new Inner; // i -> m bad
}));
auto ic = i.new const(Inner); // i -> c OK
auto ii = i.new immutable(Inner); // i -> i OK
}

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

int main()
Expand Down Expand Up @@ -2796,6 +2831,7 @@ int main()
test7669();
test7757();
test8098();
test8099();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 4c9b474

Please sign in to comment.