www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Request: behaviour of static assert() in templates

reply Don Clugston <dac nospam.com.au> writes:
The behaviour of static assert() has changed again with DMD 0.145, and 
it tends not to trigger error messages any more, as in the example 
below. I included a pragma(msg) to show that the static assert is 
actually evaluated, but ignored. Variants of this can crash the 
compiler, but I haven't cut it down to a simple case yet.
--------
template echidna(int z)
{
   pragma(msg, "This actually gets evaluated!");
   const bool quoll = false;
}

template dingo(int a)
{
   static if (a==2) {
     static assert(echidna!(a).quoll);
   } else const int dingo = a;
}
const int bilby = dingo!(2);
-----
An annoying aspect of this behaviour is that you are not given the line 
which asserted.

But rather than simply fix some superficial bugs, there's a potential 
for much better behaviour.

(1) It would be *much* nicer if static assert and pragma() did not
interfere with the implicit promotion of template members.
Both static assert() and pragma(msg, ...) are very useful in tracking 
down template bugs, but since they alter the effect of the program,
they're extremely awkward to use right now (and it seems conceptually 
wrong).

(2) Ideally, when a failed static assert() occurred, the compiler would 
simply show the context (follow the syntax tree back down to the root, 
printing the line being evaluated, but without any new error messages).
This might be as simple as setting a global 
"supressParasiticErrorMessages" boolean. (?) There's really no point in 
trying to continue compiling once you've hit a static assert. Too much 
of the symbol table is erroneous. (In DMD 0.144, once a static assert 
failed, _every_ template instantiation from then on would fail! Not sure 
if 0.145 is the same).

Together, these two things would allow D template libraries to make 
highly user-friendly error messages (in marked contrast to the STL!)
Jan 30 2006
parent Kyle Furlong <kylefurlong gmail.com> writes:
Don Clugston wrote:
 The behaviour of static assert() has changed again with DMD 0.145, and 
 it tends not to trigger error messages any more, as in the example 
 below. I included a pragma(msg) to show that the static assert is 
 actually evaluated, but ignored. Variants of this can crash the 
 compiler, but I haven't cut it down to a simple case yet.
 --------
 template echidna(int z)
 {
   pragma(msg, "This actually gets evaluated!");
   const bool quoll = false;
 }
 
 template dingo(int a)
 {
   static if (a==2) {
     static assert(echidna!(a).quoll);
   } else const int dingo = a;
 }
 const int bilby = dingo!(2);
 -----
 An annoying aspect of this behaviour is that you are not given the line 
 which asserted.
 
 But rather than simply fix some superficial bugs, there's a potential 
 for much better behaviour.
 
 (1) It would be *much* nicer if static assert and pragma() did not
 interfere with the implicit promotion of template members.
 Both static assert() and pragma(msg, ...) are very useful in tracking 
 down template bugs, but since they alter the effect of the program,
 they're extremely awkward to use right now (and it seems conceptually 
 wrong).
 
 (2) Ideally, when a failed static assert() occurred, the compiler would 
 simply show the context (follow the syntax tree back down to the root, 
 printing the line being evaluated, but without any new error messages).
 This might be as simple as setting a global 
 "supressParasiticErrorMessages" boolean. (?) There's really no point in 
 trying to continue compiling once you've hit a static assert. Too much 
 of the symbol table is erroneous. (In DMD 0.144, once a static assert 
 failed, _every_ template instantiation from then on would fail! Not sure 
 if 0.145 is the same).
 
 Together, these two things would allow D template libraries to make 
 highly user-friendly error messages (in marked contrast to the STL!)

I agree with Don, when static assert fails, the compile should stop, then show the line of the assert which failed.
Jan 30 2006