A strange div bug on Linux x86_64, (both dmd & ldc2): long -5000 / size_t 2 = 9223372036854773308

Simen Kjærås simen.kjaras at gmail.com
Fri Aug 14 05:47:53 UTC 2020


On Thursday, 13 August 2020 at 23:50:08 UTC, Walter Bright wrote:
> On 8/13/2020 4:33 PM, James Blachly wrote:
>> Especially in cases like this, compiler warnings would be 
>> helpful.
>
> Warnings are inherently problematic, as the poor user is always 
> faced with "is this a real problem or not?" I've been very 
> resistant to adding warnings in the past because it balkanizes 
> the language into dialects.
>
> The core language should decide what's an error and what isn't.

First, DMD has -w switch. I know you don't like it (I remember 
your disapproval from at least 15 years ago), but it's there. :p

It's the logical place to put a warning like this. Having it be a 
warning means copy-pasted C code will do what it did in C without 
a hitch, while also providing an easy way to check for something 
that may not be an error, but can be very surprising.


> To check for overflows, etc., use core.checkedint:

This does not in any way address the problem here, namely that 
the intuitive way to do things causes issues in 
possibly-very-rare situations. Telling users to use checkedint 
for (arr.sum / arr.length) is equivalent to telling them to 
simply cast arr.length to signed - it's bug-prone in that it's 
easy to forget, and it's bug-prone in that a new user doesn't 
know that it's necessary. We want our users to fall into the pit 
of success, and that is not what's happening here.

--
   Simen


More information about the Digitalmars-d mailing list