This seems like what could be a common cause of bugs

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 11 08:45:47 PDT 2011


On Monday 11 July 2011 08:17:50 Steven Schveighoffer wrote:
> On Sat, 09 Jul 2011 13:28:25 -0400, Nick Sabalausky <a at a.a> wrote:
> > "Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message
> > news:op.vybq4xkkeav7ka at localhost.localdomain...
> > 
> >> On Fri, 08 Jul 2011 18:45:58 -0400, Andrej Mitrovic
> >> 
> >> <andrej.mitrovich at gmail.com> wrote:
> >>> This is just an observation, not a question or anything.
> >>> 
> >>> void main()
> >>> {
> >>> 
> >>>     enum width = 100;
> >>>     double step = 1 / width;
> >>>     
> >>>     writeln(step);  // 0
> >>> 
> >>> }
> >>> 
> >>> I've just had this bug in my code. I forgot to make either width or
> >>> 1
> >>> a floating-point type. IOW, I didn't do this:
> >>> 
> >>> void main()
> >>> {
> >>> 
> >>>     enum width = 100.0;
> >>>     double step = 1 / width;   // or .1
> >>>     
> >>>     writeln(step);  // now 0.01
> >>> 
> >>> }
> >>> 
> >>> This seems like a very easy mistake to make. But I know the compiler
> >>> is probably powerless to do anything about it. It has an expression
> >>> resulting in an int at the RHS, and it can easily cast this to a
> >>> double since there's no obvious loss of precision when casting
> >>> int->double.
> >> 
> >> BTW, this is definitely not something the compiler can determine.  You
> >> may
> >> actually *want* to have integer division then convert to a double.
> >> 
> >> So a lint tool would be perfect for this -- it cannot be an error, and
> >> warnings don't exist in D.
> > 
> > Yea they do: dmd -wi
> 
> Hm... I didn't know about that switch, I thought warnings were enabled
> with -w and that made them errors.
> 
> I suppose you could stick this in there.

By default, warnings are completely disabled. -w enables them and makes the 
errors. So, it's more like telling it to add more errors than to enable 
warnings. -wi enables them, but it leaves them as warnings. They're then 
"informational" (basically, -wi makes warnings work like they do with pretty 
much every other compiler on the planet). IIRC, people kept bugging Walter 
about how warnings work with dmd, so he added -wi.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list