Struct assignment, possible DMD bug?
Timon Gehr
timon.gehr at gmx.ch
Sat Sep 29 11:16:52 PDT 2012
On 09/29/2012 06:26 PM, Maxim Fomin wrote:
> On Saturday, 29 September 2012 at 16:05:03 UTC, ixid wrote:
>> This behaviour seems inconsistent and unintuitive:
>>
>> void main() {
>> int[3] a = [1,2,3];
>> a = [4, a[0], 6];
>>
>> struct S {
>> int a, b, c;
>> }
>>
>> S s = S(1,2,3);
>> s = S(4, s.a, 6);
>>
>> assert(a == [4,1,6]);
>> assert(s == S(4,4,6));
>> }
>>
>> Setting the struct writes s.a before evaluating it while the reverse
>> is true of the array assignment. Using DMD 2.0.60. GDC does what I'd
>> expect and gives both as 4,1,6.
>
> I think this is notorious "i = ++i + ++i".
There is only one mutating sub-expression.
> Statement s = S(4, s.a, 6) writes to s object and simultaneously reads it.
> http://dlang.org/expression.html states that assign expression is
> evaluated in implementation defined-manner and it is an error to depend
> on things like this.
No evaluation order of the assignment expression can possibly lead to
this result. This seems to be a DMD bug.
More information about the Digitalmars-d-learn
mailing list