Bug 2894 – abstract classes sometimes allow non-abstract bodyless functions
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2009-04-25T02:06:00Z
Last change time
2014-04-18T09:12:05Z
Keywords
link-failure, spec
Assigned to
nobody
Creator
kamm-removethis
Comments
Comment #0 by kamm-removethis — 2009-04-25T02:06:12Z
This compiles and links
abstract class C { void foo(); }
void main() {}
while this doesn't
interface I { void foo(); }
abstract class C : I { void foo(); }
void main() {}
with an undefined reference to `_D7abstfun1C3fooMFZv'.
Since methods in abstract classes may have bodies and being allowed to implement a member function elsewhere than the class definition is a goal (see resolution of bug 1289), the first case should also require a definition for foo. This is also what I would expect from reading the spec on abstract functions and classes.
Comment #1 by smjg — 2009-04-25T08:36:00Z
That the first example links is understandable - because C.foo is never used, the compiler doesn't create any references to it.
That the second example fails is semi-understandable - presumably the compiler creates a vtbl for C's implementation of I, but the linker cannot resolve it because no implementation of C.foo() has been linked in. However, it does seem that DMD could be better at DCE.
Less understandable is that this fails with the same error:
----------
abstract class C { void foo(); }
class D : C {}
void main() { }
----------
It was my understanding as well that a function in an abstract class is automatically abstract if no body is given. But it appears now that it's the same as in a non-abstract - if the function isn't declared abstract, it references an externally-defined function. Still, the spec probably needs to be clearer on the issue.
Comment #2 by github-bugzilla — 2012-01-23T00:48:15Z