D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 1918 - __traits(getVirtualFunctions) returns final functions
Summary: __traits(getVirtualFunctions) returns final functions
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 enhancement
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2008-03-12 21:32 UTC by Neia Neutuladh
Modified: 2015-06-09 01:14 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Neia Neutuladh 2008-03-12 21:32:40 UTC
static assert(__traits(getVirtualFunctions, Object, "notifyRegister").length == 0);

That should succeed; Object.notifyRegister is final and therefore not virtual.

Clearer example:
class C
{
   final void foo(){}
}
static assert(__traits(getVirtualFunctions, C, "foo").length == 0);

That also fails.
Comment 1 Neia Neutuladh 2008-11-16 08:22:04 UTC
Okay, I've worked out how all this works.

__traits (getVirtualFunctions) gets all non-static methods.

__traits (isVirtualFunction) returns true iff the argument is a non-static method.

__traits (isFinalFunction) returns true iff __traits (isVirtualFunction) returns true and the function is not marked final.

This is consistent, and 'fixing' the issues by changing features would result in a loss of functionality.

This is not consistent with the expected meaning of "virtual function". I request that the documentation for __traits be updated to include a definition for "virtual function".

I also request that the keywords be changed to "getInstanceMethods" and "isInstanceMethod" or something like that, for clarity.
Comment 3 Andrei Alexandrescu 2012-01-21 22:17:45 UTC
Could you please explain how a final function that doesn't override anything is ever supposed to be virtual?
Comment 4 Andrei Alexandrescu 2012-01-21 22:58:29 UTC
Documenting the agreement reached with Walter:

We'll define __traits(getVirtualMethods) to do the "right" thing, i.e. only include final methods that actually override something, and put __traits(getVirtualFunctions) on the slow deprecation path.
Comment 5 yebblies 2012-01-22 00:01:55 UTC
(In reply to comment #3)
> Could you please explain how a final function that doesn't override anything is
> ever supposed to be virtual?

Final functions can be seen as the end of a chain of overridden functions - even if the functions is also the first in the chain and therefore never uses virtual dispatch.  Either definition could be useful so having both available is probably the best solution.
Comment 6 Jonathan M Davis 2012-01-22 00:14:21 UTC
Why would it be useful to have a non-virtual function listed as a virtual function? Because that's what you're doing when mark a function which doesn't override anything final. It's _not_ in any kind of override chain.

Imagine for a moment that private functions become virtual by default like TDPL says (which I still hopes doesn't happen, since it'll be a major blow to the efficiency of the language) and final functions which didn't override anything were listed as virtual functions, pretty much _every_ function would then be returned by getVirtualFunctions.

If you're using getVirtualFunctions or getVirtualMethods or whatever, then you want the _virtual_ functions. As such, I see _zero_ reason to be returning final functions which don't override anything, and so I see no reason to keep getVirtualFunctions around.
Comment 7 github-bugzilla 2012-01-22 00:36:01 UTC
Commit pushed to https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/adb62254d26ab0b29f543f2562a55b331f4ef297
fix Issue 1918 - __traits(getVirtualFunctions) returns final functions
Comment 9 yebblies 2012-01-23 07:36:20 UTC
(In reply to comment #6)
> Why would it be useful to have a non-virtual function listed as a virtual
> function? Because that's what you're doing when mark a function which doesn't
> override anything final. It's _not_ in any kind of override chain.

Well, you make some good points.  I'm not entirely convinced there isn't some template forwardingy application (are there any uses for __traits(getVirtualFunctions)?) that would find the other way useful.

Anyway, I'm not sure this fix is correct.  After a little bit of poking around I discovered that a final method that doesn't override anything IS STILL VIRTUAL.  It still creates a vtable slot.  Is this a bug?  From what I can tell that means there is no way to actually create a non-virtual non-static member function.  If final functions that do not override anything were implicitly non-virtual, it would fix this bug.
Comment 10 Jonathan M Davis 2012-01-23 07:56:13 UTC
final functions which don't override anything _definitely_ shouldn't be virtual. There's no reason for them to be virtual, and it harms performance. If the compiler fails to make them non-virtual, then that's definitely a bug.
Comment 11 yebblies 2012-01-23 09:25:43 UTC
(In reply to comment #10)
> final functions which don't override anything _definitely_ shouldn't be
> virtual. There's no reason for them to be virtual, and it harms performance. If
> the compiler fails to make them non-virtual, then that's definitely a bug.

By the looks of it, the compiler manages to optimize out the virtual call with all final functions, which is probably why nobody ever noticed this before.  The problem seems to be that whether the functions actually needs a vtable slot is resolved much too late.  This is essential for linking with c++, which I'm trying to improve.