Killing integer division problems?

Timon Gehr timon.gehr at gmx.ch
Tue Aug 4 18:37:33 UTC 2020


On 04.08.20 15:13, burt wrote:
> On Tuesday, 4 August 2020 at 12:54:42 UTC, WebFreak001 wrote:
>> On Tuesday, 4 August 2020 at 12:34:37 UTC, burt wrote:
>>> [...]
>>
>> I agree having this as warning would be useful, however it should not 
>> apply to:
>>
>> int x = y / z;
>>
>> but only to
>>
>> float x = y / z; (and double)
> 
> Yes exactly, I might not have been clear about this but that is what I 
> intended.
> 
>> In my mind I would like if this was implemented something along the 
>> lines "if this expression has an integer division in it and is 
>> implicitly converted to float/double, emit a warning", if it isn't 
>> implicitly converted to float/double it should not emit a warning, as 
>> casts are quite ugly and may introduce bugs later on if types change.
>>
>> Also alternative to casting, which would be safer:
>>
>> float x = int(y / z);
> 
> Sure.
> 
>> This is already valid syntax and is basically just like an implicit 
>> assignment, just explicitly written out. This way you can't 
>> accidentally change y or z to float/double without triggering a 
>> compiler error.
>>
>> [...]
>>
>> Though I haven't really contributed to dmd, so I don't know how 
>> feasible this actually is :)
> 
> Me neither... that's why I was asking around if it's feasible.

Sure. For someone not very familiar with the DMD code base, the way to 
make it happen is to find some error message mentioning things that are 
related to the patch you want to make (like implicit casting) and grep 
for them in the DMD source code. Then read the code around them until 
you understand how to patch DMD so it emits your warning.


More information about the Digitalmars-d mailing list