July 12, 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
July 12, 2017
On Wednesday, 12 July 2017 at 20:12:03 UTC, Steven Schveighoffer wrote:
>...
> Which means this may cause a bunch of nuisance errors.

It's a trade-off between nuisance in some cases (the Phobos ones can be solved with val = abs(val), or with static if), and possibly catching bugs in other cases.

We can compare this with negation of a bool and subtracting a bool:
```
bool b;
auto x = 1 - b; // allowed
auto y = -b; // not allowed
```

July 12, 2017
On 7/11/2017 12:46 PM, Johan Engelen wrote:
> So, adding the error may be nice, but it would make generic code a little more verbose.

The particular issue you were having appears to be a bug in the compiler (I already filed it as a bug report). Being a bug, we need more evidence that adding an error for -u is compelling.

There really isn't a comprehensive solution for mistakes using unsigned. Some languages deal with the issue by not having an unsigned type at all (Java). At some level, it's necessary to just be aware of different integer sizes, integral promotion rules, what happens with overflows, and sign.
July 13, 2017
On 7/12/17 5:24 PM, Johan Engelen wrote:
> On Wednesday, 12 July 2017 at 20:12:03 UTC, Steven Schveighoffer wrote:
>> ...
>> Which means this may cause a bunch of nuisance errors.
> 
> It's a trade-off between nuisance in some cases (the Phobos ones can be solved with val = abs(val), or with static if), and possibly catching bugs in other cases.
> 
> We can compare this with negation of a bool and subtracting a bool:
> ```
> bool b;
> auto x = 1 - b; // allowed
> auto y = -b; // not allowed
> ```
> 

My only point is with Andrei's assertion that it "exposed two bugs in Phobos".

In terms of the trade-off, it looks like this is just a straight bug that should be fixed. If the behavior goes back to being like C, is it still so bad that it needs an error?

My testing:

import std.stdio;
void main()
{
    ubyte x = 5;
    int y = -x;
    writeln(y);
    uint t = 0;
    t += x;
    writeln(t);
    t += y;
    writeln(t);
}

outputs:
251
5
256

In C:

#include <stdio.h>
int main()
{
    unsigned char x = 5;
    int y = -x;
    printf("%d\n", y);
    unsigned int t = 0;
    t += x;
    printf("%u\n", t);
    t += y;
    printf("%u\n", t);
    return 0;
}

outputs:
-5
5
0

The C behavior seems fine to me.

-Steve
1 2
Next ›   Last »