Jump to page: 1 2
Thread overview
[Bug 24] New: Arithmetic operators are allowed on boolean expressions
Mar 08, 2006
d-bugmail
Mar 08, 2006
Thomas Kuehne
Mar 08, 2006
d-bugmail
Mar 10, 2006
d-bugmail
Mar 10, 2006
d-bugmail
Mar 10, 2006
Walter Bright
Mar 10, 2006
d-bugmail
Mar 10, 2006
Derek Parnell
Mar 10, 2006
d-bugmail
Mar 10, 2006
Sean Kelly
Mar 10, 2006
Walter Bright
Mar 10, 2006
Sean Kelly
March 08, 2006
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;


-- 

March 08, 2006
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


March 08, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24


ddparnell@bigpond.com changed:

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




-- 

March 10, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24


walter@digitalmars.com changed:

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




------- Comment #2 from walter@digitalmars.com  2006-03-09 18:29 -------
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).


-- 

March 10, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24





------- Comment #3 from ddparnell@bigpond.com  2006-03-09 18:46 -------
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.


-- 

March 10, 2006
<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


March 10, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24


ddparnell@bigpond.com changed:

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




------- Comment #4 from ddparnell@bigpond.com  2006-03-09 20:03 -------
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
}


-- 

March 10, 2006
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                     |
> 
> ------- Comment #4 from ddparnell@bigpond.com  2006-03-09 20:03 -------
> 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
March 10, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24


walter@digitalmars.com changed:

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




------- Comment #5 from walter@digitalmars.com  2006-03-10 00:21 -------
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
}


-- 

March 10, 2006
A few nits.

d-bugmail@puremagic.com wrote:
> 
> ------- Comment #5 from walter@digitalmars.com  2006-03-10 00:21 -------
> 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
« First   ‹ Prev
1 2