www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5735] New: struct implicitly converted to boolean.

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

           Summary: struct implicitly converted to boolean.
           Product: D
           Version: D1 & D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: ibuclaw ubuntu.com



Example code:

struct A {}

void foo(bool cond){}

void main()
{
    A a;
    int i;

    assert(a);      // type A does not have a boolean value
    assert(i || a); // type A does not have a boolean value
    assert(0 || a); // OK

    if(a) {}        // type A does not have a boolean value
    if(i || a) {}   // type A does not have a boolean value
    if(0 || a) {}   // type A does not have a boolean value

    foo(a);         // cannot implicitly convert type A to bool
    foo(i || a);    // OK
    foo(0 || a);    // OK
}


The three examples that pass really should be errors.

Regards

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 14 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5735


Iain Buclaw <ibuclaw ubuntu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw ubuntu.com



Note, this is also the same issue for static arrays and unions.

int[1] arr;
assert(arr);       // type int[1u] does not have a boolean value
assert(0 || arr);  // OK

union B {}
B b;
assert(b);         // type B does not have a boolean value
assert(0 || b);    // OK


Regards

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5735


Iain Buclaw <ibuclaw ubuntu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch



Rough fix for 2.052 - will try out trunk later on today.

Regards

--- dmd.orig/cast.c     2011-02-18 01:15:38.000000000 +0000
+++ dmd/cast.c  2011-04-06 11:13:50.536604547 +0100
   -144,6 +144,10   
         type = Type::terror;
     }
     Expression *e = optimize(WANTvalue | WANTflags);
+    if (t->ty == Tbool)
+    {   // See if we can really convert the type to boolean.
+        e->checkToBoolean(NULL);
+    }
     if (e->type == t)
         return MATCHexact;
     if (e != this)
--- dmd.orig/optimize.c 2011-02-18 01:15:38.000000000 +0000
+++ dmd/optimize.c      2011-04-06 10:55:18.075088167 +0100
   -990,6 +990,8   

                 e = new IntegerExp(loc, n1 || n2, type);
             }
+            else if (! e2->type->checkBoolean())
+                ;   // Don't convert e2 to bool if it's type disallows it.
             else if (e1->isBool(FALSE))
                 e = new BoolExp(loc, e2, type);
         }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5735


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com



12:25:14 PDT ---
The patch seg faults building phobos because the argument to checkToBoolean is
NULL.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 18 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5735




14:28:14 PDT ---
// Suitable for inclusion in test suite

struct A {}

void foo(bool cond){}

void main()
{
    A a;
    int i;

    static assert(!__traits(compiles, assert(a)));      // type A does not have
a boolean value
    static assert(!__traits(compiles, assert(i || a))); // type A does not have
a boolean value
    static assert(!__traits(compiles, assert(0 || a))); // OK

//    if(a) {}        // type A does not have a boolean value
//    if(i || a) {}   // type A does not have a boolean value
//    if(0 || a) {}   // type A does not have a boolean value

    static assert(!__traits(compiles, foo(a)));         // cannot implicitly
convert type A to bool
    static assert(!__traits(compiles, foo(i || a)));    // OK
    static assert(!__traits(compiles, foo(0 || a)));    // OK
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 18 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5735




It can happen with '(1 && cond)' too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 18 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5735




Created an attachment (id=944)
issue5735

OK, Scrap the above, this is a check is in a better place - though it can cause
duplicate errors to emit for the same line. I'm sure you'll know what's best to
do though about that. :)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 18 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5735




15:58:09 PDT ---
Partial fix:

https://github.com/D-Programming-Language/dmd/commit/fdac2763da067cc8901f35a4095778787be25fca

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 18 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5735


Walter Bright <bugzilla digitalmars.com> changed:

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



22:34:19 PDT ---
https://github.com/D-Programming-Language/dmd/commit/f62ea9b6fe314e7ec2a2755066334b9184150b81

https://github.com/D-Programming-Language/dmd/commit/e1bdbb45f75a078109dd807f63d149fbe7231a42

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 18 2011