Thread overview
[Issue 5735] New: struct implicitly converted to boolean.
Mar 14, 2011
Iain Buclaw
Mar 16, 2011
Iain Buclaw
[Issue 5735] non-scalar types implicitly converted to boolean.
Apr 06, 2011
Iain Buclaw
Apr 18, 2011
Walter Bright
Apr 18, 2011
Walter Bright
Apr 18, 2011
Iain Buclaw
Apr 18, 2011
Iain Buclaw
Apr 18, 2011
Walter Bright
Apr 19, 2011
Walter Bright
March 14, 2011
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


--- Comment #0 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-03-14 11:04:05 PDT ---
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: -------
March 16, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5735


Iain Buclaw <ibuclaw@ubuntu.com> changed:

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


--- Comment #1 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-03-15 17:53:51 PDT ---
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: -------
April 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5735


Iain Buclaw <ibuclaw@ubuntu.com> changed:

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


--- Comment #2 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-04-06 03:22:23 PDT ---
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: -------
April 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5735


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> 2011-04-18 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: -------
April 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5735



--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2011-04-18 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: -------
April 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5735



--- Comment #5 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-04-18 14:47:20 PDT ---
It can happen with '(1 && cond)' too.

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



--- Comment #6 from Iain Buclaw <ibuclaw@ubuntu.com> 2011-04-18 14:58:27 PDT ---
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: -------
April 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5735



--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> 2011-04-18 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: -------
April 19, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5735


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #8 from Walter Bright <bugzilla@digitalmars.com> 2011-04-18 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: -------