enforce (i > 0) for i = int.min does not throw

ag0aep6g anonymous at example.com
Sat Jan 27 14:50:35 UTC 2018


On 01/27/2018 03:13 PM, kdevel wrote:
> I would expect this code
> 
> enforce3.d
> ---
> import std.exception;
> 
> void main ()
> {
>     int i = int.min;
>     enforce (i > 0);
> }
> ---
> 
> to throw an "Enforcement failed" exception, but it doesn't:
> 
> $ dmd enforce3.d
> $ ./enforce3
> [nothing]
> 
> 

Wow, that looks really bad.

Apparently, dmd implements `i < 0` as a `i >> 31`. I.e., it shifts the 
bits to the right so far that only the sign bit is left. This is ok.

But it implements `i > 0` as `(-i) >> 31`. That would be correct if 
negation would always flip the sign bit. But it doesn't for `int.min`. 
`-int.min` is `int.min` again.

So dmd emits wrong code for `i > 0`. O_O

I've filed an issue:
https://issues.dlang.org/show_bug.cgi?id=18315


More information about the Digitalmars-d-learn mailing list