Thread overview
dub test errors during linking for struct with uninstantiated templated opEquals
Jun 21, 2020
Per Nordlöw
Jun 21, 2020
Per Nordlöw
Jun 21, 2020
kinke
Jun 21, 2020
kinke
Jun 21, 2020
Per Nordlöw
Jun 21, 2020
Per Nordlöw
Jun 22, 2020
Boris Carvajal
June 21, 2020
When a module containing


module nxt.rational;

Rational!(I1) rational(I1, I2)(I1 , I2)
{
    return typeof(return)();
}

struct Rational(Int)
{
    bool opEquals(Rhs)(Rhs _) {}
}

@nogc unittest
{
    auto _ = rational(1, 2);
}


is used as the sole module of a dub project then `dub test` fails as

.dub/build/phobos-next-test-library-unittest-linux.posix-x86_64-dmd_2092-06C9DAF60B64114484B3FE9AC9866D5F/phobos-next-test-library.o:src/nxt/rational.d:_D40TypeInfo_S3nxt8rational__T8RationalTiZQm6__initZ: error: undefined reference to '_D6object10_xopEqualsFMxPvMxQeZb'

When I remove

    bool opEquals(Rhs)(Rhs _) {}

the error goes away.

I'm suspecting this is a bug in dmd that fails to correctly check if a struct has its templated `opEquals` instantiated.

For a complete minimal sample project see

https://github.com/nordlow/phobos-next/tree/master/rational.reduced
June 21, 2020
On Sunday, 21 June 2020 at 12:42:11 UTC, Per Nordlöw wrote:
> When a module containing

BTW this fails for dmd stable and dmd nightly but works for ldc 1.22.0.
June 21, 2020
On Sunday, 21 June 2020 at 12:42:11 UTC, Per Nordlöw wrote:
> I'm suspecting this is a bug in dmd that fails to correctly check if a struct has its templated `opEquals` instantiated.

I'd suggest taking dub out of the equation then, so that you can file a DMD issue once you can confirm it. Your sample works with `-main -unittest` on run.dlang.io, so it seems to depend on how the test executable is built.
June 21, 2020
On Sunday, 21 June 2020 at 16:58:43 UTC, kinke wrote:
> Your sample works with `-main -unittest` on run.dlang.io

After quickly looking at your list of compiler flags in dub.sdl, the problem manifests when adding `-preview=in`. Please file an issue.
June 21, 2020
On Sunday, 21 June 2020 at 17:05:11 UTC, kinke wrote:
> On Sunday, 21 June 2020 at 16:58:43 UTC, kinke wrote:
>> Your sample works with `-main -unittest` on run.dlang.io
>
> After quickly looking at your list of compiler flags in dub.sdl, the problem manifests when adding `-preview=in`. Please file an issue.

Thanks a lot! I'll do so.
June 21, 2020
On Sunday, 21 June 2020 at 17:05:11 UTC, kinke wrote:
> Please file an issue.

Done: https://issues.dlang.org/show_bug.cgi?id=20968
June 22, 2020
On Sunday, 21 June 2020 at 22:48:20 UTC, Per Nordlöw wrote:
> On Sunday, 21 June 2020 at 17:05:11 UTC, kinke wrote:
>> Please file an issue.
>
> Done: https://issues.dlang.org/show_bug.cgi?id=20968

You can get more info compiling with "-verrors=spec"

"(spec:1) Error: template rational.Rational!int.Rational.opEquals cannot deduce function from argument types !()(const(Rational!int)) const, candidates are:"

From reading the docs for function opEquals (https://dlang.org/spec/operatoroverloading.html#equals).
It shows a templated version with a const modifier.
If you change the function signature to:

bool opEquals(Rhs)(Rhs _) const {return true;} // Note the 'const', and putting some return back because dustmite removed it

The program links fine.

So it seems a compiler error is gagged that should be exposed and also could be improved.