Bug in ^^

Timon Gehr timon.gehr at gmx.ch
Tue Sep 17 19:22:44 UTC 2019


On 17.09.19 18:49, Vladimir Panteleev wrote:
> On Tuesday, 17 September 2019 at 01:53:12 UTC, Brett wrote:
>> 10^^16 = 1874919424    ???
>>
>> 10L^^16 is valid, but
>>
>> enum x = 10^^16 gives wrong value.
>>
>> I didn't catch this ;/
> 
> The same can be observed with multiplication:
> 
> // This compiles, but the result is "non-sensical" due to oveflow.
> enum n = 1_000_000 * 1_000_000;
> 
> The same can happen with C:
> 
> static const int n = 1000000 * 1000000;
> 
> However, C compilers warn about this:
> 
> gcc:
> 
> test.c:1:30: warning: integer overflow in expression of type ‘int’ 
> results in ‘-727379968’ [-Woverflow]
>      1 | static const int n = 1000000 * 1000000;
>        |                              ^
> 
> clang:
> 
> test.c:1:30: warning: overflow in expression; result is -727379968 with 
> type 'int' [-Winteger-overflow]
> static const int n = 1000000 * 1000000;
>                               ^
> 1 warning generated.
> 
> I think D should warn about any overflows which happen at compile-time too.
> 

It's not the same. C compilers warn about overflows that are UB. They 
don't complain about overflows that have defined behavior:

static const int n = 1000000u * 1000000u; // no warning

In D, all overflows in operations on basic integer types have defined 
behavior, not just those operating on unsigned integers.


More information about the Digitalmars-d mailing list