Skip to content

Commit

Permalink
Merge pull request #800 from 9rnsr/fix_type_deduction
Browse files Browse the repository at this point in the history
Fix type merging and deduction bugs
  • Loading branch information
WalterBright committed Mar 11, 2012
2 parents 59c8a34 + b05ed87 commit a86266e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 13 deletions.
13 changes: 6 additions & 7 deletions src/mtype.c
Expand Up @@ -417,6 +417,8 @@ Type *Type::mutableOf()
t = t->merge();
t->fixTo(this);
}
else
t = t->merge();
assert(t->isMutable());
return t;
}
Expand Down Expand Up @@ -508,6 +510,8 @@ Type *Type::unSharedOf()

t->fixTo(this);
}
else
t = t->merge();
assert(!t->isShared());
return t;
}
Expand Down Expand Up @@ -1864,14 +1868,11 @@ Type *Type::substWildTo(unsigned mod)
else if (ty == Tsarray)
t = new TypeSArray(t, ((TypeSArray *)this)->dim->syntaxCopy());
else if (ty == Taarray)
{
t = new TypeAArray(t, ((TypeAArray *)this)->index->syntaxCopy());
t = t->merge();
}
else
assert(0);

t = t->addMod(this->mod);
t = t->merge();
}
}
else
Expand Down Expand Up @@ -2464,9 +2465,7 @@ Type *TypeNext::makeMutable()
{
//printf("TypeNext::makeMutable() %p, %s\n", this, toChars());
TypeNext *t = (TypeNext *)Type::makeMutable();
if ((ty != Tfunction && next->ty != Tfunction &&
//(next->deco || next->ty == Tfunction) &&
next->isWild()) || ty == Tsarray)
if (ty == Tsarray)
{
t->next = next->mutableOf();
}
Expand Down
12 changes: 7 additions & 5 deletions src/template.c
Expand Up @@ -1934,9 +1934,7 @@ FuncDeclaration *TemplateDeclaration::deduceFunctionTemplate(Scope *sc, Loc loc,
goto Lerror;
}
{
tdargs->setDim(dedargs.dim);
memcpy(tdargs->data, dedargs.data, tdargs->dim * sizeof(void *));
fd = td->doHeaderInstantiation(sc, tdargs, fargs);
fd = td->doHeaderInstantiation(sc, &dedargs, fargs);
if (!fd)
goto Lerror;
}
Expand Down Expand Up @@ -2301,13 +2299,17 @@ MATCH Type::deduceType(Scope *sc, Type *tparam, TemplateParameters *parameters,
{
switch (X(tparam->mod, mod))
{
case X(MODwild, MODwild):
case X(MODwild | MODshared, MODwild | MODshared):
case X(MODwild, 0):
case X(MODwild, MODshared):
case X(MODwild, MODconst):
case X(MODwild, MODconst | MODshared):
case X(MODwild, MODimmutable):
case X(MODwild, MODwild):
case X(MODwild, MODwild | MODshared):
case X(MODwild | MODshared, MODshared):
case X(MODwild | MODshared, MODconst | MODshared):
case X(MODwild | MODshared, MODimmutable):
case X(MODwild | MODshared, MODwild | MODshared):

if (!at)
{
Expand Down
77 changes: 77 additions & 0 deletions test/runnable/template9.d
Expand Up @@ -1016,6 +1016,78 @@ alias T7643!(long, "x", string, "y") Specs7643;

alias T7643!( Specs7643[] ) U7643; // Error: tuple A is used as a type

/**********************************/
// 7671

inout(int)[3] id7671n1 ( inout(int)[3] );
inout( U )[n] id7671x1(U, size_t n)( inout( U )[n] );

shared(inout int)[3] id7671n2 ( shared(inout int)[3] );
shared(inout U )[n] id7671x2(U, size_t n)( shared(inout U )[n] );

void test7671()
{
static assert(is( typeof( id7671n1( (immutable(int)[3]).init ) ) == immutable(int[3]) ));
static assert(is( typeof( id7671x1( (immutable(int)[3]).init ) ) == immutable(int[3]) ));

static assert(is( typeof( id7671n2( (immutable(int)[3]).init ) ) == immutable(int[3]) ));
static assert(is( typeof( id7671x2( (immutable(int)[3]).init ) ) == immutable(int[3]) ));
}

/************************************/
// 7672

T foo7672(T)(T a){ return a; }

void test7672(inout(int[]) a = null, inout(int*) p = null)
{
static assert(is( typeof( a ) == inout(int[]) ));
static assert(is( typeof(foo7672(a)) == inout(int)[] ));

static assert(is( typeof( p ) == inout(int*) ));
static assert(is( typeof(foo7672(p)) == inout(int)* ));
}

/**********************************/
// 7684

U[] id7684(U)( U[] );
shared(U[]) id7684(U)( shared(U[]) );

void test7684()
{
shared(int)[] x;
static assert(is( typeof(id7684(x)) == shared(int)[] ));
}

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

inout(U)[] id11a(U)( inout(U)[] );
inout(U[]) id11a(U)( inout(U[]) );
inout(shared(U[])) id11a(U)( inout(shared(U[])) );

void test11a(inout int _ = 0)
{
shared(const(int))[] x;
static assert(is( typeof(id11a(x)) == shared(const(int))[] ));

shared(int)[] y;
static assert(is( typeof(id11a(y)) == shared(int)[] ));

inout(U)[n] idz(U, size_t n)( inout(U)[n] );

inout(shared(bool[1])) z;
static assert(is( typeof(idz(z)) == inout(shared(bool[1])) ));
}

inout(U[]) id11b(U)( inout(U[]) );

void test11b()
{
alias const(shared(int)[]) T;
static assert(is(typeof(id11b(T.init)) == const(shared(int)[])));
}

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

int main()
Expand Down Expand Up @@ -1057,6 +1129,11 @@ int main()
test7416();
test7563();
test7580();
test7671();
test7672();
test7684();
test11a();
test11b();

printf("Success\n");
return 0;
Expand Down
12 changes: 11 additions & 1 deletion test/runnable/testconst.d
Expand Up @@ -1426,7 +1426,7 @@ void test82(inout(int) _ = 0)
pragma(msg, typeof(o));
static assert(typeof(o).stringof == "inout(char*****)");
pragma(msg, typeof(cast()o));
static assert(typeof(cast()o).stringof == "char*****");
static assert(typeof(cast()o).stringof == "inout(char****)*");

const(char*****) p;
pragma(msg, typeof(p));
Expand Down Expand Up @@ -2549,6 +2549,15 @@ void test7518() {
stuff.empty();
}

/************************************/
// 7669

shared(inout U)[n] id7669(U, size_t n)( shared(inout U)[n] );
void test7669()
{
static assert(is(typeof( id7669((shared(int)[3]).init)) == shared(int)[3]));
}

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

int main()
Expand Down Expand Up @@ -2659,6 +2668,7 @@ int main()
test7202();
test7554();
test7518();
test7669();

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

0 comments on commit a86266e

Please sign in to comment.