dmd icon indicating copy to clipboard operation
dmd copied to clipboard

No error for ambiguous struct `opCmp`

Open ntrel opened this issue 8 months ago • 2 comments

From https://dlang.org/spec/operatoroverloading.html#compare:

Both rewrites are tried... If they both match the same, but are different functions, an ambiguity error results.

The static assert below fails, the comparison can be compiled:

struct B
{
    int opCmp(ref C) { return -1; }
}

struct C
{
    int opCmp(ref B) { return 1; }
}

void main()
{
    B b;
    C c;
    static assert(!__traits(compiles, b < c)); // both C.opCmp and B.opcmp match exactly
}

Note: this example is simplified from the spec, which also fails to compile.

ntrel avatar Apr 11 '25 16:04 ntrel

I think this is a duplicate of #19835.

However, perhaps fixing this could break code relying on the current behaviour of using the first rewrite (i.e. left-hand expression's opCmp). In that case, another option would be to fix the docs.

ntrel avatar Apr 12 '25 18:04 ntrel

Note: this example is simplified from the spec, which also fails to compile.

the spec example indeed fails to compile as documented.

Prthmsh7 avatar May 02 '25 07:05 Prthmsh7