Skip to content

Commit

Permalink
Merge pull request #466 from 9rnsr/fix6832
Browse files Browse the repository at this point in the history
6832 & 6928 & 6929 - Fix common type calculation with alias this
  • Loading branch information
WalterBright committed Nov 16, 2011
2 parents 9e54627 + 8b233a8 commit 57db617
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/cast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,15 @@ int typeMerge(Scope *sc, Expression *e, Type **pt, Expression **pe1, Expression
}
else if (t1->ty == Tstruct && t2->ty == Tstruct)
{
if (t1->mod != t2->mod)
{
unsigned char mod = MODmerge(t1->mod, t2->mod);
t1 = t1->castMod(mod);
t2 = t2->castMod(mod);
t = t1;
goto Lagain;
}

TypeStruct *ts1 = (TypeStruct *)t1;
TypeStruct *ts2 = (TypeStruct *)t2;
if (ts1->sym != ts2->sym)
Expand All @@ -1905,7 +1914,8 @@ int typeMerge(Scope *sc, Expression *e, Type **pt, Expression **pe1, Expression
e1b = resolveProperties(sc, e1b);
i2 = e1b->implicitConvTo(t2);
}
assert(!(i1 && i2));
if (i1 && i2)
goto Lincompatible;

if (i1)
goto Lt1;
Expand All @@ -1920,8 +1930,40 @@ int typeMerge(Scope *sc, Expression *e, Type **pt, Expression **pe1, Expression
{ e2 = e2b;
t2 = e2b->type->toBasetype();
}
t = t1;
goto Lagain;
}
}
else if (t1->ty == Tstruct || t2->ty == Tstruct)
{
if (t1->mod != t2->mod)
{
unsigned char mod = MODmerge(t1->mod, t2->mod);
t1 = t1->castMod(mod);
t2 = t2->castMod(mod);
t = t1;
goto Lagain;
}

if (t1->ty == Tstruct && ((TypeStruct *)t1)->sym->aliasthis)
{
e1 = new DotIdExp(e1->loc, e1, ((TypeStruct *)t1)->sym->aliasthis->ident);
e1 = e1->semantic(sc);
e1 = resolveProperties(sc, e1);
t1 = e1->type;
t = t1;
goto Lagain;
}
if (t2->ty == Tstruct && ((TypeStruct *)t2)->sym->aliasthis)
{
e2 = new DotIdExp(e2->loc, e2, ((TypeStruct *)t2)->sym->aliasthis->ident);
e2 = e2->semantic(sc);
e2 = resolveProperties(sc, e2);
t2 = e2->type;
t = t2;
goto Lagain;
}
goto Lincompatible;
}
else if ((e1->op == TOKstring || e1->op == TOKnull) && e1->implicitConvTo(t2))
{
Expand Down
64 changes: 64 additions & 0 deletions test/runnable/aliasthis.d
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,67 @@ mixin template Wrapper6479()
}

/**********************************************/
// 6832

void test6832()
{
static class Foo { }
static struct Bar { Foo foo; alias foo this; }
Bar bar;
bar = new Foo; // ok
assert(bar !is null); // ng

struct Int { int n; alias n this; }
Int a;
int b;
auto c = (true ? a : b); // TODO
assert(c == a);
}

/**********************************************/
// 6928

void test6928()
{
struct T { int* p; } // p is necessary.
T tx;

struct S {
T get() const { return tx; }
alias get this;
}

immutable(S) s;
immutable(T) t;
static assert(is(typeof(1? s:t))); // ok.
static assert(is(typeof(1? t:s))); // ok.
static assert(is(typeof(1? s:t)==typeof(1? t:s))); // fail.

auto x = 1? t:s; // ok.
auto y = 1? s:t; // compile error.
}

/**********************************************/
// 6929

struct S6929
{
T6929 get() const { return T6929.init; }
alias get this;
}
struct T6929
{
S6929 get() const { return S6929.init; }
alias get this;
}
void test6929()
{
T6929 t;
S6929 s;
static assert(!is(typeof(1? t:s)));
}

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

int main()
{
Expand All @@ -655,6 +716,9 @@ int main()
test6434();
test6366();
test6759();
test6832();
test6928();
test6929();

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

0 comments on commit 57db617

Please sign in to comment.