Bug 21705 – Nullable!T.opEquals fails for T with non-const opEquals overload
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-03-12T23:07:30Z
Last change time
2021-03-15T11:30:07Z
Keywords
pull
Assigned to
No Owner
Creator
Paul Backus
Comments
Comment #0 by snarwin+bugzilla — 2021-03-12T23:07:30Z
Example program:
---
import std.typecons;
struct S
{
int n;
bool opEquals(S rhs) { return n == rhs.n; }
}
void main()
{
Nullable!S example = S();
S s;
assert(example.opEquals(s));
}
---
As of DMD 2.095.1, this program fails to compile, with the following error message:
---
bug.d(13): Error: template `std.typecons.Nullable!(S).Nullable.opEquals` cannot deduce function from argument types `!()(S)`, candidates are:
/usr/include/dmd/phobos/std/typecons.d(2720): `opEquals()(auto ref const(typeof(this)) rhs)`
/usr/include/dmd/phobos/std/typecons.d(2730): `opEquals(U)(auto ref const(U) rhs)`
with `U = S`
must satisfy the following constraint:
` is(typeof(this.get == rhs))`
---
Note that an explicit call to opEquals is necessary to prevent the compiler from falling back to the deprecated `alias get this`.
Comment #1 by snarwin+bugzilla — 2021-03-12T23:23:31Z
Another example:
---
import std.typecons;
struct S
{
int n;
bool opEquals(S rhs) { return n == rhs.n; }
}
void main()
{
Nullable!S example1 = S(1), example2 = S(1);
assert(example1 == example2);
}
---
Produces the following error message with DMD 2.095.1:
---
/usr/include/dmd/phobos/std/typecons.d(2726): Error: mutable method `bug.S.opEquals` is not callable using a `const` object
bug.d(6): Consider adding `const` or `inout` here
bug.d(12): Error: template instance `std.typecons.Nullable!(S).Nullable.opEquals!()` error instantiating
---
Comment #2 by dlang-bot — 2021-03-13T00:19:45Z
@pbackus created dlang/phobos pull request #7852 "Fix Issue 21705 - Nullable!T.opEquals fails for T with non-const opEq…" fixing this issue:
- Fix Issue 21705 - Nullable!T.opEquals fails for T with non-const opEquals overload
https://github.com/dlang/phobos/pull/7852
Comment #3 by dlang-bot — 2021-03-15T11:30:07Z
dlang/phobos pull request #7852 "Fix Issue 21705 - Nullable!T.opEquals fails for T with non-const opEq…" was merged into master:
- 4fb71b8c768f9afd79ca1bf73405216d0b557990 by Paul Backus:
Fix Issue 21705 - Nullable!T.opEquals fails for T with non-const opEquals overload
https://github.com/dlang/phobos/pull/7852