Thread overview
warning bug - bits and ints
Mar 15, 2005
Nick
Mar 15, 2005
Derek Parnell
Mar 15, 2005
Andrew Fedoniouk
Mar 15, 2005
Derek Parnell
Mar 16, 2005
Nick Sabalausky
Mar 16, 2005
Derek Parnell
Mar 25, 2005
Ben Hinkle
March 14, 2005
There is a bug with the brand new warning system in D.
Bit is treated like an integer type, which it is *not*.

Simple test case:

> bit t() { return 1; }
> bit f() { return 0; }
> 
> void main()
> {
>   bit b = t() && f();
> }

"implicit conversion of expression (t() && f()) of
type int to bit can cause loss of data"

This code works fine:

> void main()
> {
>   bit b = true && false;
> }

(most likely because it is a compile-time expression?)


Converting any integer to a bit is *defined* as returning
0 for zero integers and 1 for non-zero integer values...

There is no "loss of data", as with going from int->short.

--anders
March 15, 2005
In article <d13hhd$126q$1@digitaldaemon.com>, =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says...
>
>There is a bug with the brand new warning system in D.
>Bit is treated like an integer type, which it is *not*.
>
>Simple test case:
>
>> bit t() { return 1; }
>> bit f() { return 0; }
>> 
>> void main()
>> {
>>   bit b = t() && f();
>> }

I think the problem here is that binary opperators like &&, || and ! return int, not bit/bool. BTW, it this bug is fixed, I will start using warnings on a permanent basis, as they have already unveiled two serious bugs in my code (Thanks Walt!)

Nick


March 15, 2005
On Tue, 15 Mar 2005 15:46:20 +0000 (UTC), Nick wrote:

[snip]
> ... I will start using warnings on a
> permanent basis, as they have already unveiled two serious bugs in my code
> (Thanks Walt!)

Agreed. I've found a few bugs because of the warnings too. Thank you Walter for this helpful device.

-- 
Derek Parnell
Melbourne, Australia
16/03/2005 7:16:51 AM
March 15, 2005
One more on the subject:

int low, int high;

bool isEmpty()
{
   return low > hight;
}

Warning: "implicit conversion of expression (low > high) of type int to bit can cause loss of data".

Andrew.


"Anders F Björklund" <afb@algonet.se> wrote in message news:d13hhd$126q$1@digitaldaemon.com...
> There is a bug with the brand new warning system in D.
> Bit is treated like an integer type, which it is *not*.
>
> Simple test case:
>
>> bit t() { return 1; }
>> bit f() { return 0; }
>>
>> void main()
>> {
>>   bit b = t() && f();
>> }
>
> "implicit conversion of expression (t() && f()) of
> type int to bit can cause loss of data"
>
> This code works fine:
>
>> void main()
>> {
>>   bit b = true && false;
>> }
>
> (most likely because it is a compile-time expression?)
>
>
> Converting any integer to a bit is *defined* as returning
> 0 for zero integers and 1 for non-zero integer values...
>
> There is no "loss of data", as with going from int->short.
>
> --anders


March 15, 2005
On Tue, 15 Mar 2005 14:12:30 -0800, Andrew Fedoniouk wrote:

> One more on the subject:
> 
> int low, int high;
> 
> bool isEmpty()
> {
>    return low > hight;
> }
> 
> Warning: "implicit conversion of expression (low > high) of type int to bit can cause loss of data".
> 

This is not a bug.  The expression (low > high) results in an integer value
and it is possible to lose data when converting an integer to a bit (bool).

Now maybe such expressions should result in a true boolean value, but D does not support true boolean values, only pseudo-boolean values called bits ( a one-bit integer ).

Consequently, you cannot take the syntax shortcuts one would code in C/C++, and in D such expressions as above need to be spelled out in full ...

  return low > high : true ? false;


-- 
Derek
Melbourne, Australia
16/03/2005 9:27:15 AM
March 16, 2005
I suppose this has probably been discussed already, but does it really make sense for the result of a comparison to be an integer?

"Derek Parnell" <derek@psych.ward> wrote in message news:1abo3r0w4p3ct.1eds1mg8mobpz$.dlg@40tude.net...
> On Tue, 15 Mar 2005 14:12:30 -0800, Andrew Fedoniouk wrote:
>
>> One more on the subject:
>>
>> int low, int high;
>>
>> bool isEmpty()
>> {
>>    return low > hight;
>> }
>>
>> Warning: "implicit conversion of expression (low > high) of type int to
>> bit
>> can cause loss of data".
>>
>
> This is not a bug.  The expression (low > high) results in an integer
> value
> and it is possible to lose data when converting an integer to a bit
> (bool).
>
> Now maybe such expressions should result in a true boolean value, but D does not support true boolean values, only pseudo-boolean values called bits ( a one-bit integer ).
>
> Consequently, you cannot take the syntax shortcuts one would code in
> C/C++,
> and in D such expressions as above need to be spelled out in full ...
>
>  return low > high : true ? false;
>
>
> -- 
> Derek
> Melbourne, Australia
> 16/03/2005 9:27:15 AM


March 16, 2005
On Tue, 15 Mar 2005 21:49:49 -0500, Nick Sabalausky wrote:

> "Derek Parnell" <derek@psych.ward> wrote in message news:1abo3r0w4p3ct.1eds1mg8mobpz$.dlg@40tude.net...
>> On Tue, 15 Mar 2005 14:12:30 -0800, Andrew Fedoniouk wrote:
>>
>>> One more on the subject:
>>>
>>> int low, int high;
>>>
>>> bool isEmpty()
>>> {
>>>    return low > hight;
>>> }
>>>
>>> Warning: "implicit conversion of expression (low > high) of type int to
>>> bit
>>> can cause loss of data".
>>>
>>
>> This is not a bug.  The expression (low > high) results in an integer
>> value
>> and it is possible to lose data when converting an integer to a bit
>> (bool).
>>
>> Now maybe such expressions should result in a true boolean value, but D does not support true boolean values, only pseudo-boolean values called bits ( a one-bit integer ).
>>
>> Consequently, you cannot take the syntax shortcuts one would code in
>> C/C++,
>> and in D such expressions as above need to be spelled out in full ...
>>
>>  return low > high : true ? false;
> I suppose this has probably been discussed already, but does it really make sense for the result of a comparison to be an integer?
> 
Not to me. I think its plain daft. But I believe DMD does it this way because it tries to be compatible with (legacy) C/C++ code.

-- 
Derek
Melbourne, Australia
16/03/2005 1:58:09 PM
March 16, 2005
More importantly, it does it this way for reasons of processor optimization, to my understanding...

-[Unknown]


> On Tue, 15 Mar 2005 21:49:49 -0500, Nick Sabalausky wrote:
> 
> 
>>"Derek Parnell" <derek@psych.ward> wrote in message news:1abo3r0w4p3ct.1eds1mg8mobpz$.dlg@40tude.net...
>>
>>>On Tue, 15 Mar 2005 14:12:30 -0800, Andrew Fedoniouk wrote:
>>>
>>>
>>>>One more on the subject:
>>>>
>>>>int low, int high;
>>>>
>>>>bool isEmpty()
>>>>{
>>>>   return low > hight;
>>>>}
>>>>
>>>>Warning: "implicit conversion of expression (low > high) of type int to bit
>>>>can cause loss of data".
>>>>
>>>
>>>This is not a bug.  The expression (low > high) results in an integer value
>>>and it is possible to lose data when converting an integer to a bit (bool).
>>>
>>>Now maybe such expressions should result in a true boolean value, but D
>>>does not support true boolean values, only pseudo-boolean values called
>>>bits ( a one-bit integer ).
>>>
>>>Consequently, you cannot take the syntax shortcuts one would code in C/C++,
>>>and in D such expressions as above need to be spelled out in full ...
>>>
>>> return low > high : true ? false;
>>
>>I suppose this has probably been discussed already, but does it really make sense for the result of a comparison to be an integer?
>>
> 
> Not to me. I think its plain daft. But I believe DMD does it this way
> because it tries to be compatible with (legacy) C/C++ code.
> 
March 16, 2005
Derek Parnell wrote:

> This is not a bug.  The expression (low > high) results in an integer value
> and it is possible to lose data when converting an integer to a bit (bool).

As long as D uses pseudo-boolean casts to convert integers and pointers into bits, there is no possibility for "loss of data" when doing it...

The result is the same as : (integer != 0) or (pointer != null)

> Now maybe such expressions should result in a true boolean value, but D
> does not support true boolean values, only pseudo-boolean values called
> bits ( a one-bit integer ).

Well, bits are *not* 1-bit integers, but they sure are pseudo-booleans.

If you convert for instance the integer 256 into a byte, you get 0.
But if you convert the integer 2 into a bit, you get 1 (i.e. 'true')

--anders

PS.
It does not help that the spec says it *should* return a "bool":
> The result type of an OrOr expression is bool,
> The result type of an AndAnd expression is bool, Equality expressions ... The type of the result is bool.
> The is compares for ... The type of the result is bool.
> The result type of a relational expression is bool.
http://www.digitalmars.com/d/expression.html
March 25, 2005
Walter, can we get warnings that don't cause the compiler to error? I can't use -w for anything due to this int/bit conversion warning. I could change my code to avoid bit and pass ints around so that I can use the comparison operators with causing the compiler to freak out, but that seems like the wrong way to go. What is the recommended way to deal with -w and int/bit?

"Anders F Björklund" <afb@algonet.se> wrote in message news:d13hhd$126q$1@digitaldaemon.com...
> There is a bug with the brand new warning system in D.
> Bit is treated like an integer type, which it is *not*.
>
> Simple test case:
>
>> bit t() { return 1; }
>> bit f() { return 0; }
>>
>> void main()
>> {
>>   bit b = t() && f();
>> }
>
> "implicit conversion of expression (t() && f()) of
> type int to bit can cause loss of data"
>
> This code works fine:
>
>> void main()
>> {
>>   bit b = true && false;
>> }
>
> (most likely because it is a compile-time expression?)
>
>
> Converting any integer to a bit is *defined* as returning
> 0 for zero integers and 1 for non-zero integer values...
>
> There is no "loss of data", as with going from int->short.
>
> --anders