www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2894] New: abstract classes sometimes allow non-abstract bodyless functions

reply d-bugmail puremagic.com writes:

           Summary: abstract classes sometimes allow non-abstract bodyless
           Product: D
           Version: 1.043
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: kamm-removethis incasoftware.de

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.

Apr 25 2009
parent d-bugmail puremagic.com writes:

smjg iname.com changed:

           What    |Removed                     |Added
                 CC|                            |smjg iname.com
           Keywords|wrong-code                  |link-failure, spec

------- Comment #1 from smjg iname.com  2009-04-25 08:36 -------
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.

Apr 25 2009