Make -preview=intpromote not suck

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Mon Oct 26 13:58:37 UTC 2020


On Monday, 26 October 2020 at 13:06:09 UTC, Steven Schveighoffer 
wrote:
> For those who don't know, the current compiler does not integer 
> promote for negation. This means that typeof(-x) is typeof(x). 
> This also means that for things like short and byte:
>
> short x = short.min; // -32768
> int y = -x; // still -32768
> ulong z = -x; // 18446744073709518848
>
> In C (and in common sense), y and z are 32768.
>
> So we have a new system, which integer-promotes a value from 
> short or byte to int *before* the negation.
>
> However, this perfectly valid code will no longer work:
>
> short x2 = -x;

The obvious solution is to not allow modular arithmetics for 
signed integers (like most languages) and trap on overflow in 
debug builds.

I really dislike the whole idea of using VRP for typing. The code 
should behave the same way whether the compiler can figure out 
the value or not. Such special casing will lead to problems down 
the road.


More information about the Digitalmars-d mailing list