Thread overview
[Issue 1624] New: test of bool doesn't hold always
Oct 29, 2007
d-bugmail
Oct 29, 2007
d-bugmail
Oct 29, 2007
Matti Niemenmaa
Oct 29, 2007
d-bugmail
Oct 29, 2007
d-bugmail
October 29, 2007
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
}


-- 

October 29, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1624


bugzilla@digitalmars.com changed:

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




------- Comment #1 from bugzilla@digitalmars.com  2007-10-29 05:51 -------
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.


-- 

October 29, 2007
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
October 29, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1624


davidl@126.com changed:

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




------- Comment #2 from davidl@126.com  2007-10-29 06:58 -------
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


-- 

October 29, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1624


matti.niemenmaa+dbugzilla@iki.fi changed:

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




------- Comment #3 from matti.niemenmaa+dbugzilla@iki.fi  2007-10-29 07:07 -------
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.


-- 

October 29, 2007
<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                     |
>
>
>
>
> ------- Comment #2 from davidl@126.com  2007-10-29 06:58 -------
> 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.