[Issue 3560] foreach over nested function generates wrong code

2015-06-09 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=3560

Andrei Alexandrescu  changed:

   What|Removed |Added

Version|D1 & D2 |D2

--


[Issue 3560] foreach over nested function generates wrong code

2010-07-24 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=3560


Walter Bright  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 CC||bugzi...@digitalmars.com
 Resolution||FIXED


--- Comment #5 from Walter Bright  2010-07-24 
16:24:48 PDT ---
http://www.dsource.org/projects/dmd/changeset/586

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 3560] foreach over nested function generates wrong code

2010-07-15 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=3560


Don  changed:

   What|Removed |Added

   Keywords||patch


--- Comment #4 from Don  2010-07-15 14:42:34 PDT ---
This is a front-end problem.
ForeachStatement::semantic() immediately runs aggr->semantic(). 
In this case, aggr is an address of a nested function 'inner'.
AddrExp->semantic() turns it into a DelegateExp.

Later on in Foreach::semantic, if the aggregate is of delegate type, it wraps
it in a CallExp, then runs CallExp::semantic. The problem is that CallExp
assumes that semantic has NOT been run on its argument.
This works fine if the aggregate is a delegate variable. But if it's a delegate
expression, CallExp transforms the DelegateExp for 'inner' into a
DotVarExp(inner, inner). And then it's a mess.  CallExp needs to be passed
'inner', not the delegateExp.

I can see two possible fixes. 
(1) Change CallExp so that changes DelegateExp(f,f) into CallExp(f), if f is a
nested function, instead of changing it into a DotVarExp.

OR
(2) If it's a delegate expression for a nested function, call the function
directly.
This patch implements the second method.

statement.c, Foreach::semantic(), line 1891

else if (tab->ty == Tdelegate)
{
/* Call:
 *  aggr(flde)
 */
Expressions *exps = new Expressions();
exps->push(flde);

+if (aggr->op == TOKdelegate &&
+((DelegateExp *)aggr)->func->isNested())
+e = new CallExp(loc, ((DelegateExp *)aggr)->e1, exps);
+else
+e = new CallExp(loc, aggr, exps);
-e = new CallExp(loc, aggr, exps);
e = e->semantic(sc);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 3560] foreach over nested function generates wrong code

2010-07-14 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=3560


Don  changed:

   What|Removed |Added

Version|2.036   |D1 & D2
Summary|foreach on closure  |foreach over nested
   |corrupted after function|function generates wrong
   |call|code


--- Comment #3 from Don  2010-07-14 12:41:39 PDT ---
It doesn't even need a function call, and doesn't need a closure.
Anything which modifies the EAX register before the foreach will do it.
This test case generates bad code (runtime segfault) even on D1 (even old
versions, eg D1.020).
The inner function assumes that EAX contains the context pointer, but the
foreach code doesn't set EAX. Instead, it's passing the context pointer in EBX.
=
void main()
{
int localvar = 7;

int inner(int delegate(ref int) dg) {
int k = localvar;
return 0;
}

int a = localvar * localvar; // This modifies the EAX register

foreach (entry; &inner)
{
}
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---