Comment #0 by qs.il.paperinik — 2023-06-05T13:49:09Z
The [spec](https://dlang.org/spec/expression.html#identity_expressions) says that:
> For class objects, identity is defined as the object references are for the same object.
In general, this requires dynamic type checks. A pointer comparison is not enough, e.g. the following doesn’t work:
```d
interface I { void g(); }
interface I1 : I { void g1(); }
interface I2 : I { void g2(); }
interface J : I1, I2 { void h(); }
class C : J
{
override void g() { }
override void g1() { }
override void g2() { }
override void h() { }
}
void main() @safe
{
C c = new C;
I i1 = cast(I1) c;
I i2 = cast(I2) c;
assert(cast(Object) i1 is cast(Object) i2); // good
assert(i1 is i2); // fails
assert(c is i2); // fails
assert(c is cast(C) i2); // good
assert(c is cast(Object) i2); // good
assert(i1 is c); // good
}
```
Clearly, `i1` and `i2` refer to the same object, but an explicit dynamic cast using `cast(Object)` is necessary.
This can be solved two ways:
Either do dynamic type checks (unless the compiler can prove them redundant)
or make `is` comparisons with interface-type expressions an error, requiring an explicit cast.
Comment #1 by dlang-bot — 2023-10-16T01:36:02Z
@SixthDot created dlang/dmd pull request #15696 "fix issue 23972 - class identity check is broken" fixing this issue:
- fix issue 23972 - class identity check is broken
insert casts to Object when interfaces are used as IdentityExp operands
https://github.com/dlang/dmd/pull/15696
Comment #2 by razvan.nitu1305 — 2023-10-17T12:54:26Z
Thank you for taking the time of writing this bug report, however, as explained in this comment [1], the bug report is invalid. I1 and I2 are different types, therefore the is expression is correct in returning false. The fact that the pointer points to the same memory address is not relevant in this case since the compiler does not do any dataflow analysis to see where the interface was assigned.
[1] https://github.com/dlang/dmd/pull/15696#pullrequestreview-1682054176
Comment #3 by razvan.nitu1305 — 2023-10-17T13:33:31Z
Actually I was thinking of is expressions, not the is operator. The bug is valid.
Comment #4 by dkorpel — 2023-10-19T17:28:42Z
I think the spec is wrong (or too vague) here. The interface object has a slightly different address than the class object, so the pointers aren't equal.
`is` should be a simple bit compare, and not be doing casting logic for classes.
Comment #5 by qs.il.paperinik — 2023-10-20T01:04:19Z
(In reply to Dennis from comment #4)
> I think the spec is wrong (or too vague) here. The interface object has a
> slightly different address than the class object, so the pointers aren't
> equal.
> `is` should be a simple bit compare, and not be doing casting logic for
> classes.
No, in this case, it shouldn't. If one actually wants to compare the pointer values, one can always cast to `void*` and compare the pointers. The fact that the class handles are represented by pointers is really an implementation detail. The current behavior is both surprising and useless. If the `is` operator does not provide class object identity, it should not be there at all; the error message in that case could point out that an explicit cast to Object is needed, but if that's what programmers will do anyways, why not provide something like an operator for it that does The Right Thing™?
Comment #6 by dlang-bot — 2023-11-14T16:17:26Z
@dkorpel created dlang/dlang.org pull request #3726 "Fix 23972 - class identity check is broken" fixing this issue:
- Fix 23972 - class identity check is broken
https://github.com/dlang/dlang.org/pull/3726
Comment #7 by dlang-bot — 2023-11-14T16:31:30Z
dlang/dlang.org pull request #3726 "Fix 23972 - class identity check is broken" was merged into master:
- 99530f59602022930f6c2e634f3809a03fd0c8b6 by Dennis Korpel:
Fix 23972 - class identity check is broken
https://github.com/dlang/dlang.org/pull/3726
Comment #8 by b2.temp — 2023-12-16T20:28:43Z
*** Issue 19633 has been marked as a duplicate of this issue. ***