[Issue 7760] New: Getting delegate address from class object requires unneeded cast

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 24 08:04:22 PDT 2012


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 at puremagic.com
        ReportedBy: k.hara.pg at gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg at gmail.com> 2012-03-24 08:04:44 PDT ---
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: -------


More information about the Digitalmars-d-bugs mailing list