Warn about do nothing expressions?

Frustrated Who at where.com
Fri Mar 28 12:35:20 PDT 2014


On Friday, 28 March 2014 at 18:29:27 UTC, QAston wrote:
> On Friday, 28 March 2014 at 18:04:41 UTC, Frustrated wrote:
>> On Friday, 28 March 2014 at 16:54:49 UTC, Benjamin Thaut wrote:
>>> I had a bug which came down to the following line today:
>>>
>>> m_takeIndex = m_takeIndex++;
>>>
>>> Actually this line does nothing. m_takeIndex remains at 
>>> exactly the same value as before (tested with dmd 2.065).
>>>
>>> Can someone please explain why? And would be suitable to warn 
>>> about this, if it is correct behaviour?
>>>
>>> Kind Regards
>>> Benjamin Thaut
>>
>> This should be invalid.
>>
>> m_takeIndex++;
>>
>> actually increments the variable.
>>
>> m_takeIndex = m_takeIndex++; should do exactly the same.
>>
>> It is a bug. It might be correct in monarch's world but it is 
>> not logical. I think monarch is just trying to justify the way 
>> D does it, regardless if D is D is wrong or not.
>>
>> I imagine what is going on is that D is creating a temp 
>> variable, assigning it to the local variable, then 
>> incrementing the temp variable(since++ increments "after" the 
>> assignment).
>>
>> That, or D really is treating that as a nop, which is wrong 
>> too.
>>
>> Test case shows it is probably the first:
>>
>> 	int j = 0, i = 0;
>> 	i = j++;
>> 	// i = 0, j = 1
>>
>> hence D is wrong, Monarch is wrong, etc...
>>
>> m_takeIndex = m_takeIndex++;
>>
>> should, given the example above, evaluate to
>>
>> m_takeIndex = m_takeIndex; (a nop)
>> m_takeIndex++; (variable incremented)
>>
>>
>> hence, D is not consistent with itself, which at the very 
>> least is a bug.
>
> Monarch is right, expression value of i++ is the old i value. 
> Right side of = is evaluated first, so i is incremented and 
> then = is evaluated so it gets it's old value back. This is 
> consistent with the rest of the language, your proposal is a 
> special case.

Nope. Simple wrong. You even state what is happening
contradictory to what actually happens.

i = i++;
vs
i++;

different results. They shouldn't be.

i = i++; could be

i = i;
i++;

or

i++;
i = i;

or

i++;

either way, all increment i, which actually never happens in D.
As was pointed out, VS does it properly... D does it wrong.
Accept it and stop trying to validate how D does it so you can
say D is correct.

Not only is it logically wrong, it is not consistent with
previous interpretations of other compilers/languages nor with
itself. It is wrong on all levels. Just because you believe in
unicorns doesn't prove that they exist. All the evidence says you
are wrong.


What you guys are failing to realize is that

j = i;
i = j++;

is not the same as

i = i;
i = i++;

D also does not make the distinction, hence the error.

i = j++; is logically equivalent to

i = j;
j++;

which means, i = is the previous value of j(before increment).
This should hold true when j is an alias for i.

i = i;
i++;

which means i should be incremented. It is not. Why? Did monarch
code the increment routines in D?

I just wish we could agree that unicorns don't exist.


More information about the Digitalmars-d mailing list