D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3655 - Virtual functions without bodies are not optimized away.
Summary: Virtual functions without bodies are not optimized away.
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-12-27 04:12 UTC by Eldar Insafutdinov
Modified: 2015-06-09 01:26 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Eldar Insafutdinov 2009-12-27 04:12:27 UTC
This is compiled fine, as final method without a body is optimized away:

class Boo
{
    final void foo();
}

However this does not:

class Boo
{
    void foo();
}

linker fails with:

main.o:(.rodata+0x194): undefined reference to `_D4main3Boo3fooMFZv'

This might be because function is put into vtable. Is it easy to fix?
Comment 1 Eldar Insafutdinov 2009-12-27 04:13:25 UTC
(In reply to comment #0)
> This is compiled fine, as final method without a body is optimized away:
> 
> class Boo
> {
>     final void foo();
> }
> 

Of course it is optimized away if it is not used.
Comment 2 Eldar Insafutdinov 2009-12-27 05:03:55 UTC
Ok, actually I am wrong here, final methods are not optimized away - they just don't exist. And if nobody uses them - you are safe. But the similar behavior should be present for virtual functions as well I believe - virtual function without a body should not be present in vtable.
Comment 3 Walter Bright 2009-12-28 21:14:32 UTC
The compiler is working as designed.

The final function is not virtual, so it is never needed in the vtbl[]. Hence, there is no undefined reference to it in the vtbl[].

The non-final function is virtual, and so a reference to it is put into the vtbl[]. The function's implementation must exist somewhere, and if it did, the linker would put a reference to the implementation in the vtbl[]. This feature allows one to have an implementation that is hidden from the user of the class.

If you truly do not want to implement the function, declare it as 'abstract'. Then, a NULL is put in the corresponding place in the vtbl[].

Not a bug.