Bug 1199 – Strange error messages when indexing empty arrays or strings at compile time

Status
RESOLVED
Resolution
FIXED
Severity
trivial
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-04-28T04:13:00Z
Last change time
2014-02-16T15:23:52Z
Keywords
diagnostic
Assigned to
bugzilla
Creator
matti.niemenmaa+dbugzilla

Comments

Comment #0 by matti.niemenmaa+dbugzilla — 2007-04-28T04:13:16Z
// Error: non-constant expression [][__dollar - 1u] char a = (cast(char[])[])[$-1]; Using an immediate value like 0 in place of $-1 gives the sensible message explaining that the index is out of bounds. // Error: string index 4294967295 is out of bounds [0 .. 17540474638172160] char b = ""[$-1]; Doesn't help here, though: // Error: string index 0 is out of bounds [0 .. 17540474638172160] char c = ""[0]; // Ditto char d = ""[$];
Comment #1 by bugzilla — 2007-07-01T13:29:48Z
Fixed DMD 1.018 and DMD 2.002
Comment #2 by matti.niemenmaa+dbugzilla — 2007-07-02T07:33:17Z
The latter two cases still claim that 0 is out of bounds of 0..(big number) (should be 0..0), and in all four cases the error message is output twice.
Comment #3 by bugzilla — 2007-07-02T12:03:06Z
1) 0 is out of bounds in an array of length 0. You cannot access the first element of a 0 length array, and [0] accesses the first element. 2) The big number is -1 as a size_t. Array indices are implicitly converted to size_t. 3) Cascaded error messages are often not very helpful, but they aren't bugs in the compiler.
Comment #4 by matti.niemenmaa+dbugzilla — 2007-07-02T14:01:37Z
My point was: why do ""[0] and ""[$] generate an error talking about 0 out of bounds of [0 .. 43730463654674432] instead of [0 .. 0]? And that number definitely isn't cast(size_t)-1, it doesn't even fit in a 32-bit integer.
Comment #5 by bugzilla — 2007-07-02T15:20:17Z
For the last 2 cases, I get: test2.d(2): Error: string index 0 is out of bounds [0 .. 0] test2.d(2): Error: string index 0 is out of bounds [0 .. 0] test2.d(5): Error: string index 0 is out of bounds [0 .. 0] test2.d(5): Error: string index 0 is out of bounds [0 .. 0] which, while redundant, is correct.
Comment #6 by matti.niemenmaa+dbugzilla — 2007-07-02T22:57:05Z
I'm running 1.018 and get: asdf.d(2): Error: string index 0 is out of bounds [0 .. 43730721352712192] asdf.d(2): Error: string index 0 is out of bounds [0 .. 43730721352712192] asdf.d(5): Error: string index 0 is out of bounds [0 .. 43730721352712192] asdf.d(5): Error: string index 0 is out of bounds [0 .. 43730721352712192] With -inline or -release thrown, it's 43730772892319744 instead, and with both thrown it's 43730824431927296.
Comment #7 by bugzilla — 2007-07-02T23:18:54Z
Please post exactly what the source file you're running is and the switches used.
Comment #8 by matti.niemenmaa+dbugzilla — 2007-07-02T23:28:21Z
Same as the original: char c = ""[0]; char d = ""[$]; "dmd asdf" leads to the errors I mentioned. "dmd asdf -inline", "dmd asdf -release", and "dmd asdf -inline -release" change the ending index in the error message.
Comment #9 by onlystupidspamhere — 2007-07-03T02:08:07Z
(In reply to comment #8) > char c = ""[0]; > char d = ""[$]; > > "dmd asdf" leads to the errors I mentioned. "dmd asdf -inline", "dmd asdf > -release", and "dmd asdf -inline -release" change the ending index in the > error message. I can confirm this. I'm using DMD 1.018 and latest Tango on Linux. Here's the output of dmd -v -c b1199.d: parse b1199 semantic b1199 import object (/home/jm/d/tango/object.di) semantic2 b1199 b1199.d(1): Error: string index 0 is out of bounds [0 .. 582418249900097536] b1199.d(1): Error: string index 0 is out of bounds [0 .. 582419830448062464] b1199.d(2): Error: string index 0 is out of bounds [0 .. 582418249900097536] b1199.d(2): Error: string index 0 is out of bounds [0 .. 582421617154457600]
Comment #10 by bugzilla — 2007-07-30T15:54:17Z
Fixed DMD 1.019 and 2.003