Skip to content

Commit

Permalink
Merge pull request #5450 from quickfur/issue15638
Browse files Browse the repository at this point in the history
typeMerge should take modifiers into account when searching for common base class
  • Loading branch information
yebblies committed Feb 13, 2016
2 parents 969e090 + ee586e9 commit c2c0362
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/dcast.d
Expand Up @@ -2508,7 +2508,7 @@ extern (C++) Type rawTypeMerge(Type t1, Type t2)
*/
extern (C++) bool typeMerge(Scope* sc, TOK op, Type* pt, Expression* pe1, Expression* pe2)
{
//printf("typeMerge() %s op %s\n", (*pe1)->toChars(), (*pe2)->toChars());
//printf("typeMerge() %s op %s\n", pe1.toChars(), pe2.toChars());
MATCH m;
Expression e1 = *pe1;
Expression e2 = *pe2;
Expand Down Expand Up @@ -2804,8 +2804,8 @@ Lagain:
ClassDeclaration cd2 = tc2.sym.baseClass;
if (cd1 && cd2)
{
t1 = cd1.type;
t2 = cd2.type;
t1 = cd1.type.castMod(t1.mod);
t2 = cd2.type.castMod(t2.mod);
}
else if (cd1)
t1 = cd1.type;
Expand Down
43 changes: 43 additions & 0 deletions test/runnable/xtest46.d
Expand Up @@ -7736,6 +7736,48 @@ void test15369()
assert(*p == '\0');
}

void test15638()
{
class A {}
class B : A {}
class C : A {}

B b;
C c;
const(B) cb;
const(C) cc;
immutable(B) ib;
immutable(C) ic;

// Common type for const derived classes
auto constCommon = true ? cb : cc;
static assert(is(typeof(constCommon) == const(A)));

// Common type for immutable derived classes
auto immutableCommon = true ? ib : ic;
static assert(is(typeof(immutableCommon) == immutable(A)));

// Common type for mixed const/immutable derived classes
auto mixed1 = true ? cb : ic;
static assert(is(typeof(mixed1) == const(A)));
auto mixed2 = true ? ib : cc;
static assert(is(typeof(mixed2) == const(A)));

// Common type for mixed mutable/immutable derived classes
auto mixed3 = true ? b : ic;
static assert(is(typeof(mixed3) == const(A)));
auto mixed4 = true ? ib : c;
static assert(is(typeof(mixed4) == const(A)));

// Array literal type deduction
auto arr1 = [ new immutable(B), new C ];
auto arr2 = [ new B, new const(C) ];
auto arr3 = [ new immutable(B), new immutable(C) ];
static assert(is(typeof(arr1) == const(A)[]));
static assert(is(typeof(arr2) == const(A)[]));
static assert(is(typeof(arr3) == immutable(A)[]));
}

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

int main()
Expand Down Expand Up @@ -8051,6 +8093,7 @@ int main()
test14211();
test15141();
test15369();
test15638();

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

0 comments on commit c2c0362

Please sign in to comment.