Skip to content

Commit

Permalink
Fix issue 15653 - IFTI fails for immutable parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
JinShil committed Mar 3, 2018
1 parent d7281c8 commit d68e181
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/dmd/dtemplate.d
Expand Up @@ -4324,6 +4324,16 @@ MATCH deduceType(RootObject o, Scope* sc, Type tparam, TemplateParameters* param
}
}

/* Returns `true` if `t` is a reference type, or an array of reference types
*/
bool isTopRef(Type t)
{
auto tb = t.baseElemOf();
return tb.ty == Tclass ||
tb.ty == Taarray ||
tb.ty == Tstruct && tb.hasPointers();
}

Type at = cast(Type)(*dedtypes)[i];
Type tt;
if (ubyte wx = deduceWildHelper(e.type, &tt, tparam))
Expand All @@ -4335,6 +4345,16 @@ MATCH deduceType(RootObject o, Scope* sc, Type tparam, TemplateParameters* param
{
result = m;
}
else if (!isTopRef(e.type))
{
/* Bugzilla 15653, In IFTI, recognize top-qualifier conversions
* through the value copy, e.g.
* int --> immutable(int)
* immutable(string[]) --> immutable(string)[]
*/
tt = e.type.mutableOf();
result = MATCH.convert;
}
else
return; // nomatch

Expand Down
51 changes: 51 additions & 0 deletions test/runnable/template9.d
Expand Up @@ -4927,6 +4927,56 @@ void test15243()
s3.apply3(&f3);
}

/******************************************/
// 15653

alias TypeTuple15653(T...) = T;

void test15653()
{
void foo(U, T)(const T x) { static assert(is(T == U)); }
void bar(U, T)(immutable T x) { static assert(is(T == U)); }

struct X { int a; long[2] b; }
struct Y { int* a; long[] b; }

foreach (U; TypeTuple15653!( byte, short, int, long,
ubyte, ushort, uint, ulong,
float, double, real,
ifloat, idouble, ireal,
cfloat, cdouble, creal,
void delegate(),
int[2], X, X[2]))
{
foo!U(U.init); // OK
bar!U(U.init); // Was error, now OK

U u;
foo!U(u); // OK
bar!U(u); // Was error, now OK
}

foreach (U; TypeTuple15653!(void*, int**, long[], double*[2]))
{
foo!U(U.init); // OK
bar!U(U.init); // Was error, now OK

U u;
foo!U(u);
static assert(!__traits(compiles, bar!U(u)), U.stringof);
}

foreach (U; TypeTuple15653!(Object, Y, Y[2], int[int]))
{
foo!U(U.init); // OK
static assert(!__traits(compiles, bar!U(U.init)), U.stringof);

U u;
foo!U(u); // OK
static assert(!__traits(compiles, bar!U(u)), U.stringof);
}
}

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

int main()
Expand Down Expand Up @@ -5044,6 +5094,7 @@ int main()
test16042();
test16042b();
test15243();
test15653();

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

0 comments on commit d68e181

Please sign in to comment.