Lowerings to strongly pure _d_arrayctor trigger warnings or risk being incorrectly removed

Teodor Dutu teodor.dutu at gmail.com
Tue Oct 12 18:04:21 UTC 2021


On Tuesday, 12 October 2021 at 16:46:00 UTC, Paul Backus wrote:
> On Tuesday, 12 October 2021 at 16:27:51 UTC, Teodor Dutu wrote:
>> ```d
>> struct S {};
>> const S[2] b;
>> const S[2] a = b;
>> ```
>
> I think the fundamental problem is that what `_d_arrayctor` is 
> doing here is undefined behavior, according to the language 
> spec:
>
>> Note that casting away a const qualifier and then mutating is 
>> undefined behavior, too, even when the referenced data is 
>> mutable.
>
> Source: https://dlang.org/spec/const3.html#removing_with_cast
>
> The spec makes [a special exception][1] for `struct` and 
> `class` constructors, which allows them to write to non-mutable 
> memory once without invoking UB, but there is no corresponding 
> exception for variables that are not part of a `struct` or 
> `class`.
>
> [1]: https://dlang.org/spec/struct.html#field-init

I have read the documentation at the links you left, but I can't 
see why the usage of `_d_arrayctor` would be an undefined 
behaviour. This lowering only occurs when initialising static 
arrays or slices, whereas the removal of the `immutable` or 
`const` qualifiers that you mentioned is showcased like this:
```d
immutable int* p = ...;
int* q = cast(int*)p;
```
And this has nothing to do with array initialisations, so I think 
I'm in the clear with using `_d_arrayctor`.

Do you think I'm missing anything?


More information about the Digitalmars-d mailing list