Bug 24 – Arithmetic operators are allowed on boolean expressions

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2006-03-07T22:17:00Z
Last change time
2014-02-15T02:08:31Z
Keywords
accepts-invalid
Assigned to
bugzilla
Creator
ddparnell

Comments

Comment #0 by ddparnell — 2006-03-07T22:17:48Z
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;
Comment #1 by thomas-dloop — 2006-03-08T08:15:24Z
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [email protected] 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-----
Comment #2 by bugzilla — 2006-03-09T18:29:36Z
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).
Comment #3 by ddparnell — 2006-03-09T18:46:24Z
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" <[email protected]> wrote in message news:[email protected]... > 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.
Comment #4 by ddparnell — 2006-03-09T20:03:21Z
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 }
Comment #5 by bugzilla — 2006-03-10T00:21:02Z
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 }
Comment #6 by sean — 2006-03-10T00:40:26Z
A few nits. [email protected] wrote: > > ------- Comment #5 from [email protected] 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
Comment #7 by sean — 2006-03-10T01:20:23Z
Walter Bright wrote: > "Sean Kelly" <[email protected]> wrote in message > news:[email protected]... >>> 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