assert and enforce both compiled out with -release

Steven Schveighoffer schveiguy at yahoo.com
Sun Jan 28 19:31:25 UTC 2018


On 1/27/18 9:31 AM, Ali Çehreli wrote:
> On 01/27/2018 05:52 AM, kdevel wrote:
>  > https://dlang.org/phobos/std_exception.html#enforce states:
>  >
>  > | Also, do not use enforce inside of contracts (i.e. inside of in and
>  > out blocks
>  > | and invariants), because they will be compiled out when compiling with
>  > -release.
>  > | Use assert in contracts.
>  >
>  > But assert is also ignored in release mode:
> 
> The documentation is not clear. "they will be compiled out" means 
> "contracts are compiled out". So, an enforce() would disappear if it's 
> inside such a block, which should not be what the programmer wants for 
> an enforce().

There is another problem with using enforce in contracts, it throws 
Exceptions, whereas contracts are expected to throw Errors. The most 
important aspect about this is that nothrow functions can still throw 
Errors. So if you have a nothrow function with an in-contract that 
throws an Exception, compiling in release mode may is a mistake (I'm not 
sure if the compiler even detects this).

I think the current docs (as they have been modified) are fine.

> Fixed it through the "Improve this page" link on that Phobos page:
> 
>    https://github.com/dlang/phobos/blob/master/std/exception.d
> 
> Ali

Hm... it appears that you committed directly to the branch. Even if 
changing docs, you should do so with a pull request.

For normal mortals, this happens automatically, but for people who have 
commit rights, you have to be careful to select the right thing ;)

-Steve


More information about the Digitalmars-d-learn mailing list