Error on negating unsigned types

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 12 13:12:03 PDT 2017


On 7/12/17 11:00 AM, Andrei Alexandrescu wrote:
> On 07/11/2017 03:46 PM, Johan Engelen wrote:
>> So, adding the error may be nice, but it would make generic code a 
>> little more verbose.
>> Ideas? People OK with that?
> 
> A compelling argument is that the rule exposed two bugs in Phobos. -- 
> Andrei
> 

Those aren't bugs, they are false negatives (pun?).

``` (std.format.formatIntegral)
     T arg = val;

     immutable negative = (base == 10 && arg < 0);
     if (negative) // will always be false for unsigned types
     {
         arg = -arg;
     }
```

``` (std.conv.toTextRange)
     T value;

     bool negative = value < 0; // also will always be false
     Unqual!(Unsigned!T) v = negative ? -value : value;
```

Not sure if the compiler can figure that out. Which means this may cause 
a bunch of nuisance errors.

-Steve


More information about the Digitalmars-d mailing list