My Kingdom For ...
Don Clugston
dac at nospam.com.au
Sat Feb 23 22:35:40 PST 2008
Derek Parnell wrote:
> On Fri, 22 Feb 2008 22:27:34 +0100, Don Clugston wrote:
>
>> Janice Caron wrote:
>>> On 22/02/2008, Derek Parnell <derek at nomail.afraid.org> wrote:
>>>> Compile with the "-w" switch. It checks for 'missing' return paths.
>>> Ah great! Thanks. That will help a lot.
>>>
>>> I thought D didn't have warnings, but obviously I was wrong.
>>>
>>> It's a shame you can't /selectively/ enable warnings, because I don't
>>> actually want to be warned about "length" shadowing. Still - it's
>>> better than it's being an error. In general, I'd prefer that /all/
>>> shadowing should be allowed. But that's another debate.
>>>
>>> Anyway, thanks for the info. Will definitely be using -w from now on.
>> Really, 'missing return value' should be an error, not a warning. Unfortunately
>> -w is horribly broken.
>
> The "-w" does not mean "warning" in D. Walter does not believe in warnings.
Nor do I.
> In D, it means an optional error check, that is to say, one can optionally
> choose to check for certain types of errors that are not checked for
> without the "-w" switch. I think these are optional because there is some
> debate over whether these things are actually errors or not.
The problem is that some of them have a >90% chance of being a bug, whereas
others (such as the one I mentioned) have a ZERO % chance of being a bug.
>
> The message text says 'warning' but the compiler treats them as errors
> anyway.
>
>> It generates some nasty spurious errors. For example, it
>> won't let you perform a bitwise operation on anything other than an int.
>>
>> short a;
>> short b = a | a; // won't compile with -w !!!!
>
> Actually that turns out not to be the case. The 'warning/error' message in
> this situatation informs the coder that there is a potential loss of
> information when assigning a larger datatype to a smaller one.
>
> "warning - test.d(4): Error: implicit conversion of expression
> (cast(int)a | cast (int)a) of type int to short can cause
> loss of data"
>
> The real problem with this is that D decides to convert your 'short' values
> to 'int' before doing the bitwise operation, leaving you with an 'int'
> value to squeeze back into a 'short'. Now you and I know that this is a
> redundant operation (converting to 'int') and that the high-end bits will
> be zeroes anyway so that there is no loss of information. But DMD is not
> that smart yet.
>
> The 'warning' message is a valid one, in the general sense. For example
> this code here gives a similar message ...
Not really. A usually incorrect error message is much, much worse than useless.
VC6 had one which was basically "warning: you're using templates". But at least
you could selectively turn that off. Unfortunately with this one the only option
is to not use -w.
>
> void main(string[] args)
> {
> short argc = args.length;
> }
>
> Even though we all know that we are not going to have 2^32 command line
> arguments to the program.
>
> The proper coding style to get the results you want is to tell the
> compiler, and other readers of your code, that you "know what you are doing
> so don't get upset"...
> short a;
> short b = cast(short)(a | a);
>
> short argc = cast(short)args.length;
>
> These do not give 'warning' messages now.
No, but now you've introduced a latent bug! If someone changes 'a' to be (say)
int a, it will now be accepted even though in that case it DOES involve loss of
data! Inserting a cast is not an acceptable workaround.
Would be better in the interim to not issue 'narrowing conversion' warnings at
all, when they are so hopelessly incorrect.
More information about the Digitalmars-d
mailing list