[Bug d/97843] Bad code gen when concatenating to array

alex at sunopti dot com gcc-bugzilla@gcc.gnu.org
Mon Nov 16 20:56:54 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97843

--- Comment #6 from Alex <alex at sunopti dot com> ---
>From cppreference.com :

The behavior of every builtin compound-assignment expression E1 op= E2 (where
E1 is a modifiable lvalue expression and E2 is an rvalue expression or a
braced-init-list (since C++11)) is exactly the same as the behavior of the
expression E1 = E1 op E2.

I think the D spec should contain something similar. It's omission is an
oversight. I don't think D developers intend different expression evaluation
behavior to C++.

If the above were part of the D spec, the equivalent code would be :

bytes = bytes ~ bytes.sum

The assignment must happen after the ~.

In the same way that the ~= must happen after the sum in bytes ~= bytes.sum

Maybe I should raise an issue with the D spec ?
Either way I don't think the compiler should do this because it doesn't make
sense. This is the first compiler release where my unit tests have detected
this behaviour.

>ref ubyte[] extend(ref ubyte[] bytes)
>{
>    bytes.length += 1;
>    bytes[$-1] = 0xde;
>    return bytes;
>}

>extend(bytes)[bytes.length] = bytes.sum;

This would be ok if a trailing ~ meant 'extend the array and return the last
element'. Then it could be evaluated before the assignment.

The assignment operator ~= should be considered to be both the ~ and the =
grouped together.


More information about the Gcc-bugs mailing list