Bug 21166 – error message when unittesting std/array depends on -cov and -O switches

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-08-16T04:36:57Z
Last change time
2022-02-27T06:43:05Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Steven Schveighoffer

Comments

Comment #0 by schveiguy — 2020-08-16T04:36:57Z
If you clone the current phobos master, and run the unittest specifically for std/array by doing: make -f posix.mak std/array.test you will get the error: Error: variable theArray used before set For the return statement in the following code in std.array: auto ret = (() @trusted { Unqual!U[n] theArray = void; return theArray; }()); I reduced this using dustmite and hand editing down to the following code: auto staticArray(size_t U, R)(R input) { import std.range; alias ElementType!R T; auto res = () @trusted { T[U] theArray = void; return theArray; }(); foreach(i, x; input) res[i] = x; return res; } @safe void main() { auto input = [0, 1]; auto arr = input.staticArray!2; assert(arr[] == input); } This code fails with: dmd -O -cov file.d If you remove either of those options, it compiles and runs. If you change the static array line from input.staticArray!2 to input.staticArray!3 (and change the input size accordingly), it compiles and runs. I tested all the way back to 2.060 on run.dlang.io, and it fails for every version.
Comment #1 by bugzilla — 2022-02-27T06:43:05Z
> Unqual!U[n] theArray = void; > return theArray; But it *is* used before set! Setting it to `void` means it is not set to anything, i.e. is unset. It happens with -O because the optimizer uses data flow analysis to detect these things. The front end has no ability to do it. The other things that affect it likely come down to an interaction between the -cov and the optimizer. I suggest fixing the code so it doesn't use uninitialized variables.