[Issue 19517] New: Spurious compiler warning with const toHash and alias this classes

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Dec 26 22:50:22 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=19517

          Issue ID: 19517
           Summary: Spurious compiler warning with const toHash and alias
                    this classes
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: hsteoh at quickfur.ath.cx

Code:
------
class C { }
struct S
{
    C c;
    alias c this;
}
Nullable!S s;

struct Nullable(T) // reduced from std.typecons.Nullable
{
    union U
    {
        T t;
    }

    U u = U.init;

    size_t toHash() const @safe nothrow
    {
        static if (__traits(compiles, .hashOf(u.t))) {}
        return 0;
    }
}
------

Compiler output:
------
Warning: struct S has method toHash, however it cannot be called with const(S)
this.
------

This warning is spurious, because the whole point of the static if is to check
for exactly this case, and to provide workaround code for it.  As things stand,
it is impossible to avoid the warning, even if there is workaround code for it.

Related to issue #18682 that introduced .toHash to std.typecons.Nullable. The
bug here, however, is related the compiler, and appears to be related to the
compiler confusing Nullable.toHash with S.c.toHash because of the `alias this`.

--


More information about the Digitalmars-d-bugs mailing list