Program logic bugs vs input/environmental errors

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 28 10:40:47 PDT 2014


On 9/28/2014 8:10 AM, Ary Borenszweig wrote:
> For me, assert is useless.
>
> We are developing a language using LLVM as our backend. If you give LLVM
> something it doesn't like, you get something this:
>
> ~~~
> Assertion failed: (S1->getType() == S2->getType() && "Cannot create binary
> operator with two operands of differing type!"), function Create, file
> Instructions.cpp, line 1850.
>
> Abort trap: 6
> ~~~
>
> That is what the user gets when there is a bug in the compiler, at least when we
> are generating invalid LLVM code. And that's one of the good paths, if you
> compiled LLVM with assertions, because otherwise I guess it's undefined behaviour.
>
> What I'd like to do, as a compiler, is to catch those errors and tell the user:
> "You've found a bug in the app, could you please report it in this URL? Thank
> you.". We can't: the assert is there and we can't change it.

You can hook D's assert and do what you want with it.


> Now, this is when you interface with C++/C code. But inside our language code we
> always use exceptions so that programmers can choose what to do in case of an
> error. With assert you loose that possibility.

If you want to use Exceptions for debugging in your code, I won't try and stop 
you. But using them for debugging in official Phobos I strongly object to.


> Installing an exception handler is cost-free,

Take a look at the assembler dump from std.file.copy() that I posted in the 
other thread.


> so I don't see why there is a need
> for a less powerful construct like assert.

Exceptions are meant for RECOVERABLE errors. If you're using them instead of 
assert for logic bugs, you're looking at undefined behavior. Logic bugs are not 
recoverable.



More information about the Digitalmars-d mailing list