D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21705 - Nullable!T.opEquals fails for T with non-const opEquals overload
Summary: Nullable!T.opEquals fails for T with non-const opEquals overload
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2021-03-12 23:07 UTC by Paul Backus
Modified: 2021-03-15 11:30 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Paul Backus 2021-03-12 23:07:30 UTC
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 Paul Backus 2021-03-12 23:23:31 UTC
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 Dlang Bot 2021-03-13 00:19:45 UTC
@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 Dlang Bot 2021-03-15 11:30:07 UTC
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