Bug 23291 – Members of arrays of shared classes cannot be compared
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-08-10T15:31:45Z
Last change time
2022-08-16T10:16:34Z
Keywords
pull
Assigned to
No Owner
Creator
Ruby The Roobster
Comments
Comment #0 by rubytheroobster — 2022-08-10T15:31:45Z
The following code worked before 2.099.1. Now it doesn't:
```d
void main()
{
shared class C { bool opEquals(const(shared(C)) rhs) const shared { return true;}}
const(C) c = new C();
const(C)[] a = [c];
const(C)[] b = [c];
assert(a[0] == b[0]);
}
```
Comment #1 by rubytheroobster — 2022-08-10T16:37:47Z
Same as issue 23140, it never worked!
```d
const(shared C) c = new shared C();
const(shared C)[] a = [c];
const(shared C)[] b = [c];
assert(a[0] == b[0]);
```
Also, you should put `override` in `opEquals` definition:
`override bool opEquals(const(shared(Object)) rhs) const shared`
Now the real problem is more clear, there is no shared compatible `opEquals` in D runtime so you can't even override it with your own implementation.
A quick search shows it's already reported in issues 4857, 9670, 14509.
Sad that one of them is 12 years old.
Comment #3 by rubytheroobster — 2022-08-11T16:36:54Z
(In reply to Boris Carvajal from comment #2)
> Same as issue 23140, it never worked!
>
This code actually did work as intended, 23140, unless you mean something else which I don't understand.
> ```d
> const(shared C) c = new shared C();
> const(shared C)[] a = [c];
> const(shared C)[] b = [c];
> assert(a[0] == b[0]);
> ```
>
> Also, you should put `override` in `opEquals` definition:
>
> `override bool opEquals(const(shared(Object)) rhs) const shared`
>
> Now the real problem is more clear, there is no shared compatible `opEquals`
> in D runtime so you can't even override it with your own implementation.
>
> A quick search shows it's already reported in issues 4857, 9670, 14509.
> Sad that one of them is 12 years old.
That's another issue that I also noticed.
Comment #4 by boris2.9 — 2022-08-12T05:18:17Z
(In reply to Ruby The Roobster from comment #3)
> (In reply to Boris Carvajal from comment #2)
> > Same as issue 23140, it never worked!
> >
>
> This code actually did work as intended, 23140, unless you mean something
> else which I don't understand.
Because type modifiers in class definitions were not applied before due to a bug, structs were though.
```d
shared class C {}
const(C) c = new C(); // this should be 'shared' implicitly
pragma(msg, typeof(c)); // now it's 'shared(const(C))' but before it was only 'const(C)'
```
So the code worked because 'shared' was discarded.
Comment #5 by dlang-bot — 2022-08-12T11:52:49Z
@RazvanN7 created dlang/dmd pull request #14365 "Fix Issue 23291 - Members of arrays of shared classes cannot be compared" fixing this issue:
- Fix Issue 23291 - Members of arrays of shared classes cannot be compared
https://github.com/dlang/dmd/pull/14365
Comment #6 by rubytheroobster — 2022-08-13T14:37:35Z
(In reply to Boris Carvajal from comment #4)
> (In reply to Ruby The Roobster from comment #3)
> > (In reply to Boris Carvajal from comment #2)
> > > Same as issue 23140, it never worked!
> > >
> >
> > This code actually did work as intended, 23140, unless you mean something
> > else which I don't understand.
>
> Because type modifiers in class definitions were not applied before due to a
> bug, structs were though.
>
> ```d
> shared class C {}
> const(C) c = new C(); // this should be 'shared' implicitly
> pragma(msg, typeof(c)); // now it's 'shared(const(C))' but before it was
> only 'const(C)'
>
> ```
> So the code worked because 'shared' was discarded.
Ah. I forgot about that. So it turns out that this bug is far older than I imagined.
Comment #7 by dlang-bot — 2022-08-16T10:16:34Z
dlang/dmd pull request #14365 "Fix Issue 23291 - Members of arrays of shared classes cannot be compared" was merged into master:
- 1f59acd97d2b830eb7f8848a3a21312452eb48e8 by RazvanN7:
Fix Issue 23291 - Members of arrays of shared classes cannot be compared
https://github.com/dlang/dmd/pull/14365