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

Teodor Dutu teodor.dutu at gmail.com
Tue Oct 12 16:27:51 UTC 2021


Hi,

I've been working on [this 
PR](https://github.com/dlang/dmd/pull/13116) for a while now and 
after seeing it fail some tests in phobos (for example, [this 
one](https://cirrus-ci.com/task/5609501660282880?logs=test_phobos#L116)), my mentors for SAoC 2021, Razvan Nitu and Eduard Staniloiu, and I figured out that, when lowered using `const` or `immutable` arguments, `_d_arrayctor` becomes a strongly pure function. For instance, in the code snippet below
```d
struct S {};
const S[2] b;
const S[2] a = b;
```
the line `const S[2] a = b` is lowered to `_d_arrayctor(a, b)`, 
which is the intended behaviour.

However, since, in this case, `_d_arrayctor` is **strongly pure** 
and since its return value is ignored, the compiler issues the 
warning in the failed test above. In addition, its strong purity 
might also cause calls to `_d_arrayctor` to be removed by the 
compiler as part of the optimisation phase.

In order to avoid such unwanted events, the only solution we 
could come up with was to force `_d_arrayctor` to be **weakly 
pure** instead. We achieved this by adding a third pointer-type 
parameter, as implemented in [this 
PR](https://github.com/dlang/druntime/pull/3587). But this is 
merely a stop-gap solution, because it acts against the language, 
by denying one of its properties: purity.

We also tried changing the type of either `to` or the `from` 
parameters to a mutable `void[]`, but in this case the compiler 
was unable to instantiate the function's template correctly. So 
this solution didn't work.

Have you faced this issue before? What were your solutions?

Thanks,
Teodor


More information about the Digitalmars-d mailing list