Thread overview
[Issue 17765] void initialisation of out parameters
Aug 22, 2017
Nicholas Wilson
Dec 17, 2022
Iain Buclaw
August 21, 2017
https://issues.dlang.org/show_bug.cgi?id=17765

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com

--- Comment #1 from Steven Schveighoffer <schveiguy@yahoo.com> ---
In the case of out variables, one of the reasons the init is done is to ensure that the data is all written to.

2 things:

1. If the compiler can prove that the out variable is completely written in all
paths, then the initial write can be removed (could be happening already).
2. If the out = void syntax is accepted, and not all the data is written, then
this should really be an error.

Both require advanced flow analysis, and may not be possible in all cases, so the result is that in cases where =void is used, not writing all the data is going to be UB.

Another issue is that the current grammar/syntax defines =X to mean "pass X as parameter if none specified". =void looks weird, and it also doesn't fit the grammar if you have required parameters after it.

I was wondering if this could more of an implementation detail in the function itself.

i.e.:

void g(out float[M][M] corr)
{
    corr = void; // disables the initial write
}

This shouldn't be allowed in @safe code.

--
August 22, 2017
https://issues.dlang.org/show_bug.cgi?id=17765

--- Comment #2 from Nicholas Wilson <iamthewilsonator@hotmail.com> ---
Yeah the compiler was not able to determine that all values were assigned despite there being no conditional logic for the initialisation:

foreach(i; 0 .. M-1)
{
    corr[i][i] = 1.0;
    for (auto j = i+1; j < M; j++)
    {
        corr[i][j] = 0.0;
        for (auto k = 0; k < N; k++)
        corr[i][j] += data[k][i] * data[k][j];
        corr[j][i] = corr[i][j];
    }
}
foreach(i; 0 .. M) corr[M-1][i] = 0.0;
corr[M-1][M-1] = 1.0;

>I was wondering if this could more of an implementation detail in the function itself.
>
> i.e.:
>
> void g(out float[M][M] corr)
> {
>     corr = void; // disables the initial write
> }

That would also work and would probably be less effort in the compiler and less confusing.

> This shouldn't be allowed in @safe code.

Definitely.

--
June 19, 2020
https://issues.dlang.org/show_bug.cgi?id=17765

Илья Ярошенко <ilyayaroshenko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ilyayaroshenko@gmail.com
           Hardware|x86                         |All
                 OS|Mac OS X                    |All

--
June 19, 2020
https://issues.dlang.org/show_bug.cgi?id=17765

--- Comment #3 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---
related issue https://issues.dlang.org/show_bug.cgi?id=20957

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=17765

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--