digitalmars.D.bugs - [Bug 24] New: Arithmetic operators are allowed on boolean expressions
- d-bugmail puremagic.com (31/31) Mar 07 2006 http://d.puremagic.com/bugzilla/show_bug.cgi?id=24
- Thomas Kuehne (15/19) Mar 08 2006 -----BEGIN PGP SIGNED MESSAGE-----
- d-bugmail puremagic.com (6/6) Mar 08 2006 http://d.puremagic.com/bugzilla/show_bug.cgi?id=24
- d-bugmail puremagic.com (11/11) Mar 09 2006 http://d.puremagic.com/bugzilla/show_bug.cgi?id=24
- d-bugmail puremagic.com (14/16) Mar 09 2006 http://d.puremagic.com/bugzilla/show_bug.cgi?id=24
- Walter Bright (4/8) Mar 09 2006 You're right, it shouldn't compile. The message I get is:
- d-bugmail puremagic.com (20/20) Mar 09 2006 http://d.puremagic.com/bugzilla/show_bug.cgi?id=24
- Derek Parnell (13/36) Mar 09 2006 I see what's happening now. The compiler is converting the 'true'/'false...
- d-bugmail puremagic.com (30/30) Mar 09 2006 http://d.puremagic.com/bugzilla/show_bug.cgi?id=24
- Sean Kelly (7/37) Mar 09 2006 bool defaults to false/0, so x5 + true should equal 1, which should not
- Walter Bright (4/9) Mar 09 2006 That would be true if x5 is a const, but it isn't, so it doesn't get
- Sean Kelly (3/13) Mar 09 2006 Oh I see. Makes a lot more sense now :-)
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
-----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
http://d.puremagic.com/bugzilla/show_bug.cgi?id=24 ddparnell bigpond.com changed: What |Removed |Added ---------------------------------------------------------------------------- Version|unspecified |0.149 --
Mar 08 2006
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
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
<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
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
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
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
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 1bool 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 1Again 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
"Sean Kelly" <sean f4.ca> wrote in message news:dur6p8$2ila$1 digitaldaemon.com...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.bool x5; x5 += true; // fails because x5+true is int, and int cannot be implicitly converted to bool unless it is 0 or 1bool defaults to false/0, so x5 + true should equal 1, which should not fail.
Mar 09 2006
Walter Bright wrote:"Sean Kelly" <sean f4.ca> wrote in message news:dur6p8$2ila$1 digitaldaemon.com...Oh I see. Makes a lot more sense now :-) SeanThat 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.bool x5; x5 += true; // fails because x5+true is int, and int cannot be implicitly converted to bool unless it is 0 or 1bool defaults to false/0, so x5 + true should equal 1, which should not fail.
Mar 09 2006