Bug 1624 – test of bool doesn't hold always

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-10-29T03:47:00Z
Last change time
2014-02-24T15:32:34Z
Assigned to
bugzilla
Creator
davidl

Comments

Comment #0 by davidl — 2007-10-29T03:47:24Z
void main() { union e{ bool k; Object v; } e m; m.v = new Object; assert(m.k); assert((!m.k) == false); // assertion failure }
Comment #1 by bugzilla — 2007-10-29T05:51:39Z
The only valid values for a bool are true and false. In the example, a union is used to set bool to an invalid value, hence the subsequent operations on that bool do not produce the expected results. The code is incorrect, not the compiler.
Comment #2 by davidl — 2007-10-29T06:58:21Z
Where does the specification tell what the value of true/false is? So you actually mean a bool in a union is dangerous? I'm not aware of that. I think either doc or compiler need to be fixed
Comment #3 by matti.niemenmaa+dbugzilla — 2007-10-29T07:07:06Z
http://www.digitalmars.com/d/1.0/type.html "A bool value can be implicitly converted to any integral type, with false becoming 0 and true becoming 1. The numeric literals 0 and 1 can be implicitly converted to the bool values false and true, respectively." If you shove a value which is neither 0 nor 1 into a variable, and expect it to behave as though it were 0 or 1, you are doing something wrong.