Issue 13833 - .classinfo.name (and typeid(obj)) does not print proper dynamic type when using an interface
Summary: .classinfo.name (and typeid(obj)) does not print proper dynamic type when usi...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-08 11:37 UTC by Andrej Mitrovic
Modified: 2022-12-17 10:37 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 Andrej Mitrovic 2014-12-08 11:37:17 UTC
-----
module test;

import std.stdio;

interface I(T) { }
class D : I!(int) { }

void main()
{
    I!(int) i = new D;
    writeln(i.classinfo.name);
    writeln(typeid(i));  // ditto
}
-----

Prints:
test.I!(int).I

It should print:
test.D
Comment 1 Andrej Mitrovic 2014-12-08 11:37:33 UTC
This bug affects both D1 and D2.
Comment 2 Kenji Hara 2015-07-21 07:03:55 UTC
The issue also happens with non-template interface.

interface I { }
class D : I { }

void main()
{
    I i = new D;
    writeln(typeid(i));
    writeln(i.classinfo.name);  // ditto
}

Currently typeid() on interface reference returns the TypeInfo of "most derived" implemented interface.

import std.stdio;

interface I {}
interface J : I {}
interface K : I {}
class E : J, K {}

void main()
{
    E e = new E;
    J j = e;
    K k = e;
    I ij = j;
    I ik = k;
    writeln(typeid(ij));      // prints 'test.J'
    writeln(typeid(ik));      // prints 'test.K'
}

The interface references ij an ik point different positions of class instance e, so the two typeid()s also return different TypeInfo objects.
Comment 3 Lodovico Giaretta 2016-09-04 15:12:05 UTC
Being pedantic, the spec does not state what happens when the static type of the expression is an interface. In fact it says that "if the type is a class, it returns the TypeInfo of the dynamic type (i.e. the most derived type)".

But the current behaviour is certainly a bug, and very confusing when encountered. See for example http://forum.dlang.org/thread/gdjaoxypicsxlfvzwbvt@forum.dlang.org

This is worth a bugfix in both the implementation and the documentation.
Comment 4 Adam D. Ruppe 2021-01-26 02:07:23 UTC
the compiler could prolly just auto-cast to object then typeid it