The following legal code fails to compile with a linker error, reduced with dustmite:
-----------------
import std.typecons;
struct Result
{
Nullable!(int) var;
}
interface A
{
bool func(S)(S a);
final void validate(Result res)
{
if (func(res))
res.var = 0;
}
}
void main() {}
-----------------
$ dmd ymd.d
Undefined symbols for architecture x86_64:
"__D3ymd1A22__T4funcTS3ymd6ResultZ4funcMFS3ymd6ResultZb", referenced from:
__D3ymd1A8validateMFS3ymd6ResultZv in ymd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: /usr/bin/gcc failed with status: 1
Comment #1 by dlang-bugzilla — 2016-05-27T15:02:46Z
How can this code be valid? You are declaring a templated function with no implementation. Templated functions cannot be virtual. I think this shouldn't even compile.
Comment #2 by jack — 2016-05-28T03:19:30Z
(In reply to Vladimir Panteleev from comment #1)
> How can this code be valid?
According to the spec ;)
> You are declaring a templated function with no
> implementation. Templated functions cannot be virtual. I think this
> shouldn't even compile.
If non final template functions aren't possible, then the compiler must check for this and the spec needs to be updated.
I would like to know why this isn't possible though.
Comment #3 by dlang-bugzilla — 2016-05-28T03:38:55Z
(In reply to Jack Stouffer from comment #2)
> (In reply to Vladimir Panteleev from comment #1)
> > How can this code be valid?
>
> According to the spec ;)
It may be syntactically valid but it is semantically useless.
> > You are declaring a templated function with no
> > implementation. Templated functions cannot be virtual. I think this
> > shouldn't even compile.
>
> If non final template functions aren't possible, then the compiler must
> check for this and the spec needs to be updated.
I think final is implied for template methods.
> I would like to know why this isn't possible though.
Because the compiler must generate a virtual function call table entry for every instantiation, but it can't do that unless it knows the instantiations ahead of time (i.e. which template arguments will that template be instantiated with ANYWHERE in the program). This is something that would only be possible if the D compiler had access to the source code of the entire program during compilation, which is not how it works currently (and would preclude partial compilation to machine code).
Comment #4 by jack — 2016-05-31T14:34:21Z
Ok, I am changing this to an improved error message request. Also, IMO it should be mentioned on the interface page of the spec that template functions that don't use the interface template parameters are auto marked as final.
Comment #5 by robert.schadek — 2024-12-13T18:48:07Z