Overflows in Phobos

Johan Engelen via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 26 08:44:59 PDT 2016


On Tuesday, 26 July 2016 at 14:28:48 UTC, Timon Gehr wrote:
>
> According to the language documentation, the patch does not fix 
> the problem.
>
> https://dlang.org/spec/expression.html#AssertExpression
>
> "The expression assert(0) is a special case; it signifies that 
> it is unreachable code. [...] The optimization and code 
> generation phases of compilation may assume that it is 
> unreachable code."
>
> One way the optimizer can use the assumption is for optimizing 
> away the overflow check.

This is not true. The spec is unclear, but what makes assert(0) 
special is that the compiler is not allowed to remove it from the 
program, not at any optimization level (other asserts can and 
will be removed by the compiler, see `-release`).
The compiler can assume it is unreachable code, but it has to 
keep it, so:
```
if (foo()) {
   // compiler can assume _almost_ zero probability for taking 
this branch
   assert(0);
}
```


More information about the Digitalmars-d mailing list