Problem with integral promotions

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 24 08:25:34 PDT 2017


On 7/22/17 6:44 AM, Walter Bright wrote:
> They are supposed to match C. But they don't for unary operators + - ~, 
> and as far as I can tell never did.

My opinion is that it should behave unsurprisingly.

It's very surprising behavior for:

ubyte x = 1;
int y = -x;

To make y = 255.

My recommendation:

1. issue a warning for using any of those operators an unsigned type 
without an unambiguous cast. In other words:

ubyte x = 1;
-x; // warning
cast(ubyte)-x; // OK, not changing size
cast(int)-x; // warning
-cast(int)x; // OK

2. Change the warning to a deprecation.
3. Change to an error

optionally, you could finally implement the C behavior after this and 
remove the error, but I'm actually thinking that the number of times 
this comes up is probably so few that it's worth requiring the cast.

Note, the negation of byte.min or short.min is not as surprising as the 
negation of unsigned types, as int or long have the same behavior, and 
it's a well-known corner case.

Also note, you did not include char/wchar in your list of affected 
types, though they have the same behavior with those operators as ubyte 
and ushort.

-Steve


More information about the Digitalmars-d mailing list