www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6438] New: [CTFE] wrong error "value used before set" when slicing =void array

reply d-bugmail puremagic.com writes:
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



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: -------
Aug 05 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6438


Don <clugdbug yahoo.com.au> changed:

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



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: -------
Aug 05 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6438




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: -------
Aug 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6438




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


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: -------
Mar 19 2012
prev sibling parent d-bugmail puremagic.com writes:
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: -------
Mar 19 2012