Overflows in Phobos

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 26 11:44:03 PDT 2016


On 26.07.2016 17:44, Johan Engelen wrote:
> 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,

The spec claims that this is true. If it isn't, the spec is in error.

> 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`).

I would assume that the compiler is allowed to remove it from the 
program if it can prove it is unreachable.

> The compiler can assume it is unreachable code, but it has to keep it,

That makes no sense. Those two statements are mutually exclusive.

> so:
> ```
> if (foo()) {
>   // compiler can assume _almost_ zero probability for taking this branch
>   assert(0);
> }
> ```

That makes more sense.


More information about the Digitalmars-d mailing list