Thread overview
[Issue 6438] New: [CTFE] wrong error "value used before set" when slicing =void array
Aug 05, 2011
Dmitry Olshansky
Aug 06, 2011
Don
Aug 06, 2011
Dmitry Olshansky
Mar 19, 2012
Walter Bright
August 05, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6438

           Summary: [CTFE] wrong error "value used before set" when
                    slicing =void array
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: dmitry.olsh@gmail.com


--- Comment #0 from Dmitry Olshansky <dmitry.olsh@gmail.com> 2011-08-05 03:00:04 PDT ---
void fillWithZero(T)(T[] arr)
{
    foreach(ref x; arr)
        x = 0;
}

T[4] f(T)()
{
    T[4] stackSpace = void;
    fillWithZero(stackSpace[]);
    return stackSpace;
}

static assert(f!int() == [0,0,0,0]);

Fails with:
bug6XXX.d(11): Error: variable stackSpace is used before initialization
bug6XXX.d(11): Error: cannot evaluate fillWithZero(stackSpace[]) at compile
time
bug6XXX.d(16): Error: cannot evaluate f() at compile time
bug6XXX.d(16): Error: static assert  (cast(int[])f() == [0,0,0,0]) is not
evaluatable at compile time

It's CTFE only issue and is a blocker for one overload of phobos insertInPlace.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6438


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2011-08-05 23:43:35 PDT ---
That's difficult. Implementing this would be a major feature, since assignment from void has a high risk of undefined behaviour.

The f() function is passing a reference to an uninitialized value, to another
function. In this particular case, it does initialize every member without
reading from any of them, but that's difficult to determine.
I guess it could be implemented by introducing an 'uninitialized expression',
but then there are problems with this:

void fillWithZero(T)(T[] arr)
{
    auto q = arr[0];   // consider this line
    foreach(ref x; arr)
        x = 0;
}

T[4] f(T)()
{
    T[4] stackSpace = void;
    fillWithZero(stackSpace[]);
    return stackSpace;
}

static assert(f!int() == [0,0,0,0]);

The assignment to q should generate a 'variable used before set' error. But to be comprehensible, it should also refer to the line where stackSpace was initialized.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6438



--- Comment #2 from Dmitry Olshansky <dmitry.olsh@gmail.com> 2011-08-06 09:41:59 PDT ---
I see, somehow I hadn't occured to me that it would require a lot of extra work (and storage) to keep track of all uninitialized slots of array.

Apart from variadic overload of insertInPlace I wouldn't expect this pattern to be too common, so it won't be a big issue.

It was propmted by:
insertInPlace(a, x, [b,c]); //allocation here is plain ridiculous

so I proposed a more generalized version that allowed multiple arguments (that
also solves this particular problem):
insertInPlace(a, x, b, c); // initializes tmp array with b, c on stack and
inserts

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 19, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6438



--- Comment #3 from github-bugzilla@puremagic.com 2012-03-19 11:26:46 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/f152b09d8f59a4981bff924802bebaccb926f948
Fix issue 6438 - [CTFE] wrong error "value used before set" when slicing =void
array

Take advantage of the new VoidExp.

https://github.com/D-Programming-Language/dmd/commit/b71836bbc5e83af949484401e6f97346e7d66a2d Merge pull request #816 from donc/voidctfe6438

Fix issue 6438 - [CTFE] wrong error "value used before set" with = void

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 19, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6438


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------