Thread overview
Assert compilation failure with certain message
Feb 11, 2011
Tomek Sowiński
Feb 11, 2011
bearophile
Feb 11, 2011
Tomek Sowiński
Feb 11, 2011
Andrej Mitrovic
Feb 11, 2011
Andrej Mitrovic
Feb 11, 2011
Tomek Sowiński
Feb 11, 2011
Andrej Mitrovic
Feb 11, 2011
bearophile
Feb 11, 2011
Jonathan M Davis
February 11, 2011
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
February 11, 2011
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
February 11, 2011
On Thursday, February 10, 2011 16:12:01 Tomek Sowiński 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 trying to assert something like that a particular template instantiation fails, the use static assert(!__traits(compiles, exp)); where exp is the expression being tested.

- Jonathan M Davis
February 11, 2011
bearophile napisał:

> > 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 :-)

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

-- 
Tomek

February 11, 2011
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;
}
February 11, 2011
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;
}
February 11, 2011
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
February 11, 2011
Andrej Mitrovic napisał:

> 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;
> }

How does it help to find out that compilation tripped on a specific static assertion?

-- 
Tomek

February 11, 2011
I thought you were just looking for a static assert with a custom message?