digitalmars.D - Error message for unreachable code
- Oskar Linde (34/34) Apr 07 2006 Hi,
- Bruno Medeiros (7/53) Apr 08 2006 Yes, it doesn't seem good to me either. As for the resolution, hum, I
- Walter Bright (3/8) Apr 11 2006 It comes about from constant folding. It would be difficult for the
- Lionello Lunesu (4/13) Apr 12 2006 How come? The compiler clearly knows the bounds of x at the instruction
Hi, The following code: void main() { char[0] x; if (x.length > 0) { char y = x[0]; } } Refuses to compile with the following error message: zeroarray.d(4): array index [0] is outside array bounds [0 .. 0] And before someone asks why I would declare a zero length static array I better tell that this is occurs in template code where the type of x can be both static and dynamic arrays. My current workaround looks like this: template ZeroLengthStaticArray(X:X[0]) { alias X ZeroLengthStaticArray; } void main() { char[0] x; static if(!is(ZeroLengthStaticArray!(typeof(x)))) { if (x.length > 0) { char y = x[0]; } } } (There are of course other ways, but they all make the code less readable.) I realize that this could potentially be problematic to correct. I could live with the workaround, especially if it is anything but trivial to fix. I see this as an interesting case of where DMD issues an error for valid code, where a typical C compiler at most would issue a warning. The generated code (with or without const folding) will be correct. I would like to hear why DMD considers the above an error while for instance functions without return values are not. /Oskar
Apr 07 2006
Oskar Linde wrote:Hi, The following code: void main() { char[0] x; if (x.length > 0) { char y = x[0]; } } Refuses to compile with the following error message: zeroarray.d(4): array index [0] is outside array bounds [0 .. 0] And before someone asks why I would declare a zero length static array I better tell that this is occurs in template code where the type of x can be both static and dynamic arrays. My current workaround looks like this: template ZeroLengthStaticArray(X:X[0]) { alias X ZeroLengthStaticArray; } void main() { char[0] x; static if(!is(ZeroLengthStaticArray!(typeof(x)))) { if (x.length > 0) { char y = x[0]; } } } (There are of course other ways, but they all make the code less readable.) I realize that this could potentially be problematic to correct. I could live with the workaround, especially if it is anything but trivial to fix. I see this as an interesting case of where DMD issues an error for valid code, where a typical C compiler at most would issue a warning. The generated code (with or without const folding) will be correct. I would like to hear why DMD considers the above an error while for instance functions without return values are not. /OskarYes, it doesn't seem good to me either. As for the resolution, hum, I think ideally the compiler should be smart enough to detect such unreachable code and ignore errors that will never come to happen. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Apr 08 2006
Oskar Linde wrote:I see this as an interesting case of where DMD issues an error for valid code, where a typical C compiler at most would issue a warning. The generated code (with or without const folding) will be correct. I would like to hear why DMD considers the above an error while for instance functions without return values are not.It comes about from constant folding. It would be difficult for the compiler to tell that this code is unreachable.
Apr 11 2006
Walter Bright wrote:Oskar Linde wrote:How come? The compiler clearly knows the bounds of x at the instruction x[0], so why not constant fold the x.length as well? L.I see this as an interesting case of where DMD issues an error for valid code, where a typical C compiler at most would issue a warning. The generated code (with or without const folding) will be correct. I would like to hear why DMD considers the above an error while for instance functions without return values are not.It comes about from constant folding. It would be difficult for the compiler to tell that this code is unreachable.
Apr 12 2006