www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compiler generated assertion error message

reply "safety0ff" <safety0ff.dev gmail.com> writes:
A year and a half ago Andrej Mitrovic created a DMD pull ([1]) 
which implements dmd adding a prefix string containing the 
failing assertion to the assertion error's "msg" field.

Unfortunately it triggered code generation bugs, issues #12852 & 
#12855, which have recently been fixed ([2] & [3].)

I have put Andrej's rebased patch in PR #3669 ([4]) and made the 
necessary phobos unit test modifications in [5]. His patch should 
now pass the tests.

An example of how this patch improves the generated "msg" field 
is given in [5], (note that it is generated by the code from [6].)

It is worth noting that the bitfield's assertion message was 
recently modified in [6] to be more informative, perhaps with 
this patch, that change would not have been as necessary.

It would be nice to have some feedback about this change.

Personally, I found it more useful than I had first expected.
I had forgotten that my local DMD was patched until I received a 
failed assertion message which gave me all the context I needed 
within the assertion message.
I recommend giving it a try.

[1] https://github.com/D-Programming-Language/dmd/pull/1426
[2] https://github.com/D-Programming-Language/dmd/pull/3624
[3] https://github.com/D-Programming-Language/dmd/pull/3622
[4] https://github.com/D-Programming-Language/dmd/pull/3669
[5] 
https://github.com/D-Programming-Language/phobos/pull/2252/files
[6] 
https://github.com/D-Programming-Language/phobos/pull/2113/files
Jun 19 2014
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On a related topic:

Feature like this is extremely convenient for unit tests. However 
using assertions in unit test blocks does not fit well with any 
custom test runner that does not immediately terminate the 
application (because AssertionError is an Error).

I'd personally love some way to get such formatted expression for 
any library function.

What is the official stance on this?
Jun 19 2014
parent Jacob Carlborg <doob me.com> writes:
On 2014-06-19 19:52, Dicebot wrote:
 On a related topic:

 Feature like this is extremely convenient for unit tests. However using
 assertions in unit test blocks does not fit well with any custom test
 runner that does not immediately terminate the application (because
 AssertionError is an Error).

There's an assert handler in druntime [1], but that expects the implementation to be nothrow, so you cannot throw an exception.
 I'd personally love some way to get such formatted expression for any
 library function.

 What is the official stance on this?

[1] https://github.com/D-Programming-Language/druntime/blob/master/src/core/exception.d#L374 -- /Jacob Carlborg
Jun 20 2014
prev sibling next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 6/19/2014 10:44 AM, safety0ff wrote:
 A year and a half ago Andrej Mitrovic created a DMD pull ([1]) which implements
 dmd adding a prefix string containing the failing assertion to the assertion
 error's "msg" field.

It appears to do more than that, such as always adding the assertion expression to the output?
Jun 19 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Friday, 20 June 2014 at 20:04:32 UTC, Jacob Carlborg wrote:
 On 2014-06-19 19:52, Dicebot wrote:
 On a related topic:

 Feature like this is extremely convenient for unit tests. 
 However using
 assertions in unit test blocks does not fit well with any 
 custom test
 runner that does not immediately terminate the application 
 (because
 AssertionError is an Error).

There's an assert handler in druntime [1], but that expects the implementation to be nothrow, so you cannot throw an exception.

Yes I have already found it. There is also https://github.com/D-Programming-Language/druntime/blob/master/src/c re/exception.d#L447 but I don't see any way to replace it with user handler. Anyway some sort of library solution (probably via __traits) is much more desired because that will be applicable also to things like std.exception.enforce and alike.
Jun 20 2014