D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7760 - Getting delegate address from class object requires unneeded cast
Summary: Getting delegate address from class object requires unneeded cast
Status: RESOLVED DUPLICATE of issue 10646
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on: 7472
Blocks:
  Show dependency treegraph
 
Reported: 2012-03-24 08:04 UTC by Kenji Hara
Modified: 2013-11-28 19:53 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kenji Hara 2012-03-24 08:04:44 UTC
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.
Comment 1 Denis Shelomovskii 2013-11-09 10:23:09 UTC
Original testcase works now. Resolved?
Comment 2 Kenji Hara 2013-11-28 19:53:00 UTC
(In reply to comment #1)
> Original testcase works now. Resolved?

Root cause was fixed in issue 10646.

*** This issue has been marked as a duplicate of issue 10646 ***