D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3537 - Casting objects with alias this takes the subtype
Summary: Casting objects with alias this takes the subtype
Status: RESOLVED DUPLICATE of issue 6777
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
: 8001 (view as issue list)
Depends on:
Blocks:
 
Reported: 2009-11-21 08:27 UTC by Tomasz Sowiński
Modified: 2018-10-01 15:42 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomasz Sowiński 2009-11-21 08:27:36 UTC
interface I { }

class A : I {
    int x;
    alias x this;
    void f() {
        auto i = cast(I) this;  // ouch
    }
}

Should work but doesn't:
Error: e2ir: cannot cast this.x of type int to type hello.I

The cast can be outside -- still the same error:

void main() {
    auto a = new A;
    auto i = cast(I) a;  // ouch
}
Comment 1 Denis Shelomovskii 2013-11-09 05:41:14 UTC
This is designed behaviour. See documentation improvement Issue 11481.
Comment 2 Jakob Ovrum 2013-11-09 05:52:45 UTC
(In reply to comment #1)
> This is designed behaviour. See documentation improvement Issue 11481.

It makes dynamic casts impossible, it has to be changed.

Here's a better example:
----
class A
{
	int i;
	alias i this;
}

class B : A {}

void main()
{
	A a = new B;
	B b = cast(B)a; // test.d(12): Error: e2ir: cannot cast a.i of type int to type test.B
}
----
Comment 3 David Nadlinger 2013-11-09 08:51:37 UTC
@Denis: Please never close any issues where the error message includes "e2ir", as these are in fact ICEs (the frontend accepted something the glue code does not understand) and thus bugs regardless of what the intended behavior is.
Comment 4 Denis Shelomovskii 2013-11-09 10:46:34 UTC
(In reply to comment #3)
> @Denis: Please never close any issues where the error message includes "e2ir",
> as these are in fact ICEs (the frontend accepted something the glue code does
> not understand) and thus bugs regardless of what the intended behavior is.

First, didn't think it's an ICE as it's so common and even Kenji create issues with `e2ir` errors without ICE keyword.

Second, your words mean I can open e.g. "`byte` must be convertible to `Object`" issue and nobody can close it as the conversion results in ICE. )
So I can't accept it. But I will open "[e2ir]" ICE issues from now for such unreported minor ICEs. Issue 11485 is for this one.
Comment 5 David Nadlinger 2013-11-09 10:52:32 UTC
(In reply to comment #4)
> Second, your words mean I can open e.g. "`byte` must be convertible to
> `Object`" issue and nobody can close it as the conversion results in ICE.

Yes, such a report shouldn't just be closed as invalid. The title might be changed to something more fitting, however, and it could be marked as a duplicate of another entry describing the root cause.
Comment 6 Denis Shelomovskii 2013-11-09 10:52:42 UTC
*** Issue 8001 has been marked as a duplicate of this issue. ***
Comment 7 RazvanN 2018-10-01 15:42:26 UTC
Marking this one as a duplicate, as there already is a PR referencing 6777

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