Bug 2196 – Link errors with an abstract class's method implementing an interface

Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2008-07-05T12:11:00Z
Last change time
2014-02-24T16:00:21Z
Keywords
link-failure, spec
Assigned to
bugzilla
Creator
d

Comments

Comment #0 by d — 2008-07-05T12:11:48Z
The following code generates an error at link time: interface A { void foo (); } abstract class B : A { void foo (); } void main() {} The error: abstract.o: In function `_TMP0': abstract.d:(.text+0x4): undefined reference to `_D8abstract1B3fooMFZv' collect2: ld returned 1 exit status Marking foo in B as abstract avoids the problem. The documentation isn't at all clear on whether this should be the case. Tried with dmd 1.031 and 1.027, and gcc (Debian 4.3.1-2) 4.3.1.
Comment #1 by 2korden — 2008-07-06T05:09:54Z
I'm not sure if it is a bug, this is a correct behaviour to me. In this code: abstract class B : A { void foo (); } you define a method foo() and should provide a function body somewhere. Merely marking class abstract says that you can't instantiate it, but you can any other class that subclasses from it, unless it is abstract, too. If you mark you method as abstract it means that there is no body, child class should provide an implementation. Interface methods are implicitly abstract.
Comment #2 by gide — 2009-04-23T05:12:23Z
(In reply to comment #1) > I'm not sure if it is a bug, this is a correct behaviour to me. > If you mark your method as abstract it means that there is no body, > child class should provide an implementation. The following compiles correctly, the original code should not link. interface A { void foo (); } abstract class B : A { abstract void foo (); } void main() {}
Comment #3 by d — 2009-04-23T08:22:25Z
Agreed. I think what I was unsure on is why the linker wanted the definitions of an abstract class that wasn't (attempted to be) used. But of course having to explicitly mark such functions as abstract does make it clear whether or not the function was intended to be defined in that class.