www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1624] New: test of bool doesn't hold always

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

           Summary: test of bool doesn't hold always
           Product: D
           Version: 1.022
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: davidl 126.com


void main()
{
        union e{
                bool k;
                Object v;
        }
        e m;
        m.v = new Object;
        assert(m.k);
        assert((!m.k) == false);   // assertion failure
}


-- 
Oct 29 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1624


bugzilla digitalmars.com changed:

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





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.


-- 
Oct 29 2007
prev sibling next sibling parent Matti Niemenmaa <see_signature for.real.address> writes:
d-bugmail puremagic.com wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=1624
 
            Summary: test of bool doesn't hold always
            Product: D
            Version: 1.022
           Platform: PC
         OS/Version: Windows
             Status: NEW
           Severity: normal
           Priority: P2
          Component: DMD
         AssignedTo: bugzilla digitalmars.com
         ReportedBy: davidl 126.com
 
 
 void main()
 {
         union e{
                 bool k;
                 Object v;
         }
         e m;
         m.v = new Object;
         assert(m.k);
         assert((!m.k) == false);   // assertion failure
 }
This is analogous to doing something like: bool b; byte* p = cast(byte*)&b; *p = 127; assert (b || !b); // 127 != 0 so "b" holds assert (b == true || b == false); // oh noes, true == 1 and 1 != 127 Expecting to find true or false when you've explicitly forced the variable to hold neither value will obviously not work. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Oct 29 2007
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1624


davidl 126.com changed:

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





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


-- 
Oct 29 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
<d-bugmail puremagic.com> wrote in message 
news:fg4hsu$cip$1 digitalmars.com...
 http://d.puremagic.com/issues/show_bug.cgi?id=1624


 davidl 126.com changed:

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





 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.
Unions _are_ dangerous. This isn't news.
Oct 29 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1624


matti.niemenmaa+dbugzilla iki.fi changed:

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





-------
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.


-- 
Oct 29 2007