Make -preview=intpromote not suck

Steven Schveighoffer schveiguy at gmail.com
Wed Oct 28 17:34:10 UTC 2020


On 10/28/20 11:13 AM, Paul Backus wrote:
> On Wednesday, 28 October 2020 at 14:52:28 UTC, Steven Schveighoffer wrote:
>>
>> I don't know if this is OK or correct. For example
>>
>> short c = a * b; => lots of truncation gonna happen.
> 
> This sort of thing is still allowed with -preview=intpromote, for types 
> that are int-sized or larger:
> 
>      int a = int.max, b = int.max;
>      int c = a*b; // compiles and truncates

Yeah, I never liked that aspect of integer promotion. In terms of 
practicality, int is probably a reasonable place to limit this for 
addition (it's very rare for adding 2 integers lead to an overflow, much 
less so than 2 shorts), but not multiplication.

> The most principled way to approach this is to either *always* require a 
> cast for possibly-truncating operations, or to *never* require one. As 
> it stands, `-preview=intpromote` is an unsatisfying half-measure that 
> both requires casts when they are not necessary, and fails to require 
> them when they may be necessary.

That ship may have sailed though. The goal is also different. I want to 
*avoid* breaking existing code, except in the case where it differs from 
the expected behavior. The exception in all of this is -int.min, which 
is still not going to work right.

I also wonder how much D coders would tolerate such rules. I've used 
more restrictive languages which don't allow implicit conversions in 
many cases between integer types, and it can be painful.

-Steve


More information about the Digitalmars-d mailing list