www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assert compilation failure with certain message

reply Tomek =?ISO-8859-2?Q?Sowi=F1ski?= <just ask.me> writes:
Is there a way to statically assert compilation of an expression failed *with a
certain message*? I want to check my static asserts trip when they should.

-- 
Tomek
Feb 10 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Tomek Sowiński:

 Is there a way to statically assert compilation of an expression failed *with
a certain message*? I want to check my static asserts trip when they should.

I have asked something like this a lot of time ago, but I don't know a way to do it. You are able to statically assert that some code doesn't compile, but I don't know how to assert that a certain message gets produced. You are asking for a specific static catch :-) Bye, bearophile
Feb 10 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
How's this?

import std.stdio;
import std.conv;

void staticAssert(alias exp, string message, string file = __FILE__,
int line = __LINE__)()
{
    static if (!exp)
    {
        pragma(msg, file ~ ":(" ~ to!string(line) ~ ") " ~
"staticAssert: " ~  to!string(message));
        assert(0);
    }
}

void main()
{
    enum x = false;
    staticAssert!(x, "Oh no we failed!");

    int y;
}
Feb 10 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I've managed to screw up the colon placement though, here's a quick fix:

import std.stdio;
import std.conv;

void staticAssert(alias exp, string message, string file = __FILE__,
int line = __LINE__)()
{
    static if (!exp)
    {
        pragma(msg, file ~ "(" ~ to!string(line) ~ "): " ~
"staticAssert: " ~  to!string(message));
        assert(0);
    }
}

void main()
{
    enum x = false;
    staticAssert!(x, "Oh no we failed!");

    int y;
}
Feb 10 2011
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Tomek S.:

 Static catch, yeah. But I'd be content with traits__(fails, expr, msg) which
seems tractable.

Asking for new features in this newsgroup is not so useful. You may add it to bugzilla... Bye, bearophile
Feb 10 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I thought you were just looking for a static assert with a custom message?
Feb 11 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, February 10, 2011 16:12:01 Tomek Sowi=C5=84ski wrote:
 Is there a way to statically assert compilation of an expression failed
 *with a certain message*? I want to check my static asserts trip when they
 should.

You mean like static assert(0, "We have a failure, Captain!"); If a static assert fails, it's obvious. Compilation fails. Now, if you're t= rying=20 to assert something like that a particular template instantiation fails, th= e use=20 static assert(!__traits(compiles, exp)); where exp is the expression being= =20 tested. =2D Jonathan M Davis
Feb 10 2011
prev sibling next sibling parent Tomek =?ISO-8859-2?Q?Sowi=F1ski?= <just ask.me> writes:
bearophile napisa=B3:

 Is there a way to statically assert compilation of an expression failed=


 my static asserts trip when they should. =20

I have asked something like this a lot of time ago, but I don't know a wa=

 assert that some code doesn't compile, but I don't know how to assert tha=

 asking for a specific static catch :-)

Static catch, yeah. But I'd be content with traits__(fails, expr, msg) whic= h seems tractable. --=20 Tomek
Feb 10 2011
prev sibling parent Tomek =?ISO-8859-2?Q?Sowi=F1ski?= <just ask.me> writes:
Andrej Mitrovic napisa=B3:

 I've managed to screw up the colon placement though, here's a quick fix:
=20
 import std.stdio;
 import std.conv;
=20
 void staticAssert(alias exp, string message, string file =3D __FILE__,
 int line =3D __LINE__)()
 {
     static if (!exp)
     {
         pragma(msg, file ~ "(" ~ to!string(line) ~ "): " ~
 "staticAssert: " ~  to!string(message));
         assert(0);
     }
 }
=20
 void main()
 {
     enum x =3D false;
     staticAssert!(x, "Oh no we failed!");
=20
     int y;
 }

How does it help to find out that compilation tripped on a specific static = assertion? --=20 Tomek
Feb 11 2011