Why does intpromote spew warnings for ~ operator?

Steven Schveighoffer schveiguy at gmail.com
Mon Sep 13 20:35:08 UTC 2021


On 9/13/21 3:55 PM, Walter Bright wrote:
> On 9/13/2021 4:42 AM, Steven Schveighoffer wrote:
>> C allows exactly what he wrote, and it already works the same as it 
>> does in C.
> 
> Allowing it is one thing, behaving the same is another.
> 
> I found the problem *because* a difference in behavior popped up.
> 

The behavior is the same in this case. When you flip the bits in a 
ushort and assign to another ushort, you don't care about the behavior 
of the other bits at all.

To reiterate, intpromote is going to require lots of casts, most of 
which wouldn't change any behavior that is currently happening.

The cases where the behavior is different is going from 8 or 16-bit 
integers to higher bit numbers. IMO, intpromote should focus on those, 
and leave the others alone.

I agree that the following is nonsense, and C gets it right.

```d
ushort x = 500;
int y = -x;
assert(y == 65036);
```

But fixing this problem doesn't mean we have to break all existing code 
for the purpose of busywork to insert casts to achieve the current behavior.

I just realized, the deprecation doesn't even say how to replicate 
current behavior. It says to cast to int before applying the operator, 
but that's not what current behavior does, it's what the *new* behavior 
does. Typically, deprecations identify how to adjust your code so it 
continues to do the same thing. This does the opposite.

-Steve


More information about the Digitalmars-d mailing list