www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7760] New: Getting delegate address from class object requires unneeded cast

http://d.puremagic.com/issues/show_bug.cgi?id=7760

           Summary: Getting delegate address from class object requires
                    unneeded cast
           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: k.hara.pg gmail.com



Following code doesn't work.
----
void main()
{
    class C1 { string var = "c1"; alias var this; }

    auto c = new C1();
    string delegate() dg = &o.toString;
    // Error: e2ir: cannot cast c.var of type string to type object.Object
}


Workaround:
----
    Object o = c;
    string delegate() dg = &o.toString;

====

This is CastExp::semantic issue.

In DelegateExp::semantic, &c.toString is translated to
&(cast(Object)c).toString.
Next in castExp::semantic, cast(Object)c is translated to cast(Object)(c.var),
because class C1 has an alias this declaration.
Of cause, this is bad cast, but semantic analysis doesn't check whether it is
invalid cast or not. Then this expression is rejected and raise an error in
glue layer.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 24 2012