Bug 1918 – __traits(getVirtualFunctions) returns final functions
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2008-03-12T21:32:00Z
Last change time
2015-06-09T01:14:36Z
Keywords
spec
Assigned to
nobody
Creator
dhasenan
Comments
Comment #0 by dhasenan — 2008-03-12T21:32:40Z
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 by dhasenan — 2008-11-16T08:22:04Z
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 #2 by github-bugzilla — 2012-01-21T21:55:30Z
Could you please explain how a final function that doesn't override anything is ever supposed to be virtual?
Comment #4 by andrei — 2012-01-21T22:58:29Z
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 by yebblies — 2012-01-22T00:01:55Z
(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 by issues.dlang — 2012-01-22T00:14:21Z
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 by github-bugzilla — 2012-01-22T00:36:01Z
(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 by issues.dlang — 2012-01-23T07:56:13Z
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 by yebblies — 2012-01-23T09:25:43Z
(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.