www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Bug 24] New: Arithmetic operators are allowed on boolean expressions

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24

           Summary: Arithmetic operators are allowed on boolean expressions
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: walter digitalmars.com
        ReportedBy: ddparnell bigpond.com


The operators 

 + - / * 

should cause a compilation error if one of the expressions is a boolean
datatype. 

The code below should no compile...

    auto q = true + true + true;
    bool x;
    int y;
    x = true;
    y = x / x;
    y = x * x;
    y = x - x;
    y = x + x;
    y += x;
    y -= x;
    y /= x;
    y *= x;


-- 
Mar 07 2006
next sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

d-bugmail puremagic.com schrieb am 2006-03-08:
 The operators 

  + - / * 

 should cause a compilation error if one of the expressions is a boolean
 datatype. 
Added to DStress as http://dstress.kuehne.cn/nocompile/o/opMul_10_A.d http://dstress.kuehne.cn/nocompile/o/opMulAssign_20_A.d http://dstress.kuehne.cn/nocompile/o/opDiv_15_A.d http://dstress.kuehne.cn/nocompile/o/opDivAssign_20_A.d (the others are tested by updated test cases) Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFEDvPN3w+/yD4P9tIRAsb2AJ97+A6r7xwaKS8N63VmtKrGk8JfkACeNouD hL5xZMXAb7g/OuGkdIzlPOc= =LhK2 -----END PGP SIGNATURE-----
Mar 08 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24


ddparnell bigpond.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|unspecified                 |0.149




-- 
Mar 08 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24


walter digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID





bool types are implicitly converted to ints when used in arithmetic operations.
This is by design, not a bug. Ints, however, are not implicitly convertible to
bool (unless they are the integer literals 0 or 1).


-- 
Mar 09 2006
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24






But you originally said it was a bug. I refer you to ...

http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.announce/3039
 -------------------
"Derek Parnell" <derek psych.ward> wrote in message 
news:1o1ukrzuobjw5$.19cyl0ofx7fqs$.dlg 40tude.net...
 It seems that arithmetic operators also work on booleans so I guess the
 operator list above is either not correct or this is a bug.
It's a bug. Sigh. It always takes me two tries to get this right :-( ------------------- So I still maintain that this is a mistake. bool x = true + true; Should not compile. The promotion to ints must occur after the initial semantics of the expression are validated. --
Mar 09 2006
parent "Walter Bright" <newshound digitalmars.com> writes:
<d-bugmail puremagic.com> wrote in message 
news:duqi91$1m4d$1 digitaldaemon.com...
 So I still maintain that this is a mistake.

  bool x = true + true;

 Should not compile. The promotion to ints must occur after the initial
 semantics of the expression are validated.
You're right, it shouldn't compile. The message I get is: test.d(1): cannot implicitly convert expression (1 + 1) of type int to bool
Mar 09 2006
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24


ddparnell bigpond.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |





With version v0.149 (Windows) I get this ...

void main()
{
    bool x1 = true + true; // fails
    bool x2 = true - true; // accepted
    bool x3 = true * true; // accepted
    bool x4 = true / true; // accepted
    bool x5; x5 += true;  // fails
    bool x6; x6 -= true; // fails
    bool x7; x7 *= true; // fails
    bool x8; x8 /= true; // fails
}


-- 
Mar 09 2006
parent Derek Parnell <derek psych.ward> writes:
On Fri, 10 Mar 2006 02:03:22 +0000 (UTC), d-bugmail puremagic.com wrote:

 http://d.puremagic.com/bugzilla/show_bug.cgi?id=24
 
 ddparnell bigpond.com changed:
 
            What    |Removed                     |Added
 ----------------------------------------------------------------------------
              Status|RESOLVED                    |REOPENED
          Resolution|INVALID                     |
 

 With version v0.149 (Windows) I get this ...
 
 void main()
 {
     bool x1 = true + true; // fails
     bool x2 = true - true; // accepted
     bool x3 = true * true; // accepted
     bool x4 = true / true; // accepted
     bool x5; x5 += true;  // fails
     bool x6; x6 -= true; // fails
     bool x7; x7 *= true; // fails
     bool x8; x8 /= true; // fails
 }
I see what's happening now. The compiler is converting the 'true'/'false' to 1 and 0 then doing the calculation and if the result is 1 or 0, it accepts the code otherwise it rejects it. However, this is still not the appropriate way to do it because even though "false + false" would be accepted by dmd, it shouldn't be because 'false' is not a number even though it can be converted to zero by convention. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 10/03/2006 1:09:49 PM
Mar 09 2006
prev sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24


walter digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |INVALID





First, note that the bool operands undergo integral promotion rules, so that
(true op true) is evaluated as (cast(int)true op cast(int)true), with a result
type of int. The integral promotion for bools does not apply if op is & | ^

void main()
{
    bool x1 = true + true; // fails because true+true => 2, and 2 cannot be
implicitly converted to bool

    bool x2 = true - true; // accepted because true-true=>0, and 0 can be
implicitly converted to bool

    bool x3 = true * true; // accepted because true*true => 1, and 1 can be
implicitly converted to bool

    bool x4 = true / true; // accepted because true/true => 1, and 1 can be
implicitly converted to bool

    bool x5; x5 += true;  // fails because x5+true is int, and int cannot be
implicitly converted to bool unless it is 0 or 1

    bool x6; x6 -= true; // fails because x6-true is int, and int cannot be
implicitly converted to bool unless it is 0 or 1

    bool x7; x7 *= true; // fails because x7*true is int, and int cannot be
implicitly converted to bool unless it is 0 or 1

    bool x8; x8 /= true; // fails because x8/true is int, and int cannot be
implicitly converted to bool unless it is 0 or 1
}


-- 
Mar 09 2006
parent reply Sean Kelly <sean f4.ca> writes:
A few nits.

d-bugmail puremagic.com wrote:
 

 First, note that the bool operands undergo integral promotion rules, so that
 (true op true) is evaluated as (cast(int)true op cast(int)true), with a result
 type of int. The integral promotion for bools does not apply if op is & | ^
 
 void main()
 {
     bool x1 = true + true; // fails because true+true => 2, and 2 cannot be
 implicitly converted to bool
 
     bool x2 = true - true; // accepted because true-true=>0, and 0 can be
 implicitly converted to bool
 
     bool x3 = true * true; // accepted because true*true => 1, and 1 can be
 implicitly converted to bool
 
     bool x4 = true / true; // accepted because true/true => 1, and 1 can be
 implicitly converted to bool
 
     bool x5; x5 += true;  // fails because x5+true is int, and int cannot be
 implicitly converted to bool unless it is 0 or 1
bool defaults to false/0, so x5 + true should equal 1, which should not fail.
     bool x6; x6 -= true; // fails because x6-true is int, and int cannot be
 implicitly converted to bool unless it is 0 or 1
 
     bool x7; x7 *= true; // fails because x7*true is int, and int cannot be
 implicitly converted to bool unless it is 0 or 1
Again no fail. 0 * 1 == 0.
     bool x8; x8 /= true; // fails because x8/true is int, and int cannot be
 implicitly converted to bool unless it is 0 or 1
 }
No fail here either, as 0 / 1 == 0. Sean
Mar 09 2006
parent reply "Walter Bright" <newshound digitalmars.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message 
news:dur6p8$2ila$1 digitaldaemon.com...
     bool x5; x5 += true;  // fails because x5+true is int, and int cannot 
 be
 implicitly converted to bool unless it is 0 or 1
bool defaults to false/0, so x5 + true should equal 1, which should not fail.
That would be true if x5 is a const, but it isn't, so it doesn't get constant folded. The same applies to the other examples.
Mar 09 2006
parent Sean Kelly <sean f4.ca> writes:
Walter Bright wrote:
 "Sean Kelly" <sean f4.ca> wrote in message 
 news:dur6p8$2ila$1 digitaldaemon.com...
     bool x5; x5 += true;  // fails because x5+true is int, and int cannot 
 be
 implicitly converted to bool unless it is 0 or 1
bool defaults to false/0, so x5 + true should equal 1, which should not fail.
That would be true if x5 is a const, but it isn't, so it doesn't get constant folded. The same applies to the other examples.
Oh I see. Makes a lot more sense now :-) Sean
Mar 09 2006