Bug 13833 – .classinfo.name (and typeid(obj)) does not print proper dynamic type when using an interface

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-12-08T11:37:17Z
Last change time
2024-12-13T18:38:19Z
Assigned to
No Owner
Creator
Andrej Mitrovic
Moved to GitHub: dmd#18921 →

Comments

Comment #0 by andrej.mitrovich — 2014-12-08T11:37:17Z
----- 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 by andrej.mitrovich — 2014-12-08T11:37:33Z
This bug affects both D1 and D2.
Comment #2 by k.hara.pg — 2015-07-21T07:03:55Z
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 by lodovico — 2016-09-04T15:12:05Z
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/[email protected] This is worth a bugfix in both the implementation and the documentation.
Comment #4 by destructionator — 2021-01-26T02:07:23Z
the compiler could prolly just auto-cast to object then typeid it
Comment #5 by robert.schadek — 2024-12-13T18:38:19Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18921 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB