[Issue 21180] New: Wrong selection of opEquals overload in classes

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Aug 20 20:16:24 UTC 2020


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

          Issue ID: 21180
           Summary: Wrong selection of opEquals overload in classes
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: blocker
          Priority: P1
         Component: druntime
          Assignee: nobody at puremagic.com
          Reporter: alexandru.ermicioi at gmail.com

Test case:
-------------
import std;

class Silly {
    bool opEquals(const Silly silly) const @safe {
        return silly is this;
    }

    alias opEquals = Object.opEquals;
}

bool comp(T)() @safe {
    return new T() == new T();
}

void main()
{
    comp!Silly.writeln;
    comp!(const Silly).writeln;
    comp!(immutable Silly).writeln;
}
-------------

Expected behavior:
For "==" overload to be selected most narrow solution which is "bool
opEquals(const Silly silly) const @safe" and not "Object.opEquals".

To note here: If instead of ==, opEquals is directly called, overload is
properly resolved to most narrow solution.

Error:
-------------
onlineapp.d(12): Error: @safe function onlineapp.comp!(Silly).comp cannot call
@system function object.opEquals
/dlang/dmd/linux/bin64/../../src/druntime/import/object.d(162):       
object.opEquals is declared here
onlineapp.d(17): Error: template instance onlineapp.comp!(Silly) error
instantiating
onlineapp.d(12): Error: @safe function onlineapp.comp!(const(Silly)).comp
cannot call @system function object.opEquals
/dlang/dmd/linux/bin64/../../src/druntime/import/object.d(162):       
object.opEquals is declared here
onlineapp.d(18): Error: template instance onlineapp.comp!(const(Silly)) error
instantiating
onlineapp.d(12): Error: @safe function onlineapp.comp!(immutable(Silly)).comp
cannot call @system function object.opEquals
/dlang/dmd/linux/bin64/../../src/druntime/import/object.d(162):       
object.opEquals is declared here
onlineapp.d(19): Error: template instance onlineapp.comp!(immutable(Silly))
error instantiating
-------------

--


More information about the Digitalmars-d-bugs mailing list