www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Error message for unreachable code

reply Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
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
next sibling parent Bruno Medeiros <brunodomedeirosATgmail SPAM.com> writes:
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.
 
 /Oskar
Yes, 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
prev sibling parent reply Walter Bright <newshound digitalmars.com> writes:
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
parent Lionello Lunesu <lio lunesu.remove.com> writes:
Walter Bright wrote:
 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.
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.
Apr 12 2006