www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21180] New: Wrong selection of opEquals overload in classes

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 puremagic.com
          Reporter: alexandru.ermicioi 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
-------------

--
Aug 20 2020