D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6438 - [CTFE] wrong error "value used before set" when slicing =void array
Summary: [CTFE] wrong error "value used before set" when slicing =void array
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 major
Assignee: yebblies
URL:
Keywords: CTFE, rejects-valid
Depends on:
Blocks:
 
Reported: 2011-08-05 03:00 UTC by Dmitry Olshansky
Modified: 2012-03-19 11:56 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Dmitry Olshansky 2011-08-05 03:00:04 UTC
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.
Comment 1 Don 2011-08-05 23:43:35 UTC
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.
Comment 2 Dmitry Olshansky 2011-08-06 09:41:59 UTC
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
Comment 3 github-bugzilla 2012-03-19 11:26:46 UTC
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