www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5020] New: Forward implicit bool conversions to alias this

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

           Summary: Forward implicit bool conversions to alias this
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: rsinfu gmail.com



---
Alias this isn't implemented for implicit conversions to bool in conditional
expressions.  The following presumably valid code doesn't compile:
--------------------
void main()
{
    S s;
    if (!s) {}  // (4)
}
struct S
{
    bool cond;
    alias cond this;
}
--------------------
% dmd -o- -c test.d
test.d(4): Error: expression s of type S does not have a boolean value

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 08 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5020


Shin Fujishiro <rsinfu gmail.com> changed:

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



---
It's simply Expression::checkToBoolean() not looking for alias this.  Here's a
proposed patch against dmd r707:
====================
--- src/expression.c
+++ src/expression.c
   -1269,6 +1269,15    Expression *Expression::checkToBoolean(Scope *sc)
             e = e->semantic(sc);
             return e;
         }
+
+        // Forward to aliasthis.
+        if (ad->aliasthis)
+        {
+            Expression *e = new DotIdExp(loc, this, ad->aliasthis->ident);
+            e = e->semantic(sc);
+            e = e->checkToBoolean(sc);
+            return e;
+        }
     }

     if (!type->checkBoolean())
====================

Note: Since CastExp takes care of aliasthis, adding a test for ad->aliasthis in
a preceding if-block also makes the repro code work.  But the if-block doesn't
check for implicit convertible-ness (i.e. checkToBoolean), and it will
eventually allow the following wrong code to be accepted:

void main()
{
    S s;
    if (s) {}   // wrong
}
struct S
{
    struct R {} // not implicitly convertible to bool
    R r;
    alias r this;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 08 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5020


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bearophile_hugs eml.cc
         Resolution|                            |FIXED



Fixed in DMD 2.051

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 21 2010