[Issue 23154] New: Linker error through templated __traits(compiles) in separate compilation unit

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jun 2 00:08:19 UTC 2022


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

          Issue ID: 23154
           Summary: Linker error through templated __traits(compiles) in
                    separate compilation unit
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: d.bugs at webfreak.org

minimized reproduction test case:
--- protocol.d
enum hasCallableFoobar(T) = __traits(compiles,
    {
        T.init.foobar();
    });

size_t barfoo(T)(T )
if (is(T == S[], S) && hasCallableFoobar!S)
{
    return 0;
}

size_t barfoo(T)(T val)
{
    static if (hasCallableFoobar!T)
        return val.foobar;
}

template ResolveThis(T)
{
    static if (is(T : SelfRef!U, U))
        alias ResolveThis = T;
    else
        alias ResolveThis = SelfRef!T[];
}

struct SelfRef(T)
{
    ResolveThis!T payload;

    size_t foobar()
    {
        static if (__traits(compiles, barfoo(payload)))
            barfoo(payload);
        return 0;
    }
}

alias SelfRefArray = SelfRef!bool;

--- foo.d
import protocol;

unittest
{
        SelfRef!(SelfRefArray) x;
}

---

to test:

#!/bin/bash
dmd -lib -ofprotocol.a protocol.d
dmd -main -c -oflib.o -unittest -I. foo.d
dmd -oftestlib lib.o protocol.a

observe that there is no compiler error for potential issues, but also we are
failing with a linker error:

ld: error: undefined symbol: _D8protocol__T7SelfRefTbZQl6foobarMFNaNbNiNfZm
>>> referenced by __main.d
>>>               lib.o:(_D8protocol__T6barfooTSQv__T7SelfRefTbZQlZQBcFNaNbNiNfQBgZm)
collect2: error: ld returned 1 exit status
Error: linker exited with status 1

Note: in my actual code this is much much more complex, basically calling

Variant!MyVariant j;
j.match!(v => v);

with MyVariant being defined as Variant!JsonAlgebraic in another compilation
unit.

--


More information about the Digitalmars-d-bugs mailing list