Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
February 11, 2011 Assert compilation failure with certain message | ||||
---|---|---|---|---|
| ||||
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 Re: Assert compilation failure with certain message | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiński | 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 Re: Assert compilation failure with certain message | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiński | 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 Re: Assert compilation failure with certain message | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: Assert compilation failure with certain message | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiński | 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 Re: Assert compilation failure with certain message | ||||
---|---|---|---|---|
| ||||
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 Re: Assert compilation failure with certain message | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiński | 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 Re: Assert compilation failure with certain message | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 Re: Assert compilation failure with certain message | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomek Sowiński | I thought you were just looking for a static assert with a custom message? |
Copyright © 1999-2021 by the D Language Foundation