Skip to content

Commit

Permalink
Merge pull request #1344 from 9rnsr/fix9109
Browse files Browse the repository at this point in the history
Issue 9109 - Regression: 2.053: Lazy Variadic Functions do not work with delegates
  • Loading branch information
Don Clugston committed Dec 3, 2012
2 parents c377a64 + f331f9f commit 0a08e95
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
10 changes: 8 additions & 2 deletions src/expression.c
Expand Up @@ -988,8 +988,14 @@ Type *functionParameters(Loc loc, Scope *sc, TypeFunction *tf,

for (size_t u = i; u < nargs; u++)
{ Expression *a = (*arguments)[u];
if (tret && !((TypeArray *)tb)->next->equals(a->type))
a = a->toDelegate(sc, tret);
TypeArray *ta = (TypeArray *)tb;
if (tret && !ta->next->equals(a->type))
{ if (tret->toBasetype()->ty == Tvoid ||
a->implicitConvTo(tret))
{
a = a->toDelegate(sc, tret);
}
}

Expression *e = new VarExp(loc, v);
e = new IndexExp(loc, e, new IntegerExp(u + 1 - nparams));
Expand Down
15 changes: 5 additions & 10 deletions src/mtype.c
Expand Up @@ -6009,7 +6009,6 @@ MATCH TypeFunction::callMatch(Expression *ethis, Expressions *args, int flag)
{
Expression *arg = (*args)[u];
assert(arg);
#if 1
if (arg->op == TOKfunction)
{
arg = ((FuncExp *)arg)->inferType(tb->nextOf(), 1);
Expand All @@ -6024,23 +6023,19 @@ MATCH TypeFunction::callMatch(Expression *ethis, Expressions *args, int flag)
if (tret)
{
if (ta->next->equals(arg->type))
{ m = MATCHexact;
}
m = MATCHexact;
else if (tret->toBasetype()->ty == Tvoid)
m = MATCHconvert;
else
{
m = arg->implicitConvTo(tret);
if (m == MATCHnomatch)
{
if (tret->toBasetype()->ty == Tvoid)
m = MATCHconvert;
}
m = arg->implicitConvTo(ta->next);
}
}
else
m = arg->implicitConvTo(ta->next);
#else
m = arg->implicitConvTo(ta->next);
#endif

if (m == MATCHnomatch)
goto Nomatch;
if (m < match)
Expand Down
60 changes: 38 additions & 22 deletions test/runnable/lazy.d
Expand Up @@ -6,37 +6,38 @@ import std.stdio;
void ifthen(bool cond, lazy void dg)
{
if (cond)
dg();
dg();
}

void ifthen(bool cond, lazy void dgthen, lazy void dgelse)
{
if (cond)
dgthen();
dgthen();
else
dgelse();
dgelse();
}

void dotimes(int i, lazy int dg)
{
for (int j = 0; j < i; j++)
dg();
dg();
}

void switcher(bool delegate()[] cases...)
{
foreach (c; cases)
{
if (c())
break;
if (c())
break;
}
}

bool scase(bool b, lazy void dg)
{
if (b)
{ dg();
return true;
{
dg();
return true;
}
return false;
}
Expand All @@ -50,7 +51,7 @@ bool sdefault(lazy void dg)
void whiler(lazy bool cond, lazy void bdy)
{
while (cond())
bdy();
bdy();
}

void test1()
Expand All @@ -66,14 +67,14 @@ void test1()

int v = 2;
switcher(
scase(v == 1, printf("it is 1\n")),
scase(v == 2, printf("it is 2\n")),
scase(v == 3, printf("it is 3\n")),
sdefault( printf("it is default\n"))
scase(v == 1, printf("it is 1\n")),
scase(v == 2, printf("it is 2\n")),
scase(v == 3, printf("it is 3\n")),
sdefault( printf("it is default\n"))
);

whiler( x < 100,
(printf("%d\n", x), x *= 2)
(printf("%d\n", x), x *= 2)
);
}

Expand Down Expand Up @@ -111,7 +112,6 @@ void abc3(int* p)

void test3()
{

int x = 3;
dotimes3(10, abc3(&x));
dotimes3(10, write(++x));
Expand Down Expand Up @@ -143,8 +143,8 @@ void test4()
{
void *abc()
{
writeln(cast(void*)&p4);
return cast(void*)&p4;
writeln(cast(void*)&p4);
return cast(void*)&p4;
}

foo4(&abc, cast(void* delegate())&abc, null, cast(void* delegate())null);
Expand All @@ -154,18 +154,18 @@ void test4()

bool nextis(void delegate() dgpositive = {})
{
return true;
return true;
}

bool looping(lazy bool condition)
{
return true;
return true;
}

void test5()
{
looping(nextis({}));
looping(nextis({}));
looping(nextis({}));
looping(nextis({}));
}

/*********************************************************/
Expand All @@ -182,7 +182,7 @@ int bar6() { return 3; }

void test6()
{
foo6(bar6(),"food_for_thought");
foo6(bar6(),"food_for_thought");
}

/*********************************************************/
Expand Down Expand Up @@ -232,6 +232,21 @@ void test6682()
assert(purefunc6682() == 1);
}

/*********************************************************/
// 9109

void test9109()
{
void foo(int delegate()[] dgs ...)
{
assert(dgs[0]() + dgs[1]() == 1 + 13);
}

int x = 10;
int delegate() dg;
foo({ return 1; }, { return 3+x; }, dg, null);
}

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

int main()
Expand All @@ -246,6 +261,7 @@ int main()
test5750a();
test5750b();
test6682();
test9109();

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

0 comments on commit 0a08e95

Please sign in to comment.