Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linker error, not failed compilation, with interface final function calling virtual template function #19134

Open
dlangBugzillaToGithub opened this issue May 27, 2016 · 4 comments

Comments

@dlangBugzillaToGithub
Copy link

Jack Stouffer (@JackStouffer) reported this on 2016-05-27T14:56:59Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=16084

CC List

Description

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
@dlangBugzillaToGithub
Copy link
Author

dlang-bugzilla (@CyberShadow) commented on 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.

@dlangBugzillaToGithub
Copy link
Author

jack (@JackStouffer) commented on 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.

@dlangBugzillaToGithub
Copy link
Author

dlang-bugzilla (@CyberShadow) commented on 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).

@dlangBugzillaToGithub
Copy link
Author

jack (@JackStouffer) commented on 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant