D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4979 - Implementing an interface twice results in different a reference for each interface
Summary: Implementing an interface twice results in different a reference for each int...
Status: REOPENED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 major
Assignee: No Owner
URL:
Keywords: wrong-code
: 11178 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-10-02 20:57 UTC by Koroskin Denis
Modified: 2024-12-13 17:53 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Koroskin Denis 2010-10-02 20:57:43 UTC
interface Foo
{
}
 
class Bar : Foo
{
}
 
class Baz : Bar, Foo
{
}
 
void main()
{
	Baz baz = new Baz();
	Bar bar = baz;
	
	Foo foo1 = bar;
	Foo foo2 = baz;
	
	assert(foo1 is foo2);
}
 
 
foo1 and foo2 have the same type and point to the same object yet they have different addresses.
 
The test above passes for C# (http://ideone.com/xK5Mu) and C++ (http://ideone.com/MnnL8 virtual inheritance used, fails otherwise, of course).
Comment 1 Denis Shelomovskii 2013-12-04 08:52:35 UTC
*** Issue 11178 has been marked as a duplicate of this issue. ***
Comment 2 Denis Shelomovskii 2013-12-04 09:07:22 UTC
Currently Identity Expression over two `interface`s just compare pointers and over an `interface` and a `class` it adds a constant dependent on operands CT types to `class` reference and then compare pointers:
---
interface I {}
class A: I {}
class B: A, I {}

void main() @safe
{
    B b = new B;
    A a = b;
    I ia = a, ib = b;
    assert(ia is a);   // ok
    assert(ib is b);   // ok
    assert(ia is b);   // fails
    assert(ib is a);   // fails
    assert(ia is ib);  // fails
}
---

Thus Identity Expression has nothing to do with "the object references are for the same object" except the case of two classes unless this bug is fixed.

Please confirm this bug is fixable.

Anyway if it isn't going to be fixed in the immediate future Identity Expression over `interface`s should be appropriately documented and possibly deprecated.
Comment 3 Denis Shelomovskii 2013-12-04 09:12:45 UTC
Filed documentation Issue 11683.
Comment 4 basile-z 2020-09-06 09:07:25 UTC
D is is not the same as c# and others. On reference types this is more to perform a simple address comparison, exp when opEquals is overloaded.

The assert should be rather

  assert(typeid(foo1) is typeid(foo2))

but unfortunately typeid() on interfaces currently has a embarrassing bug, it returns a TypeInfo_Class, not a TypeInfo_Interface.
Comment 5 FeepingCreature 2020-09-06 10:31:30 UTC
No this is a genuine bug. See https://dlang.org/spec/expression.html#identity_expressions

"For class objects, identity is defined as the object references are for the same object."

I ia and ib are interface references, but they are the same object.

D must transform the interfaces into their class reference before comparing, even if the type is the same; else, multiple implementation of the same interface must be either merged or made illegal.
Comment 6 dlangBugzillaToGithub 2024-12-13 17:53:34 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17516

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB