Identity of interfaces (or not)

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Dec 2 07:37:06 PST 2008


Tomas Lindquist Olsen wrote:
> Hi all.
> 
> I've spent that last weeks rewriting LDC's handling handling of 
> struct/union/class/interface to match DMD, in terms of datalayout, ABI 
> and correctness in general. It's done now, and seems to be working well, 
> however, running DStress on the latest revision, a new regression showed 
> up!
> 
> Test case:
> http://dsource.org/projects/dstress/browser/run/opIdentity_01.d
> 
> Original NG post with issue:
> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D.bugs&artnum=2676 

For what it's worth: both DMD and GDC also fail that test.

> I've failed to find any replies to the thread so I'm unsure if this test 
> is even valid.

It's also available from 
<http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.bugs&article_id=2676>, 
which shows that there were replies. Unfortunately, webnews is broken so 
if you click any of the links you need to edit the URL 
(s/digitalmars.d/digitalmars.d.bugs/).

> I'll paste the test case from DStress here as well:
> 
[snip]
> 
> Here's the problem. DMD and (now) LDC both compile this test fine, but 
> it fails at runtime on the marked line.
> 
> The problem is that B has two vtables that implement the interface I. 
> However, in A.addChild, i.parent get's the first one (introduced by A), 
> and in main, it gets the second one (J , which derived from I). The 
> identity check then fails because the pointers aren't the same, even 
> though it's the same object in question.
> 
> The easiest way I can see to make this work, would be to disable static 
> "downcasts" to interfaces, this way, in A.addChild, when 'this' is 
> converted to I, it would have to look through the ClassInfo and would 
> always use the same vtable for a single interface.
> 
> The other would be to change the ABI...
> 
> But, I'm not even sure if this is a bug or if D interfaces just work 
> this way, I've not been able to find much information...
> 
> So is it a bug? Or is identity for interfaces simply not guaranteed to 
> pass, even when it's the same Object in question?

Yeah, the only way to "fix" it would be to make *all* 
interface->interface casts *implicitly* dynamic, even though the 
compiler *knows* a way to get a correct I statically.

However I don't think that's a good idea. I'd say that if you really 
want interfaces to compare equal with 'is' you need to
(1) make sure no object implements any interface more than once, or
(2) cast to Object before comparing any interface that is 
multiply-implemented.



More information about the Digitalmars-d mailing list