Order of evaluation of a += a++;

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 30 23:34:17 PDT 2015


On Tuesday, 31 March 2015 at 06:20:22 UTC, Andrei Alexandrescu 
wrote:
> 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++;
> }
>

Yeah, I had an int in mind. Make it

{
     aPtr = &a;
     *aPtr = cast(typeof(a)) (*aPtr + { auto olda = *aPtr; *aPtr = 
a + 1; return olda; }());
     return *aPtr;
}()

>> 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.
>

a appear on the left of a++. a should be evaluated before a++. 
You propose that it isn't.

>> 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?
>




More information about the Digitalmars-d mailing list