Order of evaluation of a += a++;

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 30 23:20:21 PDT 2015


On 3/30/15 8:49 PM, deadalnix wrote:
> On Tuesday, 31 March 2015 at 01:01:24 UTC, Andrei Alexandrescu wrote:
>> On 3/30/15 5:49 PM, deadalnix wrote:
>>> Why are you doing the replacement top/down rather than bottom up ?
>>
>> What would be the alternative? -- Andrei
>
> Doing the replacement bottom up :
>
> a = a++;
>
> a += { auto olda = a; a = a + 1; return olda; }();
>
> a = cast(typeof(a)) (a + { auto olda = a; a = a + 1; return olda; }());

You need another lowering here because you evaluate a twice. Consider:

import std.stdio;
int a;
ref int fun() { writeln("meh"); return a; }
void main() {
   fun() = i++;
}

> Other code transformation do not require a function call to take place,
> so why would this require one (which force eager evaluation of
> parameters) ?

Everything needs to be evaluated (and only once).

> That is also inconsistent with LTR evaluation.

I don't see how.

> On the other hand, that
> would make behavior consistent with what opOpAssign would give, so why not.

Whatever makes all sleep at night. Could you please submit a PR for the 
spec with the lowerings discussed?


Andrei



More information about the Digitalmars-d mailing list