New integer promotion rules

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 18 14:30:43 UTC 2018


On 1/17/18 2:40 PM, rumbu wrote:
> This started in the last DMD version (2.078):
> 
> byte b = -10;
> ulong u = b < 0 ? -b : b;
> 
> //Deprecation: integral promotion not done for `-b`, use 
> '-transition=intpromote' switch or `-cast(int)(b)
> 
> Why do I need a to promote a byte to int to obtain an ulong? Even in the 
> extreme case where b is byte.min, -byte.min as unsigned is exactly what 
> i need: 128;
> 
> This leads to more cases:
> 
> ubyte u = cast(ubyte)-b;
> //Deprecation: integral promotion not done for `-b`, use 
> '-transition=intpromote' switch or `-cast(int)(b)`
> 
> Last time I checked, casting is somehow synonym with "I know what I'm 
> doing", why do I need another cast to prove my sanity: ubyte u = 
> cast(ubyte)-cast(int)b;

I was going to respond with a helpful guide on what to do here, but I 
either misunderstand the docs, or I don't agree with the transition 
requirements.

I thought that you could simply ignore this deprecation message if it 
doesn't affect you (i.e. you don't have the possibility of seeing the 
corner cases that are fixed), but looking at the changelog it says:

"Once deprecated this will become an error, and then the C-like behavior 
will become the default."

So this says, even if you aren't affected, you *still* have to add casts 
to avoid a future error? I thought it was going to be deprecated, and 
then after some number of versions it would just be switched to the new 
behavior.

Is there going to be a point where casts aren't needed? Otherwise, this 
is pretty ugly.

-Steve


More information about the Digitalmars-d-learn mailing list